点按手势不起作用

时间:2017-01-30 10:15:25

标签: objective-c xcode uitapgesturerecognizer

我在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; layer = CALayer:0x78f793d0

任何帮助表示感谢。

3 个答案:

答案 0 :(得分:0)

您需要在UIImageView上启用默认禁用的用户互动。在storyboardxib选择您的UIImageView,然后在“属性检查器”中勾选User Interaction Enabled

答案 1 :(得分:0)

试试这个。

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

答案 2 :(得分:0)

您是否链接了您的IBOutlet thumbImageView? 你如何申报你的财产?

也许尝试以下代码。使用userInteractionEnabled代码和xib。

#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