1、触摸是在UIView上进行的。当用户触摸到屏幕时,触摸事件就会发生。触摸是由一系列的方法控制的。在使用这些方法前,要先创建一个触摸对象:
UITouch *touch = [touches anyObject];
在给定的触摸阶段中,如果发生新的触摸动作或已有的触摸动作发生变化,应用程序就会发送这些消息:
当一个或多个手指触碰屏幕时,发送touchesBegan:withEvent:消息。
当一个或多个手指在屏幕上移动时,发送touchesMoved:withEvent:消息。
当一个或多个手指离开屏幕时,发送touchesEnded:withEvent:消息。
当触摸序列被诸如电话呼入这样的系统事件所取消时,发送touchesCancelled:withEvent:消息
UITouch对象是一个手指接触到屏幕并在屏幕上移动或离开屏幕时创建的。它有几个属性和实例方法:
phase:属性,返回一个阶段常量,指出触摸开始、继续、结束或被取消,是一个枚举配型,包含了
UITouchPhaseBegan(触摸开始)
UITouchPhaseMoved(接触点移动)
UITouchPhaseStationary(接触点无移动)
UITouchPhaseEnded(触摸结束)
UITouchPhaseCancelled(触摸取消)
tapCount:属性,轻按屏幕的次数
timeStamp:属性,触摸发生的时间
view:属性,触摸始于那个视图
window:属性,触摸始于哪个窗口
lacationInView:方法,触摸在指定视图中的当前位置
previousLocationView:方法,触摸在指定视图中的前一个位置
二、手势识别器 UIGestureRecognizer 基类是一个抽象类,我们主要是使用它的子类:
点击:UITapGestureRecognizer
捏合:UIPinchGestureRecognizer
旋转:UIRotationGestureRecognizer
滑动:UISwipeGestureRecognizer
托移:UIPanGestureRecognizer
长按:UILongPressGestureRecognizer
具体使用方法如下:
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];
2 .UIPinchGestureRecognizer (两个手指捏合动作)
手指捏合,大于1表示两个手指之间的距离变大,小于1表示两个手指之间的距离变小:scale
手指捏合动作时的速率(加速度):velocity
- UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] init];
- [pinchGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
- [self.view addGestureRecognizer:pinchGestureRecognizer];
3.UIPanGestureRecognizer (摇动或者拖拽)
最少手指个数:minimumNumberOfTouches
最多手指个数:maximumNumberOfTouches
- UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] init];
- [panGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
- [panGestureRecognizer setMinimumNumberOfTouches:1];
- [panGestureRecognizer setMaximumNumberOfTouches:5];
- [self.view addGestureRecognizer:panGestureRecognizer];
4.UISwipeGestureRecognizer (手指在屏幕上滑动操作手势)
滑动手指的个数: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];
5.UIRotationGestureRecognizer (手指在屏幕上旋转操作)
旋转方向,小于0为逆时针旋转手势,大于0为顺时针手势:rotation
旋转速率:velocity
- UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] init];
- [rotationGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
- [self.view addGestureRecognizer:rotationGestureRecognizer];
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
- UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
- [longPressGestureRecognizer addTarget:self action:@selector(gestureRecognizerHandle:)];
- [longPressGestureRecognizer setMinimumPressDuration:1.0f];
- [longPressGestureRecognizer setAllowableMovement:50.0];
- [self.view addGestureRecognizer:longPressGestureRecognizer];
(资料来源于网络)