iOS 开发笔记(一)——iOS Gesture Recognizer 手势识别
2012-04-01 14:37
525 查看
在iOS 3.2之后 UIKit中添加了 UIGestureRecognizer Class 可以用来处理手势信息
拍击 (任意次数的拍击)
向里或向外捏 (用于缩放)
摇动或者拖拽
擦碰 (以任意方向)
旋转 (手指朝相反方向移动)
长按
实现代码:
1、一个手指,拍击两次手势
// 创建一个手势识别器
UITapGestureRecognizer *oneFingerTwoTaps =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];
// Set required taps and number of touches
[oneFingerTwoTaps setNumberOfTapsRequired:2];
[oneFingerTwoTaps setNumberOfTouchesRequired:1];
// Add the gesture to the view
[[self view] addGestureRecognizer:oneFingerTwoTaps];
消息方法oneFingerTwoTaps
- (void)oneFingerTwoTaps
{
NSLog(@"Action: One finger, two taps");
}
2、两个手指,拍击两次手势
UITapGestureRecognizer *twoFingersTwoTaps =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease];
[twoFingersTwoTaps setNumberOfTapsRequired:2];
[twoFingersTwoTaps setNumberOfTouchesRequired:2];
[[self view] addGestureRecognizer:twoFingersTwoTaps];
消息方法twoFingersTwoTaps
- (void)twoFingersTwoTaps {
NSLog(@"Action: Two fingers, two taps");
}
3、一个手指向上、向下擦碰手势
// 向上擦碰
UISwipeGestureRecognizer *oneFingerSwipeUp =
[[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease];
[oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
[[self view] addGestureRecognizer:oneFingerSwipeUp];
- (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:[self view]];
NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);
}
// 向下擦碰
UISwipeGestureRecognizer *oneFingerSwipeDown =
[[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeDown:)] autorelease];
[oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
[[self view] addGestureRecognizer:oneFingerSwipeDown];
- (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:[self view]];
NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);
}
4、旋转手势
UIRotationGestureRecognizer *twoFingersRotate =
[[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)] autorelease];
[[self view] addGestureRecognizer:twoFingersRotate];
- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer
{
// Convert the radian value to show the degree of rotation
NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));
}
5、向里或向外捏的手势
UIPinchGestureRecognizer *twoFingerPinch =
[[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];
[[self view] addGestureRecognizer:twoFingerPinch];
- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{
NSLog(@"Pinch scale: %f", recognizer.scale);
}
高级手势识别器
参考资料:
1 根据 Event Handling Guide for iOS http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizers/GestureRecognizers.html 2 子龙山人的博客 IOS 5手势识别教程 http://www.cnblogs.com/andyque/archive/2011/12/30/2307060.html 3 http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more
拍击 (任意次数的拍击)
向里或向外捏 (用于缩放)
摇动或者拖拽
擦碰 (以任意方向)
旋转 (手指朝相反方向移动)
长按
实现代码:
1、一个手指,拍击两次手势
// 创建一个手势识别器
UITapGestureRecognizer *oneFingerTwoTaps =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];
// Set required taps and number of touches
[oneFingerTwoTaps setNumberOfTapsRequired:2];
[oneFingerTwoTaps setNumberOfTouchesRequired:1];
// Add the gesture to the view
[[self view] addGestureRecognizer:oneFingerTwoTaps];
消息方法oneFingerTwoTaps
- (void)oneFingerTwoTaps
{
NSLog(@"Action: One finger, two taps");
}
2、两个手指,拍击两次手势
UITapGestureRecognizer *twoFingersTwoTaps =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease];
[twoFingersTwoTaps setNumberOfTapsRequired:2];
[twoFingersTwoTaps setNumberOfTouchesRequired:2];
[[self view] addGestureRecognizer:twoFingersTwoTaps];
消息方法twoFingersTwoTaps
- (void)twoFingersTwoTaps {
NSLog(@"Action: Two fingers, two taps");
}
3、一个手指向上、向下擦碰手势
// 向上擦碰
UISwipeGestureRecognizer *oneFingerSwipeUp =
[[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease];
[oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
[[self view] addGestureRecognizer:oneFingerSwipeUp];
- (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:[self view]];
NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);
}
// 向下擦碰
UISwipeGestureRecognizer *oneFingerSwipeDown =
[[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeDown:)] autorelease];
[oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
[[self view] addGestureRecognizer:oneFingerSwipeDown];
- (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:[self view]];
NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);
}
4、旋转手势
UIRotationGestureRecognizer *twoFingersRotate =
[[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)] autorelease];
[[self view] addGestureRecognizer:twoFingersRotate];
- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer
{
// Convert the radian value to show the degree of rotation
NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));
}
5、向里或向外捏的手势
UIPinchGestureRecognizer *twoFingerPinch =
[[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];
[[self view] addGestureRecognizer:twoFingerPinch];
- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{
NSLog(@"Pinch scale: %f", recognizer.scale);
}
高级手势识别器
Simultaneous Gesture Recognizers
Custom UIGestureRecognizer
继承于UIGestureRecognizer, 在一下方法中修改:- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch * touch = [touches anyObject]; self.curTickleStart = [touch locationInView:self.view]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { } - (void)reset { } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self reset]; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { [self reset]; }
参考资料:
1 根据 Event Handling Guide for iOS http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizers/GestureRecognizers.html 2 子龙山人的博客 IOS 5手势识别教程 http://www.cnblogs.com/andyque/archive/2011/12/30/2307060.html 3 http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more
相关文章推荐
- iOS开发笔记--iphone练习之手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognize
- iOS开发-UISwipeGestureRecognizer滑动手势
- 2011斯坦福大学iOS应用开发教程学习笔记(第五课)Protocols,手势识别
- 2011斯坦福大学iOS应用开发教程学习笔记(第五课)Protocols,手势识别
- iOS中添加UITapGestureRecognizer手势识别后,UITableView的didSelectRowAtIndexPath失效
- IOS初级教程2:UITapGestureRecognizer手势识别的简单使用
- iOS中添加UITapGestureRecognizer手势识别后,UITableView的didSelectRowAtIndexPath失效
- iOS中添加UITapGestureRecognizer手势识别后,UITableView的didSelectRowAtIndexPath失效
- iOS中添加UITapGestureRecognizer手势识别后,UITableView的didSelectRowAtIndexPath失效
- IOS开发:添加了手势UITapGestureRecognizer,但是点击却没任何反应
- iOS中添加UITapGestureRecognizer手势识别后,UITableView的didSelectRowAtIndexPath失效
- iPhone/iPad高级应用与手游开发学习笔记:多点触摸与手势检测(三:UIPinchGestureRecognizer和UIRotationGestureRecognizer)
- IOS高级教程3:UITapGestureRecognizer手势识别的简单使用到精通
- iOS中添加UITapGestureRecognizer手势识别后,UITableView的didSelectRowAtIndexPath失效
- iOS中添加UITapGestureRecognizer手势识别后,UITableView的didSelectRowAtIndexPath失效 或者点击UITableView的空白处收起键盘
- iOS中添加UITapGestureRecognizer手势识别后,UITableView的didSelectRowAtIndexPath失效
- 2011斯坦福大学iOS应用开发教程学习笔记(第五课)Protocols,手势识别
- ios学习之UISwipeGestureRecognizer手势识别
- iPhone/iPad高级应用与手游开发学习笔记:多点触摸与手势检测(一:UITapGestureRecognizer)
- iOS中添加UITapGestureRecognizer手势识别后,UITableView的didSelectRowAtIndexPath失效