博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIScrollView中的手势
阅读量:6495 次
发布时间:2019-06-24

本文共 6515 字,大约阅读时间需要 21 分钟。

UIScrollView中的手势

UIScrollView自带了两个手势,分别为:

UIPanGestureRecognizer

UIPinchGestureRecognizer

他们都是readonly的.

 

监听UIPanGestureRecognizer

手势是UIPanGestureRecognizer的属性,我们可以使用KVO来进行监听.

#import "RootViewController.h"#define WIDTH    self.view.frame.size.width#define HEIGHT   self.view.frame.size.height@interface RootViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;@end@implementation RootViewController- (void)viewDidLoad{ [super viewDidLoad]; _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)]; _scrollView.contentSize = CGSizeMake(WIDTH, HEIGHT * 3); [self.view addSubview:_scrollView]; [_scrollView addObserver:self forKeyPath:@"panGestureRecognizer.state" options:NSKeyValueObservingOptionNew context:nil];}-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ // 监听pan手势开始 if (_scrollView.panGestureRecognizer.state == UIGestureRecognizerStateBegan) { NSLog(@"UIGestureRecognizerStateBegan"); } // 监听pan手势值改变 if (_scrollView.panGestureRecognizer.state == UIGestureRecognizerStateChanged) { NSLog(@"UIGestureRecognizerStateChanged"); } // 监听pan手势结束 if (_scrollView.panGestureRecognizer.state == UIGestureRecognizerStateEnded) { NSLog(@"UIGestureRecognizerStateEnded"); }}- (void)dealloc{ [_scrollView removeObserver:self forKeyPath:@"panGestureRecognizer.state"];}@end

核心代码如下:

本人在测试的时候发现,一个完整的手势事件,一般情况下,手势值的改变是会执行多次的,而UIScrollview中的pan手势处理过后,手势开始,手势值改变以及手势结束,均只执行一回.

 

添加UISwipeGestureRecognizer手势

我们可以给UIScrollview添加UISwipeGestureRecognizer手势来判断是往哪个方向轻轻滑动了.

#import "RootViewController.h"#import "GestureView.h"@interface RootViewController ()
@end@implementation RootViewController- (void)viewDidLoad{ [super viewDidLoad]; #define WIDTH self.view.frame.size.width#define HEIGHT self.view.frame.size.height UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; scrollView.backgroundColor = [UIColor grayColor]; scrollView.contentSize = CGSizeMake(WIDTH, HEIGHT*3); [self.view addSubview:scrollView]; UISwipeGestureRecognizer *up = \ [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeEvent:)]; up.direction = UISwipeGestureRecognizerDirectionUp; up.delegate = self; [scrollView addGestureRecognizer:up]; UISwipeGestureRecognizer *down = \ [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeEvent:)]; down.direction = UISwipeGestureRecognizerDirectionDown; down.delegate = self; [scrollView addGestureRecognizer:down];}- (void)swipeEvent:(UISwipeGestureRecognizer *)gesture{ NSLog(@"%@", gesture);}- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES;}@end

我直接将手势对象添加进category中,运行时关联上手势对象:

UIScrollView+Swipe.h + UIScrollView+Swipe.m

#import 
typedef enum { E_UP = 0x1000, E_DOWN,} EDirection;@protocol UIScrollViewSwipeProtocol
- (void)swipeDirection:(EDirection)direction;@end@interface UIScrollView (Swipe)@property (nonatomic, assign) id
swipeProtocol;@property (nonatomic, strong, readonly) UISwipeGestureRecognizer *upGesture;@property (nonatomic, strong, readonly) UISwipeGestureRecognizer *downGesture;- (void)activateSwipeGesture;- (void)cancelSwipeGesture;@end
#import "UIScrollView+Swipe.h"#import 
@interface UIScrollView ()
@property (nonatomic, strong, readwrite) UISwipeGestureRecognizer *upGesture;@property (nonatomic, strong, readwrite) UISwipeGestureRecognizer *downGesture;@endstatic char swipeProtocolFlag;static char upGestureFlag;static char downGestureFlag;@implementation UIScrollView (Swipe)- (id
)swipeProtocol{ return objc_getAssociatedObject(self, &swipeProtocolFlag);}- (void)setSwipeProtocol:(id
)swipeProtocol{ objc_setAssociatedObject(self, &swipeProtocolFlag, swipeProtocol, OBJC_ASSOCIATION_ASSIGN);}- (void)setUpGesture:(UISwipeGestureRecognizer *)upGesture{ objc_setAssociatedObject(self, &upGestureFlag, upGesture, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (UISwipeGestureRecognizer *)upGesture{ return objc_getAssociatedObject(self, &upGestureFlag);}- (void)setDownGesture:(UISwipeGestureRecognizer *)downGesture{ objc_setAssociatedObject(self, &downGestureFlag, downGesture, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (UISwipeGestureRecognizer *)downGesture{ return objc_getAssociatedObject(self, &downGestureFlag);}- (void)activateSwipeGesture{ if (self.upGesture == nil) { self.upGesture = \ [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureEvents:)]; self.upGesture.direction = UISwipeGestureRecognizerDirectionUp; self.upGesture.delegate = self; [self addGestureRecognizer:self.upGesture]; } if (self.downGesture == nil) { self.downGesture = \ [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureEvents:)]; self.downGesture.direction = UISwipeGestureRecognizerDirectionDown; self.downGesture.delegate = self; [self addGestureRecognizer:self.downGesture]; }}- (void)swipeGestureEvents:(UISwipeGestureRecognizer *)gesture{ if (self.swipeProtocol) { if (self.downGesture == gesture) { [self.swipeProtocol swipeDirection:E_DOWN]; } if (self.upGesture == gesture) { [self.swipeProtocol swipeDirection:E_UP]; } }}- (void)cancelSwipeGesture{ [self removeGestureRecognizer:self.upGesture]; [self removeGestureRecognizer:self.downGesture]; self.upGesture = nil; self.downGesture = nil;}- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES;}@end

然后这么使用即可:),so easy!

 

 

 

 

 

转载地址:http://cjcyo.baihongyu.com/

你可能感兴趣的文章
字符串变量小议
查看>>
232. Implement Queue using Stacks
查看>>
Poj(1469),二分图最大匹配
查看>>
和菜鸟一起学linux之V4L2摄像头应用流程【转】
查看>>
spin_lock、spin_lock_irq、spin_lock_irqsave区别【转】
查看>>
删除 mac 垃圾桶内清除不掉的文件
查看>>
【响应式编程的思维艺术】 (5)Angular中Rxjs的应用示例
查看>>
/bin/bash^M: bad interpreter: No such file or dire
查看>>
python xml rpc
查看>>
Java设置以及获取JavaBean私有属性进阶
查看>>
db2表结构导出导入,数据库备份
查看>>
策略模式
查看>>
OrderOnline——项目概述
查看>>
POJ-2739(Water)
查看>>
【转】第三节 UNIX文件系统结构
查看>>
为什么sql里面not in后面的子查询如果有记录为NULL的,主查询就查不到记录
查看>>
Angular7里面实现 debounce search
查看>>
Linux 内核链表
查看>>
git学习------>Git 分支管理最佳实践
查看>>
括号和出栈所有序列问题
查看>>