注意点 : 手势的优先级比较高,如果给tableView添加手势,那么会造成tableView无法滚动,是不是很慌,不用怕,只需要给手势设置代理并且让代理实现代理方法就行
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
一.轻扫手势
- 轻扫手势:
UISwipeGestureRecognizer
- 轻扫手势是有方向的:
UISwipeGestureRecognizerDirection
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
};
####手势的创建
```objc
// 默认方向是往右
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
- 默认状态下方向是从左向右:
UISwipeGestureRecognizerDirectionRight
- 如果想给一个View添加多个轻扫手势,必须创建多个轻扫手势,并且指定手势的方向,一个手势只能有一个方向;但是默认情况下,是只能接收一个手势的,所以我们必须设置手势的代理,实现代理方法,允许View接收多个手势,如下
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionUp;
swipe.delegate = self;
[self.tableView addGestureRecognizer:swipe];
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
swipeDown.delegate = self;
[self.tableView addGestureRecognizer:swipeDown];
- 实现代理方法,接收多个手势
// 是否允许支持多个手势,默认是不支持:NO
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- 实现手势的方法
- (void)swipe:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(@"向下滚动");
[UIView animateWithDuration:0.25 animations:^{
self.navigationController.navigationBar.znb_y = 20;
}];
}else if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(@"上滚");
[UIView animateWithDuration:0.25 animations:^{
self.navigationController.navigationBar.znb_y = - 64;
}];
}
}
二.长按手势
注意点:长按手势是有状态的 默认会触发两次
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible,
UIGestureRecognizerStateBegan,
UIGestureRecognizerStateChanged,
UIGestureRecognizerStateEnded,
UIGestureRecognizerStateCancelled,
UIGestureRecognizerStateFailed,
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
};
手势的创建
#pragma mark - 添加长按手势
- (void)setupLongPress {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
// 设置代理,实现代理方法,让tableView可以就收手势实现手势的方法
longPress.delegate = self;
[self.tableView addGestureRecognizer:longPress];
}
// 实现长按手势的方法
- (void)longPress:(UILongPressGestureRecognizer *)longPress {
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"UIGestureRecognizerStateBegan");
}else if (longPress.state == UIGestureRecognizerStateEnded) {
NSLog(@"UIGestureRecognizerStateEnded");
// self.navigationController.navigationBarHidden = !self.navigationController.navigationBarHidden;
[UIView animateWithDuration:0.25 animations:^{
self.navigationController.navigationBar.znb_y = self.navigationController.navigationBar.znb_y == 0 ? - 64 : 20;
}];
}
}```
##三.旋转手势
>注意点:旋转手势中,旋转角度都是相对于最开始的位置的,结合例子,加深理解
####创建手势 and 实现手势方法
```objc
#pragma mark - 旋转手势
- (void)setUpRotation
{
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[self.imageView addGestureRecognizer:rotation];
}
// 默认传递的旋转的角度都是相对于最开始的位置
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
// 相对于上次的角度 CGAffineTransformRotate
// 相对于最开始的位置的旋转角度 CGAffineTransformMakeRotation
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
// 复位
rotation.rotation = 0;
// 获取手势旋转的角度
NSLog(@"%f",rotation.rotation);
}
四.捏合手势
创建手势 and 实现手势方法
#pragma mark - 捏合
- (void)setUpPinch
{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
pinch.delegate = self;
[self.imageView addGestureRecognizer:pinch];
}
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
// 复位
pinch.scale = 1;
}
五.拖拽手势
创建手势 and 实现手势方法
#pragma mark - 拖拽
- (void)setUpPan
{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.imageView addGestureRecognizer:pan];
}
- (void)pan:(UIPanGestureRecognizer *)pan
{
// 获取手势的触摸点
// CGPoint curP = [pan locationInView:self.imageView];
// 移动视图
// 获取手势的移动,也是相对于最开始的位置
CGPoint transP = [pan translationInView:self.imageView];
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);
// 复位
[pan setTranslation:CGPointZero inView:self.imageView];
// NSLog(@"%@",NSStringFromCGPoint(curP));
}
网友评论