iOS 手势详解

//

//  ViewController.m

//  手势识别

//

//  Created by 谢泽锋 on 15/4/2.

//  Copyright (c) 2015年 谢泽锋. All rights reserved.

//

 

#import "ViewController.h"

#define imageFrame CGRectMake(22, 22, 222, 222)

@interface ViewController ()

{

    

    UIImageView * imageview ;

    float rotation;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

     imageview =[[UIImageView alloc]initWithFrame:imageFrame];

    imageview.tag=10;

    imageview.image=[UIImage imageNamed:@"1.jpg"];

    [imageview setUserInteractionEnabled:YES];

    [self.view addSubview:imageview];

//    UITapGestureRecognizer * tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];

//    

//    

//    [tap setNumberOfTapsRequired:1];//双击

//    [tap setNumberOfTouchesRequired:1];//几根手指

//    [imageview addGestureRecognizer:tap];//添加手势

//    

//    //2长按

//    

//    UILongPressGestureRecognizer * longPress =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];

//    

//    [imageview addGestureRecognizer:longPress];

//    

    //3拖动手势

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

    [imageview addGestureRecognizer:pan];

    

//

//    //4 旋转

    

    UIRotationGestureRecognizer * rotation =[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];

    [imageview addGestureRecognizer:rotation];

//

    //5 捏合手势

    UIPinchGestureRecognizer * pinch =[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(Pinch:)];

    [imageview addGestureRecognizer:pinch];

    //6 清扫手势

//    UISwipeGestureRecognizer * swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];

//    

//    [swipe setDirection:UISwipeGestureRecognizerDirectionRight];//手势方向

//    [self.view addGestureRecognizer:swipe];

    

    // Do any additional setup after loading the view, typically from a nib.

}

-(void)swipe:(UISwipeGestureRecognizer*)recognier

{

    //手指在屏幕拖动 ,扫地手势区间 手指离开屏幕时触发监听方法

    CGRect frame =imageFrame;

    if (UISwipeGestureRecognizerDirectionRight==recognier.state) {

//    UIImageView * imageview=(UIImageView*)[self.view viewWithTag:10];

       frame.origin.x+=111;

        imageview.frame=frame;

    }

    

}

#pragma mark 捏合手势

-(void)Pinch:(UIPinchGestureRecognizer*)recognizer

{

    if (UIGestureRecognizerStateChanged==recognizer.state) {

        

//        [recognizer.view setTransform:CGAffineTransformMakeScale(recognizer.scale, recognizer.scale)];

 

        recognizer.view.transform=CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);

        recognizer.scale=1;

    }

 

}

#pragma mark 旋转手势 至少两根手指着

-(void)rotation:(UIRotationGestureRecognizer*)recognizer

{

    if(UIGestureRecognizerStateBegan==recognizer.state){

//        recognizer.view.transform=CGAffineTransformMakeRotation(rotation);

        NSLog(@"开始 ===%d",recognizer.state);

    }

    if (UIGestureRecognizerStateChanged==recognizer.state) {

        //变化过程中旋转

        //结束时恢复

    //1.

//        [recognizer.view setTransform:CGAffineTransformMakeRotation(recognizer.rotation)];

//        rotation+=recognizer.rotation;

//        NSLog(@"旋转中%f",recognizer.rotation);

//        NSLog(@"rotation =======%f",rotation);

    //2.

        [imageview setTransform:CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation)];//累加的形变 应该把手势识别的rotation设置为0; 下次触发是以当前旋转角度为基础

        recognizer.rotation=0;

    }else if(UIGestureRecognizerStateEnded==recognizer.state)

    {

//        [recognizer.view setTransform:CGAffineTransformIdentity];

    }

    

}

#pragma mark 拖动手势

-(void)pan:(UIPanGestureRecognizer*)recognizer

{

    /*

     拖动结束后

     */

    

    

    CGPoint point = [recognizer translationInView:self.view];

    

    NSLog(@"X:%f;Y:%f",point.x,point.y);

    

    

    

    recognizer.view.center = CGPointMake(recognizer.view.center.x + point.x, recognizer.view.center.y + point.y);

    

    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

    

    

    

//    //UIGestureRecognizerStateChanged 会不断被调用

//    if (UIGestureRecognizerStateChanged==recognizer.state) {

////        CGPoint location =[recognizer locationInView:self.view];

////        [recognizer.view setCenter:location];

//        CGPoint point = [recognizer translationInView:self.view];

////        [recognizer.view setTransform:CGAffineTransformMakeTranslation(point.x, point.y)];

//        [recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, point.x, point.y)];

//      

////        CGRect targetrect =imageFrame;

////        targetrect.origin.x+=point.x;

////        targetrect.origin.y+=point.y;

////        [recognizer.view setFrame:targetrect];

//    }

//    else if (UIGestureRecognizerStateEnded==recognizer.state)

//    {

//        [UIView animateWithDuration:1 animations:^{

//            [recognizer.view setFrame:imageFrame];

//        }];

//    }

}

#pragma mark 长按

-(void)longPress:(UILongPressGestureRecognizer*)recognizer

{

    if(UIGestureRecognizerStateBegan==recognizer.state)

    {

        NSLog(@"长按");

         //长按手势 连续手势

    [UIView animateWithDuration:1 animations:^{

        [recognizer.view setTransform:CGAffineTransformMakeRotation(M_PI)];

        

    }completion:^(BOOL finished) {

        //将视图的形变复原(平移 缩放 旋转)

        [recognizer.view setTransform:CGAffineTransformIdentity];

    }];

    }else if(UIGestureRecognizerStateChanged==recognizer.state)

    {

//        [UIView animateWithDuration:1 animations:^{

//            [recognizer.view setTransform:CGAffineTransformMakeRotation(M_PI)];

//            

//        }completion:^(BOOL finished) {

//            //将视图的形变复原(平移 缩放 旋转)

//            [recognizer.view setTransform:CGAffineTransformIdentity];

//        }];

    }

   

    

}

-(void)tap:(UITapGestureRecognizer*)recognizer

{

    NSLog(@"点我了");

    CGRect initFrame =recognizer.view.frame;

    CGRect targeetFrame=recognizer.view.frame;

    targeetFrame.origin.y+=320;

    [UIView animateWithDuration:1 animations:^{

        [UIView setAnimationRepeatCount:2];

        [UIView setAnimationRepeatAutoreverses:YES];

         [recognizer.view setFrame:targeetFrame];

    }completion:^(BOOL finished) {

        [UIView animateWithDuration:1 animations:^{

           [recognizer.view setFrame:initFrame];

        }];

    

    }];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。