IOS学习 触摸和手势 手势识别器:单击、双击、轻扫、滑动、长按、旋转
2016-03-15 14:12
543 查看
@implementation HomeViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
//单击
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer
alloc]initWithTarget:self
action:@selector(tap:)];
[self.view
addGestureRecognizer:tapGesture];
//双击
UITapGestureRecognizer *doubletap = [[UITapGestureRecognizer
alloc]initWithTarget:self
action:@selector(doubletap:)];
doubletap.numberOfTapsRequired =
2 ;
[self.view
addGestureRecognizer:doubletap];
//区别单击、双击手势
[tapGesture requireGestureRecognizerToFail:doubletap];
//轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer
alloc]initWithTarget:self
action:@selector(swipe:)];
//指定轻扫方向,只能监听一个方向,默认向右轻扫
swipe.direction =
UISwipeGestureRecognizerDirectionUp;
/* UISwipeGestureRecognizerDirectionRight
UISwipeGestureRecognizerDirectionLeft
UISwipeGestureRecognizerDirectionUp
UISwipeGestureRecognizerDirectionDown */
[self.view
addGestureRecognizer:swipe];
//平移,滑动手势
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer
alloc]initWithTarget:self
action:@selector(panGesture:)];
[self.view
addGestureRecognizer:panGesture];
//长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer
alloc]initWithTarget:self
action:@selector(longPress:)];
//长按时间设置,秒
longPress.minimumPressDuration =
3;
[self.view
addGestureRecognizer:longPress];
//旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer
alloc]initWithTarget:self
action:@selector(rotationTap:)];
[self.view
addGestureRecognizer: rotation];
}
//单击事件
-(void)tap:(UITapGestureRecognizer *)tapGesture{
NSLog(@"单击");
}
//双击事件
-(void)doubletap:(UITapGestureRecognizer *)tapGesture{
NSLog(@"双击");
}
//轻扫事件
-(void)swipe:(UISwipeGestureRecognizer *)swipe{
NSLog(@"轻扫");
}
//平移,滑动事件
-(void)panGesture:(UIPanGestureRecognizer *)panGesture{
NSLog(@"平移,滑动");
CGPoint point = [panGesture
locationInView:self.view];
NSLog(@"%@",NSStringFromCGPoint(point));
}
//长按事件
-(void)longPress:(UILongPressGestureRecognizer *)lp{
// NSLog(@"long press: %u",[lp state]); //手势状态
if (lp.state ==
UIGestureRecognizerStateEnded) {
return;
}
NSLog(@"长按");
}
//旋转事件
-(void)rotationTap:(UIRotationGestureRecognizer *)rotation{
NSLog(@"rotation :%f",rotation.rotation);
//旋转弧度
//转换成角度
float degree = rotation.rotation*(180/M_PI);
NSLog(@"degree = %f",degree);
}
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
//单击
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer
alloc]initWithTarget:self
action:@selector(tap:)];
[self.view
addGestureRecognizer:tapGesture];
//双击
UITapGestureRecognizer *doubletap = [[UITapGestureRecognizer
alloc]initWithTarget:self
action:@selector(doubletap:)];
doubletap.numberOfTapsRequired =
2 ;
[self.view
addGestureRecognizer:doubletap];
//区别单击、双击手势
[tapGesture requireGestureRecognizerToFail:doubletap];
//轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer
alloc]initWithTarget:self
action:@selector(swipe:)];
//指定轻扫方向,只能监听一个方向,默认向右轻扫
swipe.direction =
UISwipeGestureRecognizerDirectionUp;
/* UISwipeGestureRecognizerDirectionRight
UISwipeGestureRecognizerDirectionLeft
UISwipeGestureRecognizerDirectionUp
UISwipeGestureRecognizerDirectionDown */
[self.view
addGestureRecognizer:swipe];
//平移,滑动手势
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer
alloc]initWithTarget:self
action:@selector(panGesture:)];
[self.view
addGestureRecognizer:panGesture];
//长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer
alloc]initWithTarget:self
action:@selector(longPress:)];
//长按时间设置,秒
longPress.minimumPressDuration =
3;
[self.view
addGestureRecognizer:longPress];
//旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer
alloc]initWithTarget:self
action:@selector(rotationTap:)];
[self.view
addGestureRecognizer: rotation];
}
//单击事件
-(void)tap:(UITapGestureRecognizer *)tapGesture{
NSLog(@"单击");
}
//双击事件
-(void)doubletap:(UITapGestureRecognizer *)tapGesture{
NSLog(@"双击");
}
//轻扫事件
-(void)swipe:(UISwipeGestureRecognizer *)swipe{
NSLog(@"轻扫");
}
//平移,滑动事件
-(void)panGesture:(UIPanGestureRecognizer *)panGesture{
NSLog(@"平移,滑动");
CGPoint point = [panGesture
locationInView:self.view];
NSLog(@"%@",NSStringFromCGPoint(point));
}
//长按事件
-(void)longPress:(UILongPressGestureRecognizer *)lp{
// NSLog(@"long press: %u",[lp state]); //手势状态
if (lp.state ==
UIGestureRecognizerStateEnded) {
return;
}
NSLog(@"长按");
}
//旋转事件
-(void)rotationTap:(UIRotationGestureRecognizer *)rotation{
NSLog(@"rotation :%f",rotation.rotation);
//旋转弧度
//转换成角度
float degree = rotation.rotation*(180/M_PI);
NSLog(@"degree = %f",degree);
}
相关文章推荐
- iOS7 之后 tableView被navigationBar遮挡 问题详解
- IOS中的多线程
- ios优化的25条建议和技巧
- 【ios】xcode7.2导出ipa文件测试
- ios打包ipa的四种实用方法
- iOS开发中常用但经常忘记的技巧
- iOS蓝牙4.0开发
- iOS学习之block(一)
- iOS - coreData
- iOS 打开系统打电话 发短信 邮件 浏览器 地图
- iOS开发中那些高效常用的宏
- iOS NSRuntime机制
- 利用runtime懒人实现iOS 防止按钮连续点击 连续响应事件,可以自己设置时间间隔,拖进工程中立即生效
- IOS Cocoapods安装及使用
- ios 毛玻璃效果
- iOS开发小技巧
- iOS 友盟分享
- iOS 字典与字符串的相互转换
- iOS Tabbar和Naviagtion
- iOS cell分割线长度设置