使用手势很简单,分为两步:
1
创建手势实例。当创建手势时,指定一个回调方法,当手势开始,改变、或结束时,
回调方法被调用。
2
添加到需要识别的
View 中。每个手势只对应一个 View,当屏幕触摸在 View 的边界
内时,如果手势和预定的一样,那就会回调方法。
ps:一个手势只能对应一个 View,但是一个 View 可以有多个手势。
建议在真机上运行这些手势
,模拟器操作不太方便,可能导致你认为手势失效。
3、Pan 拖动手势:
3
UIImageView *snakeImageView = [[UIImageView alloc] initWithImage:[UIImage
imageNamed:@
"snake.png"
]];
4
snakeImageView.frame = CGRectMake(50, 50, 100, 160);
5
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
6
initWithTarget:self
7
action:@selector(handlePan:)];
8
[snakeImageView addGestureRecognizer:panGestureRecognizer];
9
[self.view setBackgroundColor:[UIColor whiteColor]];
10
[self.view addSubview:snakeImageView];
新建一个
ImageView,然后添加手势
回调方法:
11
- (
void
) handlePan:(UIPanGestureRecognizer*) recognizer
12
{
13
CGPoint translation = [recognizer translationInView:self.view];
14
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
15
recognizer.view.center.y + translation.y);
16
[recognizer setTranslation:CGPointZero inView:self.view];
17
18
}
4、
Pinch
缩放手势
19
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer
alloc]
20
initWithTarget:self
21
action:@selector(handlePinch:)];<p
class
=
"p1"
>[<span
class
=
"s1"
>snakeImageView</span> <span
class
=
"s2"
>addGestureRecognizer</span>:pinchGestureRecognizer];</p>