天天看點

UIGestureRecognizer 手勢操作 iOS

iPhone 中處理手機觸摸屏的手勢操作在3.2版本以前是UIResponder,它是需要程式分辨不同的手勢操作來做出相應的反應!而在3.2以後蘋果公司提供了一種漸變快捷的方法:UIGestureResponder的方法;以下介紹以下UIGestureResponder方法的使用;

  手勢:

          如果使用者需要定義手勢,需要 touchBegain   touchMove   touchEnd     touchCancle 這四個方法,系統重寫這四個類方法,為我們提供了使用者常見的手勢, UIGestureRecognizer 系統所有手勢的父類,  在這父類中為所有手勢定義了, 初始化方法。

       - (instancetype)initWithTarget:(id)target action:(SEL)action;

 手勢的狀态,state

   目前手勢響應的視圖 view

   以及幾個重要的方法

// 目前手勢屏蔽另外的手勢

- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;

//  目前手勢在view上的位置

- (CGPoint)locationInView:(UIView*)view;  

//  目前手勢有多少個手指

- (NSUInteger)numberOfTouches;

系統定義了一些常見的手勢

  UITapGestureRecognizer: 輕拍(單擊或者輕按兩下)

  UIPinchGestureRecognizer: 捏合

 UIPanGestureRecognizer:拖動

 UILongPressGestureRecognizer:  長按

 UISwipeGestureRecognizer:  輕掃

 UIRotationGestureRecognizer: 旋轉

 */

少廢話,直接上代碼

首先我們以對一個view視圖來操作來實作手勢的功能

定義一個視圖

targetview=[[UIView alloc]initWithFrame:CGRectMake(100, 200, 200, 200)];

targetview.backgroundColor=[UIColor cyanColor];

[self.window addSubview:targetview];

//   實作單擊響應事件

    //1.建立手勢執行個體,并且連接配接tapAction來響應手勢

    UITapGestureRecognizer *singletapGestureRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];

//  給手勢添加辨認

   [targetview addGestureRecognizer:singletapGestureRecognizer];

//  實作輕按兩下響應點選事件  doubleTAP 來響應手勢

UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTAP:)];

    設定點選的次數實作輕按兩下

    doubleTap.numberOfTapsRequired=2;

//    單擊入要執行,必須輕按兩下失效,實作互斥

[singletapGestureRecognizer requireGestureRecognizerToFail:doubleTap];

//給視圖添加手勢

 [targetview addGestureRecognizer:doubleTap];

添加捏合手勢 pinchTap:   

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

  [targetview addGestureRecognizer:pinch];

//手勢拖動

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

     // 添加手勢

[targetview addGestureRecognizer:pan];

 //長按添加事件

    UILongPresszGestureRecognizer *longP=

[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longAction:)];

 // 給視圖添加事件

  [targetview addGestureRecognizer:longP];

//    輕掃添加事件                                                                                                                                    

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

    swipe.direction=UISwipeGestureRecognizerDirectionLeft;

  //給視圖添加拖動手勢   

 [targetview addGestureRecognizer:swipe];

  //添加選裝手勢                                    

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

//給視圖添加旋轉的手勢

    [targetview addGestureRecognizer:ratation];

  self.window.backgroundColor = [UIColor whiteColor];

 [self.window makeKeyAndVisible];

    return YES;

}

//旋轉

-(void)rotaA:(UIRotationGestureRecognizer *)sender

{

    sender.view.transform=CGAffineTransformRotate(sender.view.transform, sender.rotation);

}

//輕掃

-(void)swipe:(UISwipeGestureRecognizer *)sender

{

    switch (sender.direction) {

        case UISwipeGestureRecognizerDirectionLeft:

            NSLog(@" 左");

            break;

        case UISwipeGestureRecognizerDirectionRight:

            break;

        case UISwipeGestureRecognizerDirectionDown:

            break;

        case UISwipeGestureRecognizerDirectionUp:

            break;

        default:

            break;

    }

}

-(void)tapAction:(UITapGestureRecognizer *)sender

{

    NSLog(@"單擊");

}

-(void)doubleTAP:(UITapGestureRecognizer *)sender

{

    NSLog(@"輕按兩下");

}

-(void)pinchTap:(UIPinchGestureRecognizer *)sender

{

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

}

-(void)pan:(UIPanGestureRecognizer *)sender

{

    CGPoint point=[sender translationInView:self.window];

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

    [sender setTranslation:CGPointZero inView:self.window ];

}

-(void)longAction:(UILongPressGestureRecognizer *)sender

{

//    根據手勢的狀态

 if (sender.state==UIGestureRecognizerStateEnded) {

//       

 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"長按" message:@"長按" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"button1",@"button2", nil];

//      

  [alert dismissWithClickedButtonIndex:0 animated:YES];

//        UITextField *text=[alert textFieldAtIndex:3];

//        [alert show];

//        UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"登陸" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登陸", nil];

//        [alertview setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];

//        UITextField *text=[alertview textFieldAtIndex:0];

//         UITextField *text1=[alertview textFieldAtIndex:1];

//        [alertview show];

        UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@"title" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"1" otherButtonTitles:@"2",@"3", nil];

        [actionSheet showInView:self.window];     

    }

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    NSLog(@"buttonindex=%ld",buttonIndex);

    switch (buttonIndex) {

        case 0:

            NSLog(@"button");

            break;

        case 1:

            NSLog(@"button2");

            break;

        case 2:

            NSLog(@"取消");

            break;

        case 3:

            NSLog(@"hhh"); 

            break;

        default:

            break;

    }

}

//actionsheet

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    switch (buttonIndex) {

        case 0:

            NSLog(@"button");

            break;

        case 1:

            NSLog(@"button2");

            break;

        case 2:

            NSLog(@"取消");

            break;

        case 3:

            NSLog(@"hhh");          

            break;   

        default:

            break;

    }

}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{

    //手勢取消!

}