进击的KFC:UI(五)手势识别器
2015-11-26 10:05
501 查看
一.什么是⼿势识别器?
手势类:UIGestureRecognizer是一个抽象类 , 其具体功能交给子类去实现.
二.⼿势识别器的分类
手势识别器有7个子类:
分别识别:轻拍手势,平移手势,长按手势,轻扫手势,缩放手势,旋转手势,边缘扫手势
一旦指定的手势被识别我们可以执行我们自己定义好的操作
三.如何使⽤识别器
我们不会直接使⽤⼿势识别器这个抽象⽗类,⽽是根据需要使⽤特定的⼿势识别器创建对象。
1、创建UIxxxGestureRecognizer对象,使⽤initWithTarget:action:⽅法;
2、配置要识别的⼿势的相关信息;
3、将⼿势添加到某个视图上;
4、实现⼿势识别器⾥定义的⽅法
(1)轻拍手势:
(2)长按
(3)旋转
(4)捏合
(5)平移
(6)轻扫
(7)边缘扫
手势类:UIGestureRecognizer是一个抽象类 , 其具体功能交给子类去实现.
二.⼿势识别器的分类
手势识别器有7个子类:
分别识别:轻拍手势,平移手势,长按手势,轻扫手势,缩放手势,旋转手势,边缘扫手势
一旦指定的手势被识别我们可以执行我们自己定义好的操作
三.如何使⽤识别器
我们不会直接使⽤⼿势识别器这个抽象⽗类,⽽是根据需要使⽤特定的⼿势识别器创建对象。
1、创建UIxxxGestureRecognizer对象,使⽤initWithTarget:action:⽅法;
2、配置要识别的⼿势的相关信息;
3、将⼿势添加到某个视图上;
4、实现⼿势识别器⾥定义的⽅法
1.创建一个imageView来添加手势 UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds]; imageView.backgroundColor = [UIColor redColor]; [self.view addSubView:imageView]; // 开启imageView的交互 (之前提过UIImageView和UILabel的交互默认是关闭的) imageView.userInteractionEnabled = YES;
(1)轻拍手势:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)]; // 设置需要点击的次数 (默认是点1次) tap.numberOfTapRequired = 2; // 设置几根手指 (默认是一根) tap.numberOfTouchesRequired = 2; //把手势添加到视图上 [imageView addGestureRecognizer:tap]; // 方法实现: - (void)taAction:(UITapGestureRecognizer *)tap { NSLog(@"轻拍"); }
(2)长按
UILongPressGestureRecognizer *longPress = [[UILongPressRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)]; // 设置长按的时间 (默认是0.5秒) longPress.minimumPressDuration = 1; // 设置移动时间 longPress.allowableMovement = 5; [imageView addGesture:longPress]; // 实现方法 - (void)longPressAction:(UILongPressGestureRecognizer *)longPress { // 注意,进入长按是一瞬间的事,进入长按状态后无需在调用次方法 // 所以要对状态进行一次判断 if(longPress.state == UIGestureRecognizerStateBegin){ NSLog(@"长按"); } }
(3)旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)]; [imageView addGestureRecognizer:rotation]; // 实现方法 - (void)rotationAction:(UIRotationGestureRecognizer *)rotation { // 手势.view : 表示取出 被添加了 手势 的 视图 (view是手势类的一个属性) // 形变:transform // 参数1:视图.transform // 参数2:根据弧度去创建; 用旋转手势自带的弧度属性rotation rotation.view.transform = CGAffineTransformRotate(rotation.view.transform,rotation.rotation); // 注意:需要每次 把 旋转角度 重置为0. // 因为要接替上一次的角度 开始旋转 rotation.rotation = 0; NSLog(@"旋转"); }
(4)捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)]; [imageView addGestureRecognizer:pinch]; // 实现方法 - (void)pinchAction:(UIPinchGestureRecognizer *)pinch { // 是根据scale 比例来形变的 pinch.view.transform = CGAffineTransformScale(pinch.view.transform,pinch.scale,pinch.scale); // 重置捏合的比例为:1 pinch.scale = 1; NSLog(@"捏合"); }
(5)平移
// 平移手势 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; [self.view addGestureRecognizer:pan]; // 实现方法 - (void)panAction:(UIPanGestureRecognizer *)pan { // 获取平移的点 (相对于要平移的视图) CGPoint p = [pan translationInView:pan.view]; // 根据这个点来改变形变属性 pan.view.transform = CGAffineTransformTranslate(pan.view.transform,p.x,p.y); // 重置这个点的初值 [pan setTranslation:CGPointMake:(0,0) inView:pan.view]; NSLog(@"平移"); }
(6)轻扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)]; [self.view addGestureRecognizer:swipe]; // 设置清扫的方向 swipe.direction = UISwipeGestureRecognizerDirectionLeft; // 实现轻扫方法 - (void)swipeAction:(UISwipeGestureRecognizer *)swipe { NSLog(@"轻扫"); }
(7)边缘扫
UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanAction:)]; [self.view addGestureRecognizer:edgePan]; edgePan.edges = UIRectEdgeLeft; // 从手机左侧边缘向右扫 // 实现方法 - (void)edgePanAction:(UIScreenEdgePanGestureRecognizer *)edgePan { // 一定是靠近屏幕边缘开始扫,才能触发 NSLog(@"哎呀,边缘扫啦"); }
相关文章推荐
- Android UI开发详解之ActionBar
- UEditor编辑器上传图片开发流程
- ueditor编辑器和at.js集成
- /Users/xxx/Library/Developer/Xcode/DerivedData/MAR-akbwbarcniqxy/build/products/debug-iphoneos
- storyboard 使用 UIRefreshControl 下拉更新
- 1051. Pop Sequence (25)
- 1051. Pop Sequence (25)
- iOS-Core-Animation-Advanced-Techniques
- iOS开发笔记-UITableView的单选
- IOS开发笔记-UITableView的多选
- HBuilder简介
- CF 601B(Lipshitz Sequence-贪心)
- UISlide 的应用之三色素的控制
- Flex4 flash builder保留MXML转换的AS代码
- Android之PinnedHeaderExpandableListView- 仿ios的UITableView的header置顶效果
- Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tools
- easyui中导航菜单accordion与tree的动态添加
- [LeetCode282]Closest Binary Search Tree Value II
- iOS之UI--指示器HUD的创建和设置
- [LeetCode270]Closest Binary Search Tree Value