【问题标题】:Tap gesture is not working点击手势不起作用
【发布时间】:2017-01-30 10:15:25
【问题描述】:

我在 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;帧 = (0 0; 736 485);不透明=否;自动调整大小 = W+H;手势识别器 = NSArray: 0x7bc80910;层 = CALayer: 0x78f793d0

任何帮助表示赞赏。

【问题讨论】:

  • 能否分享一下viewTapped方法?
  • 当然。请检查编辑。
  • 您的代码正在运行。
  • 它在我的页面中不起作用:(正如我所说的,甚至 shouldReceiveTouch 方法都没有调用。
  • 你是如何在你的 ViewController 中添加 imageView 的?通过故事板?还是通过代码手动?

标签: objective-c xcode uitapgesturerecognizer


【解决方案1】:

您需要在UIImageView 上启用默认禁用的用户交互。在您的storyboardxib 上选择您的UIImageView 并在属性检查器中勾选User Interaction Enabled

【讨论】:

  • 我在代码中启用了它,当我在上面设置手势时。
  • 您的代码正在运行。检查您的 UIImageView 上是否没有任何其他视图。
  • ;层 = >, >, >, ;层 =
  • 这是该死的简单代码。它仍然无法正常工作。这就是为什么我想知道还剩下什么。
【解决方案2】:

试试这个。

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

【讨论】:

    【解决方案3】:

    您是否链接了您的 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 
    

    【讨论】:

    • 是的,我的 IBOutlet 已连接 __weak IBOutlet UIImageView *thumbImageView;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 2016-06-30
    • 1970-01-01
    • 2018-01-20
    相关资源
    最近更新 更多