iOS疯狂讲解之手势识别器
2015-09-17 21:35
609 查看
#import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor redColor]; // 创建一个imageView UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds]; imageView.image = [UIImage imageNamed:@"u=2473889012,1045156706&fm=21&gp=0.jpg"]; // UIImageView 默认交互是关闭的 ,所以需要打开 imageView.userInteractionEnabled = YES; [self.view addSubview:imageView]; [imageView release]; // 手机识别器这个类 , 是一个抽象类 // 自己本身不实现什么具体功能 //具体功能都是由其子类来实现 // 第一个:轻拍手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTap:)]; // 用 几个手指 轻拍 tap.numberOfTouchesRequired = 2; // 默认是1 // 轻拍的次数 tap.numberOfTapsRequired = 1; // 默认是1 // 把手势添加到要点击轻拍的视图上 [imageView addGestureRecognizer:tap]; [tap release]; /* //第二个: 长按手势 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(actionLongPress:)]; // 最短长按时间 longPress.minimumPressDuration = 1; // 添加到视图上 [imageView addGestureRecognizer:longPress]; [longPress release]; */ /* // 第三个: 旋转手势 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(actionRotation:)]; // 添加到视图上 [imageView addGestureRecognizer:rotation]; [rotation release]; */ /* // 第四个: 捏合手势 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(actionPinch:)]; // 直接添加到视图上 [imageView addGestureRecognizer:pinch]; [pinch release]; */ /* // 第五个 平移手势 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(actionPan:)]; // 添加到视图上 [imageView addGestureRecognizer:pan]; [pan release]; */ // // 第六个 清扫手势 // UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(actionSwipe:)]; // // 设置左右扫 // swipe.direction = UISwipeGestureRecognizerDirectionLeft; // 默认是右扫 // // // 添加到视图上 // [imageView addGestureRecognizer:swipe]; // [swipe release]; // 第七个: 边缘扫 是默认的 不设置也有边缘扫 UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(actionScreenEdgePan:)]; // 设置从屏幕边缘 那边去扫 screenEdgePan.edges = UIRectEdgeLeft; [imageView addGestureRecognizer:screenEdgePan]; [screenEdgePan release]; } //1, 轻拍触发方法 - (void)actionTap:(UITapGestureRecognizer *)tap { NSLog(@"轻拍了"); } // 2,长按触发方法 - (void)actionLongPress:(UILongPressGestureRecognizer *)longPress { // 长按 换图片 // 获取到长按的view UIImageView *imageView = (UIImageView *)longPress.view; imageView.image = [UIImage imageNamed:@"u=3484715933,459413563&fm=23&gp=0.jpg"]; NSLog(@"长按手势实现"); } // 3,旋转触发方法 - (void)actionRotation:(UIRotationGestureRecognizer *)rotation { // transform 形变属性 // 描述一下这个方法 // 第一个参数 传入要创建那个视图的形变属性 // 第二个参数 传入 手势的弧度 rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation); // 把弧度重置 rotation.rotation = 0; NSLog(@"旋转了"); } //4, 捏合触发方法 - (void)actionPinch:(UIPinchGestureRecognizer *)pinch { // 形变属性控制 捏合 // 第二个参数 按捏合的刻度比例 形变 pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale); // 重置刻度比例 pinch.scale = 1; NSLog(@"捏合手势"); } // 5. 平移触发方法 - (void)actionPan:(UIPanGestureRecognizer *)pan { // 这个点 以手指触摸到屏幕那一刻 即为0,0点 CGPoint p = [pan translationInView:pan .view]; NSLog(@"%@", NSStringFromCGPoint(p)); pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y); // 重置 点 让他以为每次都是刚开始触发 [pan setTranslation:CGPointMake(0, 0) inView:pan.view]; //[pan translationInView:self.view]; } // 6. 左右扫触发方法 - (void)actionSwipe:(UISwipeGestureRecognizer *)swipe { NSLog(@"左扫了"); } // 7. 边缘扫触发方法 - (void)actionScreenEdgePan:(UIScreenEdgePanGestureRecognizer *)screenEdgePan { // 刚一扫就触发 if (screenEdgePan.state == UIGestureRecognizerStateBegan) { NSLog(@"边缘扫 左扫"); } }
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器
- IOS开发第三方语音-微信语音
- iOS开发之路--微博OAuth授权_取得用户授权的accessToken
- iOS开发中实现邮件和短信发送的简单示例
- ios通过按钮点击异步加载图片
- 加载带有手势识别器的XIB文件需注意哪些问题