iphone开发中的手势操作:Swipes

简介:
复制代码
- (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 @"";
}
}
复制代码

本文转自老Zhan博客园博客,原文链接:http://www.cnblogs.com/mybkn/archive/2012/03/26/2417446.html,如需转载请自行联系原作者

相关文章
|
2月前
|
数据采集 iOS开发 Python
Chatgpt教你开发iPhone风格计算器,Python代码实现
Chatgpt教你开发iPhone风格计算器,Python代码实现
|
Shell iOS开发
iOS逆向:tweak开发教程(iPhone/tool)
iOS逆向:tweak开发教程(iPhone/tool)
1022 0
iOS逆向:tweak开发教程(iPhone/tool)
|
编解码 iOS开发
iphone 开发的基本入门知识
iphone 开发的基本入门知识
210 0
「镁客早报」iPhone或将在今年采用三摄;传Facebook致力于开发语音助力服务与亚马逊、苹果竞争
亚马逊向美国Alexa设备推免费音乐服务;视频会议软件开发商Zoom纳斯达克上市。
258 0
|
Web App开发 缓存 开发工具
|
存储 iOS开发 计算机视觉
|
Web App开发 前端开发 JavaScript
|
API iOS开发 编解码
iOS开发UI篇—iPad和iPhone开发的比较
iOS开发UI篇—iPad和iPhone开发的比较 一、iPad简介 1.什么是iPad   一款苹果公司于2010年发布的平板电脑   定位介于苹果的智能手机iPhone和笔记本电脑产品之间   跟iPhone一样,搭载的是iOS操作系统    2.
1046 0