[转载]为UIView添加上下左右滑动操作(切换时)
来源:互联网 发布:电影院票房数据 编辑:程序博客网 时间:2024/05/17 23:56
原文地址:为UIView添加上下左右滑动操作作者:xiongbiao0228
方法一: 在viewDidLoad时为UIView添加操作
- UISwipeGestureRecognizer
*recognizer; - recognizer
= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; - [recognizer
setDirection:(UISwipeGestureRecognizer DirectionRight)]; - [[self
view] addGestureRecognizer:recognizer]; - [recognizer
release]; -
- recognizer
= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; - [recognizer
setDirection:(UISwipeGestureRecognizer DirectionLeft)]; - [[self
view] addGestureRecognizer:recognizer]; - [recognizer
release]; -
- recognizer
= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; - [recognizer
setDirection:(UISwipeGestureRecognizer DirectionUp)]; - [[self
view] addGestureRecognizer:recognizer]; - [recognizer
release]; -
- recognizer
= [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; - [recognizer
setDirection:(UISwipeGestureRecognizer DirectionDown)]; - [[self
view] addGestureRecognizer:recognizer]; - [recognizer
release];
响应操作绑定的事件
- -
( void)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer{ -
//UISwipeGestureRecognizer DirectionLeft UISwipeGestureRecognizer DirectionRight UISwipeGestureRecognizer DirectionUp UISwipeGestureRecognizer DirectionDown -
if (recognizer.direction==UISwipeGestureRecognizer DirectionLeft ) { -
NSLog(@"Left"); -
} -
- }
方法二:
- #define
HORIZ_SWIPE_DRAG_MIN 12 - #define
VERT_SWIPE_DRAG_MAX 4 - CGPoint
startTouchPosition; - NSString
*dirString ; -
- -
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ -
UITouch *touch = touches.anyObject; -
startTouchPosition = [touch locationInView:self.view]; - }
-
- -
(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event - {
-
UITouch *touch = touches.anyObject; -
CGPoint currentTouchPosition = [touch locationInView:self.view]; -
if (fabsf(startTouchPosition.x - currentTouchPosition.x) >= -
HORIZ_SWIPE_DRAG_MIN && -
fabsf(startTouchPosition.y - currentTouchPosition.y) <= -
VERT_SWIPE_DRAG_MAX) -
{ -
// Horizontal Swipe -
if (startTouchPosition.x < currentTouchPosition.x) { -
NSLog(@"from left" ); -
dirString = @"left"; -
} -
else{ -
NSLog(@"from right" ); -
dirString = @"right";} -
} -
else if (fabsf(startTouchPosition.y - currentTouchPosition.y) >= -
HORIZ_SWIPE_DRAG_MIN && -
fabsf(startTouchPosition.x - currentTouchPosition.x) <= -
VERT_SWIPE_DRAG_MAX) -
{ -
// Vertical Swipe -
if (startTouchPosition.y < currentTouchPosition.y) { -
NSLog(@"from bottom" ); -
dirString = @"bottom";} -
else { -
NSLog(@"from top" ); -
dirString = @"top";} -
} else -
{ -
//dirString = @""; -
} - }
-
- -
(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ -
if (dirString "right")== @ { -
NSLog(@"event right" ); -
}else if (dirString "left")== @ { -
NSLog(@"event left" ); -
} - }
- [转载]为UIView添加上下左右滑动操作(切换时)
- 为UIView添加上下左右滑动操作
- 为UIView添加上下左右滑动操作
- 为UIView添加上下左右滑动操作
- AndroidSwipeLayout实现上下左右滑动显示操作按钮
- 使用viewpager嵌套实现上下左右滑动切换图片(IOS双向滚动翻页效果相同)
- 为UIView添加边框
- 使用viewpager嵌套实现上下左右滑动切换图片
- 为UIView添加图片背景
- 为UIView添加点击事件
- 为UIView添加点击事件
- 为UIView添加点击事件
- 添加手势,滑动切换网页
- 添加滑动条操作
- UIView详解(转载)
- UIView操作:插入添加置顶,判断一个视图是否为子视图view
- iOS 多视图UIView左右来回滑动切换效果
- android上下左右滑动
- 《开往天堂的列车》--肖玄 【为7.23特大高铁事故逝去的人们祈福!】
- poj 2828 Buy Tickets
- C#核心概念--装箱和拆箱(什么是装箱和拆箱)
- STMS传输问题
- 移除项目中VSS管理的链接(已解决)
- [转载]为UIView添加上下左右滑动操作(切换时)
- IIS发生意外错误0x8ffe2740
- android的各种*.img 文件
- ZOJ 3479 Chinese Zodiac
- Http协议
- 使用BOOST实现简单的HTTP网页下载
- 多种协议数据包结构
- js清空fckeditor的值。
- [转载]手势做的简单的翻页效果 翻书带背影效果