http://www.cocoachina.com/ios/20140922/9711.html
1.轻击手势(TapGestureRecognizer)的添加
//新建tap手势
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
//设置点击次数和点击手指数
tapGesture.numberOfTapsRequired = 1;
//点击次数
tapGesture.numberOfTouchesRequired = 1;
//点击手指数
[self.view addGestureRecognizer:tapGesture];
2.长按手势(LongPressGestureRecognizer)
初始化代码:
1
2
3
4
5
|
//添加长摁手势
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
//设置长按时间
longPressGesture.minimumPressDuration = 0.5;
//(2秒)
[self.view addGestureRecognizer:longPressGesture];
|
添加轻扫手势,一个向左一个向右,代码如下:
1
2
3
4
5
6
7
8
9
10
11
|
//添加轻扫手势
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
//默认向右
[self.view addGestureRecognizer:swipeGesture];
//添加轻扫手势
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft;
//默认向右
[self.view addGestureRecognizer:swipeGestureLeft];
|
5.拖动手势(PanGestureRecognizer)
拖动手势的初始化
1
2
3
|
//添加拖动手势
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[self.view addGestureRecognizer:panGesture];
|
拖动手势要做的方法(通过translationInView获取移动的点,和TouchesMoved方法类似)
1
2
3
4
5
6
7
8
9
|
//拖动手势
-(void) panGesture:(id)sender
{
UIPanGestureRecognizer *panGesture = sender;
CGPoint movePoint = [panGesture translationInView:self.view];
//做你想做的事儿
}
|