zoukankan      html  css  js  c++  java
  • 解决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>
    

      

  • 相关阅读:
    Android网络开发的那些事儿
    first day to Ruby on rails
    [转]Windows SDK与DirectX SDK集成
    Windows8
    [转]MPI--MPI+VS2010 配置及编译
    codeblock添加头文件路径和静态库路径
    汇编笔记1:debug
    Eclipse Error
    Android SDk 离线安装方法
    求一程序员合租,回龙观东大街地铁站十分钟,精装次卧2000,无需押金,一共两家
  • 原文地址:https://www.cnblogs.com/ak23173969/p/4450644.html
Copyright © 2011-2022 走看看