UIGestureRecognizer 各种手势的判断

UIGestureRecognizer手势的判断

 
#import  <UIKit/UIKit.h>

typedef enum{

    LongPressGestureRecognizer = 0,

    PanGestureRecognizer,

    PinchGestureRecognizer,

    RotationGestureRecognizer,

    SwipeGestureRecognizer,

    TapGestureRecognizer,

    RecognizerTypeCount

}RecognizerType;


@interface FirstViewController : UIViewController <UIGestureRecognizerDelegate>{


}


@end






 

#import "FirstViewController.h"



@implementation FirstViewController


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}



- (void)didReceiveMemoryWarning

{

    // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

    

    // Release any cached data, images, etc. that aren't in use.

}


- (void)performAlert:(NSString *)msg

{

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:msg delegate:nilcancelButtonTitle:@"确定" otherButtonTitles: nil];

    [alertView show];

    [alertView release];

}

- (void)performSwipeGesture:(UISwipeGestureRecognizer *)recognizer

{

    NSLog(@"performSwipeGesture:");

    if(recognizer.state != UIGestureRecognizerStateEnded){

        NSLog(@"not UIGestureRecognizerStateEnded");

    }else {

        [self performAlert:@"轻扫"];

    }

}

- (void)performRotationSwipeGesture:(UIRotationGestureRecognizer *)recognizer

{


    NSLog(@"PerformRotationSwipeGesture:");

    if(recognizer.state != UIGestureRecognizerStateEnded){

        NSLog(@"not UIGestureRecognizerStateEnded");

    }else {

        [self performAlert:@""];

    }

}


- (void)performLongPressGesture:(UILongPressGestureRecognizer *)recognizer

{

    NSLog(@"performLongPressGesture:");

    

    if(recognizer.state != UIGestureRecognizerStateEnded){

        NSLog(@"not UIGestureRecognizerStateEnded");

    }else {

        [self performAlert:@"长按"];

    }

}

- (void)performTapGesture:(UITapGestureRecognizer *)recognizer

{

    NSLog(@"performTapGesture:");

    if(recognizer.state != UIGestureRecognizerStateEnded){

        NSLog(@"not UIGestureRecognizerStateEnded");

    }else {

        [self performAlert:@""];

    }

}

- (void)performPinchGesture:(UIPinchGestureRecognizer *)recognizer

{

    NSLog(@"performPinchGesture:");

    if(recognizer.state != UIGestureRecognizerStateEnded){

        NSLog(@"not UIGestureRecognizerStateEnded");

    }else {

        [self performAlert:@"缩放"];

    }

}

- (void)performPanGesture:(UIPanGestureRecognizer *)recognizer

{

    NSLog(@"performPanGesture:");

    if(recognizer.state == UIGestureRecognizerStateEnded){

        [self performAlert:@""];

    }

}


- (void)view:(UIView *)view addGestureRecognizer:(NSInteger)type 

    delegate:(id<UIGestureRecognizerDelegate>)delegate

{

    UIGestureRecognizer *recognizer = nil;

    SEL action = nil;

    switch (type) {

        case LongPressGestureRecognizer:

            action = @selector(performLongPressGesture:);

            recognizer = [[UILongPressGestureRecognizer allocinitWithTarget:self action:action];

            break;

        case PanGestureRecognizer:

            action = @selector(performPanGesture:);

            recognizer = [[UIPanGestureRecognizer allocinitWithTarget:self action:action];

            break;

        case PinchGestureRecognizer:

            action = @selector(performPinchGesture:);

            recognizer = [[UIPinchGestureRecognizer allocinitWithTarget:self action:action];

            break;

        case RotationGestureRecognizer:

            action = @selector(performRotationSwipeGesture:);

            recognizer = [[UIRotationGestureRecognizer allocinitWithTarget:self action:action];

            break;

        case SwipeGestureRecognizer:

            action = @selector(performSwipeGesture:);

            recognizer = [[UISwipeGestureRecognizer allocinitWithTarget:self action:action];

            break;

        case TapGestureRecognizer:

            action = @selector(performTapGesture:);

            recognizer = [[UITapGestureRecognizer allocinitWithTarget:self action:action];

            ((UITapGestureRecognizer *)recognizer).numberOfTapsRequired = 1;

            break;

        default:

            return;

    }

    recognizer.delegate = delegate;

    [view addGestureRecognizer:recognizer];

    [recognizer release];

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    for (int i = 0; i < RecognizerTypeCount; ++ i) {

        [self view:self.view addGestureRecognizer:i delegate:self];

    }

}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch*)touch

{

    if (touch.view == self.view) {

        return YES;

    }

    return NO;

}


- (void)viewDidUnload

{

    [super viewDidUnload];


    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}



- (void)dealloc

{

    [super dealloc];

 

}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值