美文网首页
iOS-手势

iOS-手势

作者: CDLOG | 来源:发表于2018-07-24 16:31 被阅读20次

手势使用方法

1.创建手势
2.添加手势
3.实现手势方法

添加点按手势

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
//手势也可以设置代理
tap.delegate = self;
//添加手势
[self.imageV addGestureRecognizer:tap];

代理方法:
是否允许接收手指

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    让图片的左边不可以点击,
    获取当前手指所在的点.是在图片的左边还是在图片的右边.
    CGPoint curP = [touch locationInView:self.imageV];
    if (curP.x > self.imageV.bounds.size.width * 0.5) {
        在图片的右侧
        return YES;
    }else{
        在图片的左侧
        return NO;
    }
    return YES;
}

添加长按手势

UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longP:)];  
[self.imageV addGestureRecognizer:longP];

//当长按时调用.
//这个方法会调用很多次, 当手指长按在上面不松,来回移动时,会持续调用.
//所以要判断它的状态.
- (void)longP:(UILongPressGestureRecognizer *)longP{
    if(longP.state == UIGestureRecognizerStateBegan){
        NSLog(@"开始长按");
    }else if(longP.state == UIGestureRecognizerStateChanged){
        NSLog(@"长按时手指移动");
    }else if(longP.state == UIGestureRecognizerStateEnded){
        NSLog(@"手指离开屏幕");
    }
}

添加轻扫手势

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
 //轻扫手势默认是向右边称轻扫
 //可以设置轻扫的方法.
 //一个轻扫手势只能设置一个方法的轻扫.想要让它有多个方向的手势,必须得要设置的
  swipe.direction =  UISwipeGestureRecognizerDirectionLeft;
  [self.imageV addGestureRecognizer:swipe];
   
//添加轻扫手势
UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
//轻扫手势默认是向右边称轻扫
//可以设置轻扫的方法.
//一个轻扫手势只能设置一个方法的轻扫.想要让它有多个方向的手势,必须得要设置的
  swipe2.direction =  UISwipeGestureRecognizerDirectionUp;
  [self.imageV addGestureRecognizer:swipe2];


- (void)swipe:(UISwipeGestureRecognizer *)swipe{
     //判断的轻扫的方向
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"向左轻扫");
    }else if(swipe.direction == UISwipeGestureRecognizerDirectionUp){
        NSLog(@"向上轻扫");
    }
}

添加平移手势

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.imageV addGestureRecognizer:pan];

//当手指拖动时调用
- (void)pan:(UIPanGestureRecognizer *)pan{
   
    //拖动手势也有状态
    if(pan.state == UIGestureRecognizerStateBegan){
         // 开始拖动  
    }else if(pan.state == UIGestureRecognizerStateChanged){
       // 可能获取不当前手指移动的举例
        //是相对于最原始的点
        CGPoint transP = [pan translationInView:self.imageV];
       
        //会清空上一次的形变
        self.imageV.transform = CGAffineTransformMakeTranslation(transP.x,transP.y);
        self.imageV.transform = CGAffineTransformTranslate(self.imageV.transform, transP.x, transP.y);

       // 复位,让它相对于上一次.
        [pan setTranslation:CGPointZero inView:self.imageV];
    }else if(pan.state == UIGestureRecognizerStateEnded){
       // 结束拖动       
    }
}

添加捏合手势

UIPinchGestureRecognizer *pinGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinGes:)];
//设置代理使其能够同时支持多个手势
pinGes.delegate = self;
[self.imageV addGestureRecognizer:pinGes];


//旋转时调用
- (void)pinGes:(UIPinchGestureRecognizer *)pin{
    self.imageV.transform = CGAffineTransformScale(self.imageV.transform, pin.scale, pin.scale);
    //复位
    [pin setScale:1];
   
}

添加旋转手势

 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
 //设置代理使其能够同时支持多个手势
 rotation.delegate = self;
 [self.imageV addGestureRecognizer:rotation];


//当手指开始旋转时调用.
- (void)rotation:(UIRotationGestureRecognizer *)rotation{
    self.imageV.transform = CGAffineTransformRotate(self.imageV.transform, rotation.rotation);
    //复位.
    [rotation setRotation:0];
}

相关文章

  • iOS-手势

    手势使用方法 1.创建手势2.添加手势3.实现手势方法 添加点按手势 代理方法:是否允许接收手指 添加长按手势 添...

  • iOS-手势

    UIResponder UIResponder:是一个响应者(传达者)用来响应用户的触摸屏幕的某些事件 手势 手势...

  • iOS-手势

    iOS中所有的手势操作都继承于UIGestureRecognizer,这个类本身不能直接使用。这个类中定义了这几种...

  • iOS-手势识别

    ios系统提供了一些常用的手势(UIgestureRecognizer的子类),方便我们直接使用。 UIGestu...

  • iOS-手势UIGestureRecognier详解

    一. 手势UIGestureRecognier简介 iOS 3.2之后,苹果推出了手势识别功能(Gesture R...

  • IOS-手势事件简述

    在iOS中事件分为三类: 触摸事件:通过触摸。手势进行触发事件(七大手势)运动时间:通过加速器进行触发的事件(摇一...

  • iOS-解决手势冲突

    最近的项目中里面有一个需求,UIScrollerView 上面放了一个横向滑动的UICollectionView,...

  • iOS-根据Pan手势,精确计算手势方向

    本文将分享: 怎么根据Pan手势,精确计算手势方向 哈哈。。。老样子,上传一张动漫图。 Talk is cheap...

  • IOS-手势指定响应区域

    在开发过程中,我们可能会遇到这个问题. 当我们给一个view添加了手势,但是我们又不想点击view上面的视图也触发...

  • IOS-手势图片点击放大

    需要实现的效果:当点击一张图片时,可以扩大到整个屏幕.再次点击时缩小到原来的大小实现思路:1.封装一个继承UIIm...

网友评论

      本文标题:iOS-手势

      本文链接:https://www.haomeiwen.com/subject/zhdemftx.html