学习iOS开发的第19天
来源:互联网 发布:linux使用ccproxy 编辑:程序博客网 时间:2024/05/01 21:03
学了一下手势识别器的运用。下面简单地演示一下。
手势包括轻击、捏合、平移、轻扫、旋转、长按等。它们分别由相应类来实现。依次为UITapGestureRecognizer、UIPinchGestureRecognizer、UIPanGestureRecognizer、UISwipeGestureRecognizer、UIRotationGestureRecognizer、UILongPressGestureRecognizer。它们都继承于UIGestureRecognizer类。创建这些类的实例来获取响应的手势事件并会绑定制定的方法,手势会触发相应的方法。创建好手势识别器后,可以将手势识别器添加到view上面。
新建一个项目,然后创建一个继承于UIViewController的视图控制器,在里面分别创建那几个手势识别器。手势识别器也有很多属性可以设置,有兴趣者可以深入研究下。
//轻击识别器 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)]; [self.view addGestureRecognizer:tap]; //捏合 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch)]; [self.view addGestureRecognizer:pinch]; //平移 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan)]; [self.view addGestureRecognizer:pan]; //轻扫 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)]; //设置方向 swipe.direction = UISwipeGestureRecognizerDirectionDown; [self.view addGestureRecognizer:swipe]; //旋转 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation)]; [self.view addGestureRecognizer:rotation]; //长按 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress)]; //设置长按最小时间 longPress.minimumPressDuration = 1; [self.view addGestureRecognizer:longPress];下面是各个手势所触发的方法。
-(void)tap{ NSLog(@"轻击");}-(void)pinch{ NSLog(@"捏合");}-(void)pan{ NSLog(@"平移");}-(void)swipe{ NSLog(@"轻扫");}-(void)rotation{ NSLog(@"旋转");}-(void)longPress{ NSLog(@"长按");}
最后在应用程序的代理类里创建视图控制器。
TouchViewController *controller = [[TouchViewController alloc] init]; self.window.rootViewController = controller;
运行程序,在屏幕上做出各种手势,会输出相应手势的名称。
0 0
- 学习iOS开发的第19天
- 学习iOS开发的第1天
- 学习IOS开发的第2天
- 学习iOS开发的第3天
- 学习IOS开发的第4天
- 学习IOS开发的第5天
- 学习IOS开发的第7天
- 学习IOS开发的第9天
- 学习IOS开发的第10天
- 学习IOS开发的第11天
- 学习IOS开发的第12天
- 学习IOS开发的第13天
- 学习IOS开发的第14天
- 学习iOS开发的第15天
- 学习iOS开发的第16天
- 学习iOS开发的第17天
- 学习iOS开发的第18天
- 学习iOS开发的第20天
- Cortex A8,PWM裸机程序
- 第一个所谓的c++接口与实现的分离
- 阿里巴巴2014研发实习生笔试解析
- 利用底层键盘钩子拦载任意按键(回调版)
- [Android]adb学习笔记(一)
- 学习iOS开发的第19天
- 用程序模拟键盘和鼠标键盘
- Android Dialog用法
- 脚下的那块生我养我的土地依然如此亲
- 使用CAS实现SSO
- [Android]adb学习笔记(二)
- SQL plus连接远程Oralce数据库
- 如何截取QQ密码和聊天内容、去掉QQ广告栏、添加QQ尾巴
- 软件项目管理——文档