iOS手势UIGestureRecognizer
来源:互联网 发布:linux systemd-logind 编辑:程序博客网 时间:2024/06/05 06:33
@UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为:
typedef NS_ENUM(NSInteger,UIGestureRecognizerState) {
UIGestureRecognizerStatePossible, // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state
UIGestureRecognizerStateBegan, // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateChanged, // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
UIGestureRecognizerStateEnded, // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateCancelled, // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
UIGestureRecognizerStateFailed, // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible
// Discrete Gestures – gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
};
它有6个子类处理具体的手势:
1.UITapGestureRecognizer (任意手指任意次数的点击)
// 点击次数
numberOfTapsRequired
// 手指个数
numberOfTouchesRequired
- UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] init];
- [tapGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
- [tapGestureRecognizer setNumberOfTapsRequired:2];
- [tapGestureRecognizer setNumberOfTouchesRequired:2];
- [self.view addGestureRecognizer:tapGestureRecognizer];
- [tapGestureRecognizer release];
2.UIPinchGestureRecognizer (两个手指捏合动作)
// 手指捏合,大于1表示两个手指之间的距离变大,小于1表示两个手指之间的距离变小
scale
// 手指捏合动作时的速率(加速度)
velocity
<span style="font-family:Comic Sans MS;font-size:18px;"> UIView * pinchView = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 10, 10)]; pinchView.backgroundColor = [UIColor redColor]; [self.view addSubview:pinchView]; UIPinchGestureRecognizer * panGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureRecognizer:)]; [pinchView addGestureRecognizer:panGesture]; [panGesture release]; - (void)pinchGestureRecognizer:(UIPinchGestureRecognizer *)pinchGesture{ // 实现了缩放 pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale); pinchGesture.scale = 1; }</span>
3.UIPanGestureRecognizer (摇动或者拖拽)
// 最少手指个数
minimumNumberOfTouches
// 最多手指个数
maximumNumberOfTouches
(CGPoint)translationInView:(UIView *)view方法的API解释如下:
The translation of the pan gesture in the coordinate system of the specified view.
Return Value
A point identifying the new location of a view in the coordinate system of its designated superview.
字面理解是:
在指定的视图坐标系统中转换(拖动?) pan gesture
返回参数:返回一个明确的新的坐标位置,在指定的父视图坐标系统中
简单的理解就是
该方法返回在横坐标上、纵坐标上拖动了多少像素
因为拖动起来一直是在递增,所以每次都要用setTranslation:重置 0 这样才不至于不受控制般滑动出视图
- (CGPoint)velocityInView:(UIView *)view方法的API解释如下:
The velocity of the pan gesture in the coordinate system of the specified view.
Return Value
The velocity of the pan gesture, which is expressed in points per second. The velocity is broken into horizontal and vertical components.
字面理解:
在指定坐标系统中pan gesture拖动的速度
返回参数:返回这种速度
简单的理解就是
你拖动这个图片的时候肯定有个速度,因此返回值就是你拖动时X和Y轴上的速度,速度是矢量,有方向。
- UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] init];
- [panGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
- [panGestureRecognizer setMinimumNumberOfTouches:1];
- [panGestureRecognizer setMaximumNumberOfTouches:5];
- [self.view addGestureRecognizer:panGestureRecognizer];
- [panGestureRecognizer release];
<span style="font-family:Comic Sans MS;font-size:18px;">- (void)panGestureRecognizer:(UIPanGestureRecognizer *)panGesture{ // NSLog(@"%s",__FUNCTION__); //CGPoint panPoint = [panGesture translationInView:self.view]; /*CGPoint newCenter = panGesture.view.center; newCenter.x += panPoint.x; newCenter.y += panPoint.y; panGesture.view.center = newCenter;*/ panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, panPoint.x, panPoint.y); [panGesture setTranslation:CGPointMake(0, 0) inView:self.view]; }</span>
4.UISwipeGestureRecognizer (手指在屏幕上滑动,轻扫操作手势)------>4个方向:上下左右
// 滑动手指的个数
numberOfTouchesRequired
// 手指滑动的方向 (Up,Down,Left,Right)
direction
- UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] init];
- [swipeGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
- [swipeGestureRecognizer setNumberOfTouchesRequired:2];
- [swipeGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];
- [self.view addGestureRecognizer:swipeGestureRecognizer];
- [swipeGestureRecognizer release];
5.UIRotationGestureRecognizer (手指在屏幕上旋转操作)
// 旋转方向,小于0为逆时针旋转手势,大于0为顺时针手势
rotation
// 旋转速率
velocity
<span style="font-family:Comic Sans MS;font-size:18px;">#pragma mark - 旋转手势 UILabel * rotationLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; rotationLabel.text = @"hello world"; rotationLabel.backgroundColor = [UIColor yellowColor]; [self.view addSubview:rotationLabel]; rotationLabel.userInteractionEnabled = YES; [rotationLabel release]; UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGestureHandle:)]; [rotationLabel addGestureRecognizer:rotation]; [rotation release]; - (void)rotationGestureHandle:(UIRotationGestureRecognizer *)gesture{ NSLog(@"%s",__FUNCTION__); // 进行旋转 gesture.view.transform = CGAffineTransformRotate(gesture.view.transform, gesture.rotation); gesture.rotation = 0;}</span>
6.UILongPressGestureRecognizer (长按手势)
// Default is 0. The number of full taps required before the press for gesture to be recognized
numberOfTapsRequired
// 需要长按的手指的个数
numberOfTouchesRequired
// 需要长按的时间,最小为0.5s
minimumPressDuration
// 手指按住允许移动的距离
allowableMovement
<span style="font-family:Comic Sans MS;font-size:18px;"> UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] init]; [longPressGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)]; [longPressGestureRecognizer setMinimumPressDuration:1.0f]; [longPressGestureRecognizer setAllowableMovement:50.0]; [self.view addGestureRecognizer:longPressGestureRecognizer]; [longPressGestureRecognizer release]; - (void)gestureRecognizerHandle:(UILongPressGestureRecognizer *)longGesture{ // 实现长按只执行一次 if (longGesture.state == UIGestureRecognizerStateBegan) { NSLog(@"%s",__FUNCTION__); } }</span>
- 使用iOS手势UIGestureRecognizer
- 使用iOS手势UIGestureRecognizer
- 使用iOS手势UIGestureRecognizer
- 使用iOS手势UIGestureRecognizer
- iOS手势UIGestureRecognizer
- 使用iOS手势UIGestureRecognizer
- 使用iOS手势UIGestureRecognizer
- 使用iOS手势UIGestureRecognizer
- 使用iOS手势UIGestureRecognizer
- 使用iOS手势UIGestureRecognizer
- 使用iOS手势UIGestureRecognizer
- ios 手势识别UIGestureRecognizer
- iOS手势UIGestureRecognizer
- iOS手势UIGestureRecognizer
- 使用iOS手势UIGestureRecognizer
- iOS手势UIGestureRecognizer
- 使用iOS手势UIGestureRecognizer
- iOS手势UIGestureRecognizer
- UIGestureRecognizer学习笔记
- iOS系统GCD学习(6):Dispatch Sources
- 计算机书目分享
- C# DataTable 用法小结
- Fedora20问题解决集锦
- iOS手势UIGestureRecognizer
- hive0.12+sqoop1.4 从mysql导入数据问题
- 典型DP_背包问题
- 无聊之破坏
- mysql行转列(综合利用if + sum)---为解决OSC的一个朋友的问题
- iOS系统GCD学习(7):dispatch_once
- 黑马程序员_GUI
- PL/SQL_使用复合数据类型3(多级集合)
- DNN摸索系列2: DNN 6 如何卸载模块