zoukankan      html  css  js  c++  java
  • ios多手势事件

    开发ios应用时我们经常用到多手势来处理事情,如给scrollView增加点击事件,scrollView不能响应view的touch事件,但有时候却要用到多手势事件,那么我们可以给这个scrollView增加自己的手指事件。给imageView增加点击、放大、旋转等都可以通过多手势来完成。
    下面就介绍下各种的手势的使用方法,及注意事项。
    - (void)viewDidLoad
     
    {
        [super viewDidLoad];
        //为了能响应多手势事件,imageView的userInteractionEnabled属性要设为YES.
        self.imageView.userInteractionEnabled=YES;
        
        //1、手指点击事件
        //单指单击
        UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                          action:@selector(fingerIncident:)];
    //手指数
        singleFingerOne.numberOfTouchesRequired = 1;
    //点击次数        
        singleFingerOne.numberOfTapsRequired = 1;  
    //设置代理方法         
        singleFingerOne.delegate= self;    
    //增加事件者响应者,                 
        [self.imageView addGestureRecognizer:singleFingerOne]; 
        
        //单指双击
        UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                          action:@selector(fingerIncident:)];
        singleFingerTwo.numberOfTouchesRequired = 1;
        singleFingerTwo.numberOfTapsRequired = 2;
        singleFingerTwo.delegate= self;
        [self.imageView addGestureRecognizer:singleFingerTwo];
        
        
        //双指单击
        UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                          action:@selector(fingerIncident:)];
        doubleFingerOne.numberOfTouchesRequired = 2;
        doubleFingerOne.numberOfTapsRequired = 1;
        doubleFingerOne.delegate= self;
        [self.imageView addGestureRecognizer:doubleFingerOne];
        
        //双指双击
        UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                          action:@selector(fingerIncident:)];
        doubleFingerTwo.numberOfTouchesRequired = 2;
        doubleFingerTwo.numberOfTapsRequired = 2;
        doubleFingerTwo.delegate= self;
        [self.imageView addGestureRecognizer:doubleFingerTwo];
        
        //如果不加下面的话,当单指双击时,会先调用单指单击中的处理,再调用单指双击中的处理
        [singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];
        //同理双指也是如此
        [doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];
    
        
        
        //2、手势为捏的姿势:按住option按钮配合鼠标来做这个动作在虚拟器上
        UIPinchGestureRecognizer *pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)];
        [self.imageView addGestureRecognizer:pinchGesture];//imageView添加手势识别
        [pinchGesture release];
        
        //3、旋转手势:按住option按钮配合鼠标来做这个动作在虚拟器上
        UIRotationGestureRecognizer *rotateGesture=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotateGesture:)];
        [self.imageView addGestureRecognizer:rotateGesture];
        [rotateGesture release];
        
        //4、拖手势
        UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];
        [self.imageView addGestureRecognizer:panGesture];
        [panGesture release];
        
        //当实现上面拖手势(UIPanGestureRecognizer)后将不能响应下面的左右划(UISwipeGestureRecognizer)事件,事件会被拖手势栏截,所以左右划和拖手势只能选其一
        //右划
        UISwipeGestureRecognizer *swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
        [self.imageView addGestureRecognizer:swipeGesture];
        [swipeGesture release];
        //左划
        UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
        swipeGesture.direction=UISwipeGestureRecognizerDirectionLeft;//不设置黑夜是右
        [self.imageView addGestureRecognizer:swipeLeftGesture];
        [swipeLeftGesture release];
        
        UILongPressGestureRecognizer *longpressGesutre=[[UILongPressGestureRecognizer alloc]initWithTarget:self
                                                                                                    action:@selector(handleLongpressGesture:)];
        //长按时间为1秒
        longpressGesutre.minimumPressDuration=0.5;
        //允许15秒中运动
        longpressGesutre.allowableMovement=3;
        //所需触摸1次
        longpressGesutre.numberOfTouchesRequired=1;
        [self.imageView addGestureRecognizer:longpressGesutre];
        [longpressGesutre release];
    }
    
    //手指点击事件
    - (void)fingerIncident:(UITapGestureRecognizer *)sender
    {
        if (sender.numberOfTouchesRequired==1) {
            //单指点击事件
            if(sender.numberOfTapsRequired == 1) {
                //单指单击
                NSLog(@"单指单击");
                [sender.view.layer removeAllAnimations];
            }
            else if(sender.numberOfTapsRequired ==2 ){
                //单指双击
                NSLog(@"单指双击");
            }
        }
        else if (sender.numberOfTouchesRequired==2) {
            //双指点击事件
            if(sender.numberOfTapsRequired == 1) {
                //双指单击
                NSLog(@"双指单击");
            }
            else if(sender.numberOfTapsRequired ==2 ){
                //双指双击
                NSLog(@"双指双击");
            }
        }
    }
    
    //手指左右滑动事件
    -(void)handleSwipeGesture:(UISwipeGestureRecognizer *)sender{
        if (sender.direction==UISwipeGestureRecognizerDirectionLeft) {
            NSLog(@"左滑");
        }
        else{
            NSLog(@"右滑");
        }
    }
    
    //手指捏事件
    -(void)handlePinchGesture:(UIPinchGestureRecognizer *)sender{
        NSLog(@"");
        
        if([sender state] == UIGestureRecognizerStateEnded) {
            lastScale = 1.0;
            return;
        }
        
        CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
        
        CGAffineTransform newTransform = CGAffineTransformScale(sender.view.transform, scale, scale);
        
        [sender.view setTransform:newTransform];
        lastScale = [sender scale];
    }
    
    //手指旋转事件
    -(void)handleRotateGesture:(UIRotationGestureRecognizer *)sender{
        NSLog(@"旋转");
        
        NSLog(@"sender.scale=%f",sender.rotation);
        NSLog(@"sender.velocity=%f",sender.velocity);
    
        sender.view.transform=CGAffineTransformRotate(sender.view.transform, sender.rotation);
        sender.rotation=0;
    }
    
    //手指拖事件
    -(void)handlePanGesture:(UIPanGestureRecognizer *)sender{
        NSLog(@"");
        NSLog(@"sender=%@",sender);
        CGPoint translation=[(UIPanGestureRecognizer*)sender translationInView:self.view];
        
        //开始
        if (sender.state==UIGestureRecognizerStateBegan) {
            x=sender.view.center.x;
            y=sender.view.center.y;
        }
        //进行中
        if (sender.state==UIGestureRecognizerStateChanged) {
            sender.view.center=CGPointMake(x+translation.x, y+translation.y);
        }
        //结束
        if (sender.state==UIGestureRecognizerStateEnded) {
            sender.view.center=CGPointMake(x+translation.x, y+translation.y);
        }
    }
    
    //手指长按事件
    -(void)handleLongpressGesture:(UILongPressGestureRecognizer *)sender{
        NSLog(@"长按");
        if (sender.state==UIGestureRecognizerStateBegan) {
            CABasicAnimation* rotationAnimation_ = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
            //角度转弧度
            rotationAnimation_.fromValue = [NSNumber numberWithFloat:(-0.02)];
            rotationAnimation_.toValue = [NSNumber numberWithFloat:(0.02)];
            //动画时间
            rotationAnimation_.duration = 0.1f;
            //动画次数
            rotationAnimation_.repeatCount=FLT_MAX;
            //回退动画
            rotationAnimation_.autoreverses = YES;
            //动画开始结束的快慢,设置为加速
            rotationAnimation_.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            [[sender.view layer] addAnimation:rotationAnimation_ forKey:@"revItUpAnimation"];
        }
    }
  • 相关阅读:
    Javascript内容整理——BOM
    前端实现excel报表,vue+luckysheet
    代码转图片 的一个好用网站
    推荐一个非常好用前端在线编辑器!!! 一点都不卡
    前端常用的一些网站
    vue中使用moment,如何按需打包?
    better-scroll
    时间线
    碎纸屑动画
    动画
  • 原文地址:https://www.cnblogs.com/741162830qq/p/4620514.html
Copyright © 2011-2022 走看看