解决UIScrollView,UIImageView等控件不能响应touch事件的问题

关于UIScrollView,UIImageView等控件不能响应touch事件,主要涉及到事件响应者链的问题,如果在UIScrollView,UIImageView等控件添加了子View,这样事件响应将会被UIScrollView,UIImageView等控件终止,而且这些控件的userInteractionEnabled属性默认的是NO,所以想要解决使用触摸事件,我通过两种方法进行解决。

方法一:

创建UIScrollView,UIImageView等控件的类别文件,将touch的四个方法进行父类方法重写:(创建Objective-C File文件,然后修改类型,创建对应的类别文件)

#import "UIScrollView+TouchEvent.h"

@implementation UIScrollView (TouchEvent)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesBegan:touches withEvent:event];
    [super touchesBegan:touches withEvent:event];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesMoved:touches withEvent:event];
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesEnded:touches withEvent:event];
    [super touchesEnded:touches withEvent:event];
}
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [[self nextResponder] touchesEnded:touches withEvent:event];
    [super touchesEnded:touches withEvent:event];
}
@end

方法二:通过给UIScrollView,UIImageView等控件添加手势,执行对应的方法

如:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 200, 400)];
    bgScrollView.backgroundColor = [UIColor lightGrayColor];

//添加手势

    UITapGestureRecognizer *tapGestureR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(updateImageVAction:)];
    [bgScrollView setUserInteractionEnabled:YES];
    [bgScrollView addGestureRecognizer:tapGestureR];

    [self.view addSubview:bgScrollView];
    
    textFie = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];
    textFie.layer.borderWidth = 1;
    [bgScrollView addSubview:textFie];
    
}

//手势执行的方法

-(void)updateImageVAction:(UITapGestureRecognizer *)tapGR
{
    [textFie resignFirstResponder];
}



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
UIScrollView 上添加手势识别器时,有可能会出现手势冲突的情况。这通常是由于 UIScrollView 自身的手势识别器和添加的手势识别器之间的冲突引起的。 要解决这个问题,有几种可能的方法: 1. 禁用 UIScrollView手势识别器。这可以通过将 UIScrollView 的属性 `canCancelContentTouches` 和 `delaysContentTouches` 设置为 `NO` 来实现。但是,这样会使 UIScrollView 的滚动功能也被禁用,因此在某些情况下可能并不可取。 2. 使用手势识别器代理方法来控制手势冲突。在手势识别器代理方法 `gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:` 中,可以根据需要返回 `YES` 或 `NO`,以允许或禁止多个手势识别器同时识别手势。 3. 将手势识别器添加到 UIScrollView 的子视图中,而不是直接添加到 UIScrollView 上。这样,手势识别器就不会与 UIScrollView手势识别器冲突了。 下面是一个示例代码,演示了如何在 UIScrollView 上添加一个 UISwipeGestureRecognizer 手势识别器,并处理手势冲突的情况: ```objc // 创建一个 UISwipeGestureRecognizer 手势识别器 UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; // 设置手势方向 swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; // 将手势识别器添加到 UIScrollView 上 [scrollView addGestureRecognizer:swipeGestureRecognizer]; // 实现手势识别器代理方法,处理手势冲突 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] && [otherGestureRecognizer.view isKindOfClass:[UIScrollView class]]) { return NO; // 禁止 UISwipeGestureRecognizerUIScrollView 同时识别手势 } return YES; } // 处理 UISwipeGestureRecognizer 手势 - (void)handleSwipe:(UISwipeGestureRecognizer *)gestureRecognizer { // 处理手势事件 } ``` 希望这些提示可以帮助你解决 UIScrollView 上的手势冲突问题

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hbblzjy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值