UIScrollView无法响应touch事件解决方法

UIScrollView不会响应touch事件,这样就无法在touchesEnd方法中做一些事情了,比如关闭键盘等等。可以写个category解决这个问题


#import "UIScrollView+TouchEvent.h"


@implementation UIScrollView (TouchEvent)


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [[selfnextResponder] touchesBegan:toucheswithEvent:event];

    [super touchesBegan:touches withEvent:event];

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    [[selfnextResponder] touchesMoved:toucheswithEvent:event];

    [super touchesMoved:touches withEvent:event];

}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    [[selfnextResponder] touchesEnded:toucheswithEvent:event];

    [super touchesEnded:touches withEvent:event];

}


@end







UITableView是不会响应touchesBegan:withEvent:之类的UIResponder的方法的。因此,加在其上的所有视图的响应者链就断了。如果在UITableView其上加任何的自身不具备类似UIButton一样有目标动作机制的UIView及其子类控件的时候,这个控件也不会响应touchesBegan:withEvent:方法。即便是设置该控件的userInteractionEnabled为YES也没用。

如此一来,如果想要这些控件具有交互性能怎么办?有一种很直观的方法,给这个控件加上手势识别器。

- (void)addAGesutreRecognizerForYourView

{

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesturedDetected:)]; // 手势类型随你喜欢。

    tapGesture.delegate = self;

    [yourView addGestureRecognizer:panGesture];

}

- (void)tapGesturedDetected:(UITapGestureRecognizer *)recognizer

{

    // do something

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值