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

本文共 4116 字,大约阅读时间需要 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 @""; } }

转载于:https://www.cnblogs.com/mybkn/archive/2012/03/26/2417446.html

你可能感兴趣的文章
VS添加节点
查看>>
MapReduce之知识整理
查看>>
sed 常用操作纪实
查看>>
C++复习:对C的拓展
查看>>
linux设置静态IP和DNS以及改网卡名
查看>>
连续字符换行 溢出点点点 多行省略
查看>>
nodejs全局变量设置设置
查看>>
二分查找法(递归和非递归算法)
查看>>
C# DevExpress TreeList指定KeyFieldName后无法显示该列的问题
查看>>
多条记录的同一字段组合成一个字符串 FOR XML PATH
查看>>
APUE学习笔记——10.9 信号发送函数kill、 raise、alarm、pause
查看>>
剑指Offer面试题33(java版):把数组排成最小的数
查看>>
jquery中的 $(function(){ .. }) 函数
查看>>
奇怪的国家
查看>>
Linux nohup命令详解
查看>>
[MSDN] Using the Windows Azure Storage Services
查看>>
计算回文数
查看>>
MVC中如何选取后台数据展示下拉列表项
查看>>
java 代理的概念与作用
查看>>
get_free_page 和其友
查看>>