博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iphone开发中的手势操作:Swipes
阅读量:5758 次
发布时间:2019-06-18

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

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject]; gestureStartPoint = [touch locationInView:self.view]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject]; CGPoint currentPosition = [touch locationInView:self.view]; CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) {
label.text = @"Horizontal swipe detected"; [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; } else if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance){
label.text = @"Vertical swipe detected"; [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; } }

亦可以使用Automatic Gesture Recognition :(UIGestureRecognizer)

- (void)viewDidLoad {
[super viewDidLoad]; UISwipeGestureRecognizer *vertical = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(reportVerticalSwipe:)]; vertical.direction = UISwipeGestureRecognizerDirectionUp| UISwipeGestureRecognizerDirectionDown; [self.view addGestureRecognizer:vertical]; UISwipeGestureRecognizer *horizontal = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(reportHorizontalSwipe:)]; horizontal.direction = UISwipeGestureRecognizerDirectionLeft| UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:horizontal]; }

然后加上自定义的两个响应方法:

- (void)reportHorizontalSwipe:(UIGestureRecognizer *)recognizer {
label.text = @"Horizontal swipe detected"; [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; } - (void)reportVerticalSwipe:(UIGestureRecognizer *)recognizer {
label.text = @"Vertical swipe detected"; [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; }

就OK啦!

以上的只是单个轻扫动作,下面的是多个轻扫动作同时进行的情况:

在viewDidLoad中添加循环:

- (void)viewDidLoad {
[super viewDidLoad]; for (NSUInteger touchCount = 1; touchCount <= 5; touchCount++) {
UISwipeGestureRecognizer *vertical; vertical = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(reportVerticalSwipe:)]; vertical.direction = UISwipeGestureRecognizerDirectionUp| UISwipeGestureRecognizerDirectionDown; vertical.numberOfTouchesRequired = touchCount; [self.view addGestureRecognizer:vertical]; UISwipeGestureRecognizer *horizontal; horizontal = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(reportHorizontalSwipe:)]; horizontal.direction = UISwipeGestureRecognizerDirectionLeft| UISwipeGestureRecognizerDirectionRight; horizontal.numberOfTouchesRequired = touchCount; [self.view addGestureRecognizer:horizontal]; } }

修改对于响应动作函数:

- (void)reportHorizontalSwipe:(UIGestureRecognizer *)recognizer {
label.text = [NSString stringWithFormat:@"%@Horizontal swipe detected", [self descriptionForTouchCount:[recognizer numberOfTouches]]]; [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; } - (void)reportVerticalSwipe:(UIGestureRecognizer *)recognizer {
label.text = [NSString stringWithFormat:@"%@Vertical swipe detected", [self descriptionForTouchCount:[recognizer numberOfTouches]]];; [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; }
- (NSString *)descriptionForTouchCount:(NSUInteger)touchCount {
switch (touchCount) {
case 2: return @"Double "; case 3: return @"Triple "; case 4: return @"Quadruple "; case 5: return @"Quintuple "; default: return @""; } }

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

你可能感兴趣的文章
mysql 8小时自动断开连接
查看>>
基于gensim深入自然语言处理
查看>>
【CCF】Z字形扫描
查看>>
八数码问题_启发搜索
查看>>
Struts2笔记——struts.xml配置详解
查看>>
发一个 ExtJS 4.2.0 的页面源码(规则比对公式 的设置页面)
查看>>
Entity Framework Code First添加修改及删除单独实体
查看>>
openstack
查看>>
线程进程间通信机制
查看>>
galera mysql 多主复制启动顺序及命令
查看>>
Glibc辅助运行库 (C RunTime Library): crt0.o,crt1.o,crti.o crtn.o,crtbegin.o crtend.o
查看>>
毛玻璃效果
查看>>
mysql常用命令
查看>>
郑捷《机器学习算法原理与编程实践》学习笔记(第七章 预测技术与哲学)7.1 线性系统的预测...
查看>>
Partial Sum
查看>>
JS prototype 属性
查看>>
中位数性质——数列各个数到中位数的距离和最小
查看>>
排序算法 & 迷宫的深度, 广度优先
查看>>
eclipse里报:An internal error occurred during: "Building workspace". Java heap space(内存溢出)...
查看>>
51Nod 1091:线段的重叠(贪心)
查看>>