转自:http://blog.163.com/china_uv/blog/static/117137267201252102612185/
UISwipeGestureRecognizer 左右事件相同为非注释代码, 左右事件不同为注释代码。
@implementation GestureRecognizerViewController
-(id)init
{
if (self == [super init]) {
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
// recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
// [recognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
// [[self view] addGestureRecognizer:recognizer];
// [recognizer release];
//
// recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
// [recognizer setDirection:UISwipeGestureRecognizerDirectionRight];
// [[self view] addGestureRecognizer:recognizer];
// [recognizer release];
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor darkGrayColor];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void) dealloc
{
for (UISwipeGestureRecognizer *recognizer in [[self view] gestureRecognizers]) {
[[self view] removeGestureRecognizer:recognizer];
}
[super dealloc];
}
// ------------------------------------------------------------------------
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
NSLog(@"direction ------------- %d", recognizer.direction);
if (recognizer.direction == (UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)) {
NSLog(@"left & right");
}
// if (UISwipeGestureRecognizerDirectionLeft == recognizer.direction) {
// NSLog(@"left");
// }
//
// if (UISwipeGestureRecognizerDirectionRight == recognizer.direction) {
// NSLog(@"right");
// }
}
感叹下IOS的智慧,UISwipeGestureRecognizerDirection定义用的是位移。
typedef enum {
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
} UISwipeGestureRecognizerDirection;