解决UIScrollView 的点击事件

目前有两种方法

第一种 通过 Category 扩展 UIScrollView 对象,添加触摸事件,(不建议,后续扩展不方便)代码如下

@implementation UIScrollView (ExtendTouch)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesBegan:touches withEvent:event];
    [super touchesBegan:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    CGFloat startx = [touch locationInView:self].x;
    NSLog(@"%f",startx);
}

-(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];
}

 第二种 添加手势 (推荐,易于维护)

    
    //添加点按击手势监听器
    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapUiscrollView:)];
    //设置手势属性
    tapGesture.delegate = self;
    tapGesture.numberOfTapsRequired=1;//设置点按次数,默认为1,注意在iOS中很少用双击操作
    tapGesture.numberOfTouchesRequired=1;//点按的手指数
    [self.scrllview addGestureRecognizer:tapGesture];

  

@interface ViewController ()<UIScrollViewDelegate,UIGestureRecognizerDelegate>

  

posted @ 2015-04-23 15:16  匹夫鹏  阅读(2027)  评论(0编辑  收藏  举报