IOS--手势控制的使用
手势识别是具有互斥的原则的,比如单击和双击,如果它识别出一种手势,其后的手势将不被识别
// 添加单击的手势UITapGestureRecognize
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] init];
[tapGestureRecognizer addTarget:self action:@selector(tapGestureAction:)]; // 添加点击手势的方法
[self.view addGestureRecognizer:tapGestureRecognizer]; // 添加到当前的View上
// 添加双击的手势UITapGestureRecognize
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] init];
tapGestureRecognizer.numberOfTapsRequired = 2; // 设置单击几次才触发方法
[tapGestureRecognizer addTarget:self action:@selector(tapGestureAction:)]; // 添加点击手势的方法
[self.view addGestureRecognizer:tapGestureRecognizer]; // 添加到当前的View上
添加长按的手势UILongPressGestureRecognizer
注意:会调用两次方法,开始长按调用一次 松开后再调用一次 当长按并且滑动的时候,会多次调用长按的方法
UILongPressGestureRecognizer *pressLongGestureRecognizer = [[UILongPressGestureRecognizeralloc] init];
[pressLongGestureRecognizer addTarget:self action:@selector(pressLongGestureAction:)]; // 给长按手势添加方法
[self.view addGestureRecognizer:pressLongGestureRecognizer]; // 添加到当前的View上
添加捏合的手势UIPinchGestureRecognizer
注意:捏合手势不是捏合一次调用一次方法,而是在捏合的过程中不停的调用方法
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] init];
[pinchGestureRecognizer addTarget:self action:@selector(pinchGestureAction:)]; // 添加捏合手势的方法
[self.view addGestureRecognizer:pinchGestureRecognizer]; // 添加到当前的View上
添加旋转的手势UIRotationGestureRecognizer
注意:旋转手势是两指同时进行旋转
UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizeralloc] init];
[rotationGestureRecognizer addTarget:self action:@selector(rotationGestureAction:)]; // 给旋转手势添加方法
[self.view addGestureRecognizer:rotationGestureRecognizer]; // 添加到当前的View上
添加滑动的手势(轻扫手势) UISwipeGestureRecognizer
注意: 快速移动,是用于监测滑动的方向的
UISwipeGestureRecognizer *swipGestureRecognizer = [[UISwipeGestureRecognizer alloc] init];
swipGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp; // 添加手势的方法
// 以下是设置滑动的方向
// typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
// UISwipeGestureRecognizerDirectionRight = 1 << 0, // 从左向右滑动
// UISwipeGestureRecognizerDirectionLeft = 1 << 1, // 从右向左滑动
// UISwipeGestureRecognizerDirectionUp = 1 << 2, // 从下向上滑动
// UISwipeGestureRecognizerDirectionDown = 1 << 3 // 从上向下滑动
// };
[swipGestureRecognizer addTarget:self action:@selector(swipGestureAction:)]; // 给滑动手势添加方法
[self.view addGestureRecognizer:swipGestureRecognizer]; // 添加到当前的View上
添加拖移手势(平移手势) UIPanGestureRecognizer
注意:慢速移动,是用于监测偏移的量的
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] init];
[panGestureRecognizer addTarget:self action:@selector(panGestureAction:)]; // 添加托移收拾的方法
[self.view addGestureRecognizer:panGestureRecognizer]; // 添加到当前的View
[panGestureRecognizer release], panGestureRecognizer = nil; // 释放内存
#pragma mark - 实现单击手势的方法
- (void)tapGestureAction:(UITapGestureRecognizer *) sender {
NSLog(@"您 轻拍 了屏幕");
}
#pragma mark - 实现长按手势的方法
- (void)pressLongGestureAction:(UILongPressGestureRecognizer *) sender {
NSLog(@"您 长按 了屏幕");
}
#pragma mark - 实现了捏合手势的方法
- (void)pinchGestureAction:(UIPinchGestureRecognizer *) sender {
NSLog(@"您 捏合 了屏幕");
}
#pragma mark - 实现旋转手势的方法
- (void)rotationGestureAction:(UIRotationGestureRecognizer *) sender {
NSLog(@"您使用了 旋转 手势");
}
#pragma mark - 实现滑动手势的方法
- (void)swipGestureAction:(UISwipeGestureRecognizer *) sender {
NSLog(@"您 滑动 了屏幕");
}
#pragma mark - 实现了托移手势的方法
- (void)panGestureAction:(UIPanGestureRecognizer *) sender {
NSLog(@"您 托移 了。。。。");
}
晃动手势
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
application.applicationSupportsShakeToEdit = YES;
return YES;
}
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake) {
NSLog(@"检测到晃动");
}
}
最新文章
- iOS---初识Swift(一)
- js限制input标签中只能输入中文
- 论github客户端的使用与团队协作
- 老项目的#iPhone6与iPhone6Plus适配#LaunchImage适配
- Thinkphp中自己组合的数据怎样使用框架的分页
- 关于FileStream读取大文件问题
- POJ C++程序设计 编程题#1 大整数的加减乘除
- android开发系列之gradle认识
- BZOJ1037: [ZJOI2008]生日聚会Party
- Eclipse超级完美汉化教程
- linux下利用openssl来实现证书的颁发(详细步骤)
- The mmap module
- update和saveOrUpdate具体解释
- MTK 快速开机 技术详解
- 数据结构之ConcurrentHashMap
- Java学习笔记四:三目运算符与字符串连接符等
- python中的顺序表
- 解决css设置背景透明,文字不透明
- vc++读取文件属性的详细信息描述 通过读取QQ的注册表和EXE路径两种方式
- 一个小工具 TcpTextListener
热门文章
- psd切图
- 解决方法 test: database removal failed: ERROR: database ";test"; is being accessed by other users
- Java 中 Comparable 和 Comparator 比较
- 用Js+css3实现图片旋转,缩放,裁剪,滤镜
- jq 实现发送验证码倒计时功能
- MySQL 死锁问题分析
- Python内置的字符串处理函数整理
- 【转】pycharm快捷键、常用设置、包管理
- three.js
- Jenkins+Jmeter+Ant 接口持续集成(转)