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

来源:互联网 发布:淘宝 seo 教程 编辑:程序博客网 时间:2024/05/16 02:21

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 0
原创粉丝点击