IOS手势
UIKit中包含了UIGestureRecognizer类,用于检测发生在设备中的手势。UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有下面一些子类用于处理具体的手势:
1、拍击UITapGestureRecognizer (任意次数的拍击)
2、向里或向外捏UIPinchGestureRecognizer (用于缩放)
3、摇动或者拖拽UIPanGestureRecognizer
4、擦碰UISwipeGestureRecognizer (以任意方向)
5、旋转UIRotationGestureRecognizer (手指朝相反方向移动)
6、长按UILongPressGestureRecognizer
对于不同类型的手势识别器,具有不同的配置属性。比如UITapGestureRecognizer,可以配置拍击次数。界面接收到手势之后,可以发送一个消息,用于处理响应手势动作后的任务。当然,不同的手势识别器,发送的消息方法也会有所不同。
[代码] 一个手指,拍击两次手势
01
// 创建一个手势识别器
02
UITapGestureRecognizer *oneFingerTwoTaps =
03
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];
04
05
// Set required taps and number of touches
06
[oneFingerTwoTaps setNumberOfTapsRequired:2];
07
[oneFingerTwoTaps setNumberOfTouchesRequired:1];
08
09
// Add the gesture to the view
10
[[self view] addGestureRecognizer:oneFingerTwoTaps];
11
12
消息方法oneFingerTwoTaps
13
- (void)oneFingerTwoTaps
14
{
15
NSLog(@"Action: One finger, two taps");
16
}
[代码] 两个手指,拍击两次手势
01
UITapGestureRecognizer *twoFingersTwoTaps =
02
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease];
03
[twoFingersTwoTaps setNumberOfTapsRequired:2];
04
[twoFingersTwoTaps setNumberOfTouchesRequired:2];
05
[[self view] addGestureRecognizer:twoFingersTwoTaps];
06
07
消息方法twoFingersTwoTaps
08
- (void)twoFingersTwoTaps {
09
NSLog(@"Action: Two fingers, two taps");
10
}
[代码] 一个手指向上、向下擦碰手势
01
// 向上擦碰
02
UISwipeGestureRecognizer *oneFingerSwipeUp =
03
[[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease];
04
[oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
05
[[self view] addGestureRecognizer:oneFingerSwipeUp];
06
07
- (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer
08
{
09
CGPoint point = [recognizer locationInView:[self view]];
10
NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);
11
}
12
13
// 向下擦碰
14
UISwipeGestureRecognizer *oneFingerSwipeDown =
15
[[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeDown:)] autorelease];
16
[oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
17
[[self view] addGestureRecognizer:oneFingerSwipeDown];
18
19
- (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer
20
{
21
CGPoint point = [recognizer locationInView:[self view]];
22
NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);
23
}
[代码] 旋转手势
1
UIRotationGestureRecognizer *twoFingersRotate =
2
[[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)] autorelease];
3
[[self view] addGestureRecognizer:twoFingersRotate];
4
5
- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer
6
{
7
// Convert the radian value to show the degree of rotation
8
NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));
9
}
[代码] 向里或向外捏的手势
1
UIPinchGestureRecognizer *twoFingerPinch =
2
[[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];
3
[[self view] addGestureRecognizer:twoFingerPinch];
4
5
- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
6
{
7
NSLog(@"Pinch scale: %f", recognizer.scale);
8
}
补充:移动开发 , IOS ,