UIScrollView自带两种手,分别为:UIPanGestureRecognizer和UIPinchGestureRecognizer
我们可以通过KVO来监听手势,首先需要让scrollview添加监听
[imagescrollView addObserver:self forKeyPath:@"panGestureRecognizer.state" options:NSKeyValueObservingOptionNew context:nil];
下面是具体的监听事件
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{ if (imagescrollView.panGestureRecognizer.state == UIGestureRecognizerStateBegan) { NSLog(@"开始"); }
if (imagescrollView.panGestureRecognizer.state == UIGestureRecognizerStateChanged) { NSLog(@"改变"); }
if (imagescrollView.panGestureRecognizer.state == UIGestureRecognizerStateEnded) {
NSLog(@"结束");
}
}
如果想检测scrollview的左右滑动,可以给scrollview添加两个手势
left.delegate = self;
left.direction = UISwipeGestureRecognizerDirectionLeft;
[imagescrollView addGestureRecognizer:left];```
UISwipeGestureRecognizer * right = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightSwip)];
right.delegate = self;
right.direction = UISwipeGestureRecognizerDirectionRight;
[imagescrollView addGestureRecognizer:right];```
最后再加上下面的方法即可
`- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}`