2017-01-30 95 views
1

我在UIImageView上添加了UITapGesture。已添加所有代码。 ImageView描述显示手势,但仍不起作用。点按手势不起作用

@interface VideoBaseProgramEditorViewController()<UIGestureRecognizerDelegate> { 

UITapGestureRecognizer* tapGesture; 
} 

...

tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped)]; 
       [tapGesture setNumberOfTapsRequired:1]; 
       tapGesture.delegate = self; 
       thumbImageView.userInteractionEnabled = YES; 
      [thumbImageView addGestureRecognizer:tapGesture]; 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { 
    UIView *view = touch.view; 
    NSLog(@"%@",view); 
    return YES; 
} 

-(void) viewTapped { 

    [self loadVideo]; 
} 

问题:shouldReceiveTouch方法也没有要求。

图像视图描述显示日志中添加的手势。

UIImageView:0x7bb7b370; frame =(0 0; 736 485); opaque = NO; autoresize = W + H; gestureRecognizers = NSArray:0x7bc80910;层= CALayer:0x78f793d0

任何帮助赞赏。

+0

你也可以分享viewTapped方法吗? – EDUsta

+0

当然。 Plz检查编辑。 –

+0

您的代码正在工作。 – Sunny

回答

0

您需要在UIImageView上启用用户交互,默认情况下禁用该功能。在您的storyboardxib上选择您的UIImageView,然后在Attribute Inspector中勾选User Interaction Enabled

+0

我在我设置手势时启用它 –

+0

您的代码正在工作检查是否没有任何其他视图覆盖您的UIImageView – Sunny

+0

没有视图重叠请参阅以下堆栈: –

0

试试这个。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     [super touchesBegan:touches withEvent:event]; 
     NSLog(@"Successfully Tap"); 
    } 
0

你连接你的IBOutlet thumbImageView? 你如何申报你的房产?

也许试试下面的代码。与代码和xib一起使用userInteractionEnabled

#import "ViewController.h" 

@interface ViewController() <UIGestureRecognizerDelegate> 

@property (nonatomic, strong) UITapGestureRecognizer* tapGesture; 

@property (nonatomic, weak) IBOutlet UIImageView *thumbImageView; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)]; 
    _tapGesture.delegate = self; 
    [_tapGesture setNumberOfTapsRequired:1]; 
    // _thumbImageView.userInteractionEnabled = YES; // Uncomment if you allow interaction from code 
    [_thumbImageView addGestureRecognizer:_tapGesture]; 
} 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { 
    UIView *view = touch.view; 
    NSLog(@"%@",view); 
    return YES; 
} 

- (void)viewTapped:(UITapGestureRecognizer *)sender { 
    UIView *view = sender.view; 
    NSLog(@"%@",view); 
} 

@end 
+0

是我的IBOutlet已连接 __weak IBOutlet UIImageView * thumbImageView; –