1. 程式人生 > >iOS中手勢的新增

iOS中手勢的新增

#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