学习iOS开发的第19天
2014-03-31 20:16
537 查看
学了一下手势识别器的运用。下面简单地演示一下。
手势包括轻击、捏合、平移、轻扫、旋转、长按等。它们分别由相应类来实现。依次为UITapGestureRecognizer、UIPinchGestureRecognizer、UIPanGestureRecognizer、UISwipeGestureRecognizer、UIRotationGestureRecognizer、UILongPressGestureRecognizer。它们都继承于UIGestureRecognizer类。创建这些类的实例来获取响应的手势事件并会绑定制定的方法,手势会触发相应的方法。创建好手势识别器后,可以将手势识别器添加到view上面。
新建一个项目,然后创建一个继承于UIViewController的视图控制器,在里面分别创建那几个手势识别器。手势识别器也有很多属性可以设置,有兴趣者可以深入研究下。
最后在应用程序的代理类里创建视图控制器。
运行程序,在屏幕上做出各种手势,会输出相应手势的名称。
手势包括轻击、捏合、平移、轻扫、旋转、长按等。它们分别由相应类来实现。依次为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;
运行程序,在屏幕上做出各种手势,会输出相应手势的名称。
相关文章推荐
- iOS开发学习笔记 1-0 开篇
- ios开发网络学习五:MiMEType ,多线程下载文件思路,文件的压缩和解压缩
- 基于Cocos2D的IOS游戏开发进阶与实践 —— Stage 2 —— 学习笔记
- 学习ios开发的网站
- ios开发网络学习九:NSURLSessionDownloadTask实现大文件下载
- 【iOS开发-33】学习手动内存管理暂时抛弃ARC以及retain/assign知识——iOS工程师面试必考内容
- iOS开发学习3:ViewController
- IOS 开发学习(2): XCode4 开发环境学习
- IOS开发基础学习网址引用
- iOS开发学习之C语言---C05 函数-3
- iOS开发学习之C语言---C10 函数指针-3
- iOS 开发学习之 User Interface(6)UITextField 与 UIImageView
- ios蓝牙开发学习笔记(五)与peripheral通信的最佳实践
- iOS开发学习之UIWindow
- ios开发学习--动画(Animation)效果源码分享--系列教程2
- IOS 学习笔记17 iPhone开发之使用NSUserDefaults存储数据
- iOS 开发学习 使用UIScrollView
- 逻辑运算符-----IOS开发学习记录第8天之C语言学习
- iOS开发学习心得(2)----关于 iPhone 导航控制器 (UINavigationController)
- 2011斯坦福大学iOS应用开发教程学习笔记(第三课)Objective-C