美文网首页
IOS 中的手势

IOS 中的手势

作者: 墨凌风起 | 来源:发表于2016-07-15 16:08 被阅读62次

    IOS中有六种手势,分别是点击、长按、轻扫、选转、捏合、拖拽。

    /**************************  点击 **********************************/

    -(void)createTarget01{    

    //打开手势    

    self.firstImage.userInteractionEnabled = YES;    

    //创建手势    

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];    

    //设置属性   

     //1⃣️设置点击几次才能响应    

    tap.numberOfTapsRequired = 1;    

    //2⃣️添加几个手指点击   

     tap.numberOfTouchesRequired = 1;    

    //添加手势识别器到view   

     [self.firstImage addGestureRecognizer:tap];    

    //设置监听   

     [tap addTarget:self action:@selector(tapView)];

    }

    -(void)tapView{     NSLog(@"点击手势有效……");}

    ```

    /**************************  长按 **********************************/

    ```

    -(void)createTarget02{  

     //创建手势识别器    

    UILongPressGestureRecognizer *longprss = [[UILongPressGestureRecognizer alloc]init];    //设置属性    

    //1⃣️设置最少按住几秒   

     longprss.minimumPressDuration = 1;    

    //2⃣️y允许的移动范围    

    longprss.allowableMovement = 50;    

    [self.button addGestureRecognizer:longprss];   

     [longprss addTarget: self action:@selector(longTarget)];}

    -(void)longTarget{    NSLog(@"长按成功……");  }

    ```/*******************  3.轻扫手势  **************************//```

    -(void)createTaget03{        

    //设置清扫向上  

     UISwipeGestureRecognizer *swip1 = [[UISwipeGestureRecognizer alloc]init];    swip1.direction = UISwipeGestureRecognizerDirectionUp;   

     [swip1 addTarget:self action:@selector(up)];  

      [self.firstImage addGestureRecognizer:swip1];   

     //设置清扫向下    UISwipeGestureRecognizer *swip2 = [[UISwipeGestureRecognizer alloc]init];   

     swip2.direction = UISwipeGestureRecognizerDirectionDown;    [swip2 addTarget:self action:@selector(down)];    [self.firstImage addGestureRecognizer:swip2];        

    //设置清扫向左   

     UISwipeGestureRecognizer *swip3 = [[UISwipeGestureRecognizer alloc]init];   

     swip3.direction = UISwipeGestureRecognizerDirectionLeft;    

    [swip3 addTarget:self action:@selector(left)];   

     [self.firstImage addGestureRecognizer:swip3];        

    //设置清扫向右   

     UISwipeGestureRecognizer *swip4 = [[UISwipeGestureRecognizer alloc]init];    

    swip4.direction = UISwipeGestureRecognizerDirectionRight;   

    [swip4 addTarget:self action:@selector(right)];   

    [self.firstImage addGestureRecognizer:swip4];

    }

    -(void)up{    NSLog(@"向上滑扫……");}

    -(void)down{    NSLog(@"向下滑扫……");}

    -(void)left{    NSLog(@"向左滑扫……");}

    -(void)right{    NSLog(@"向右滑扫……");}

    ```/*******************  4.旋转手势  **************************//

    ```//-(void)createTarget04{    

    self.thirdImage.userInteractionEnabled = YES;  

      UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]init];    //设置代理  rotation.delegate = self;

    //添加        [self.thirdImage addGestureRecognizer:rotation];

    [rotation addTarget:self action:@selector(rotationTarget:)];

    }

    -(void)rotationTarget:(UIRotationGestureRecognizer *)gesture{

    NSLog(@"旋转了%f",gesture.rotation);

    //在转入弧度

    self.thirdImage.transform = CGAffineTransformRotate(self.thirdImage.transform,gesture.rotation);

    //将旋转弧度清零,(ps:并不是将弧度清零,而是将当前旋转的角度清零)

    gesture.rotation = 0;

    }

    ```/**************************  捏合 **********************************/

    ```-(void)createTarget05{

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]init];

    //设置代理

    pinch.delegate = self;

    [self.thirdImage addGestureRecognizer:pinch];

    [pinch addTarget:self action:@selector(pinchTarget:)];

    }

    //捏合事件

    -(void)pinchTarget:(UIPinchGestureRecognizer *)pin{

    NSLog(@"捏合事件%f",pin.scale);

    self.thirdImage.transform = CGAffineTransformScale(self.thirdImage.transform, pin.scale, pin.scale);

    }

    ```/**************************  拖拽 **********************************/

    ```-(void)createTarget06{

    //创建拖拽手势

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]init];

    //添加到视图

    [self.thirdImage addGestureRecognizer:pan];

    //添加事件

    [pan addTarget:self action:@selector(panView:)];

    }

    -(void)panView:(UIPanGestureRecognizer *)panG{

    //返回值是以手指按下去的点位原点

    CGPoint point = [panG translationInView:panG.view];

    NSLog(@"拖拽事件 %@",NSStringFromCGPoint(point));

    CGPoint temp = self.thirdImage.center;

    temp.x += point.x;

    temp.y += point.y;

    self.thirdImage.center = temp;

    //记住

    [panG setTranslation:CGPointZero inView:panG.view];

    }

    相关文章

      网友评论

          本文标题:IOS 中的手势

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