iOS有3大处理事件
- 触摸事件
- 加速计事件
- 远程操控事件
响应者对象
只有继承了UIResponder的对象,才是响应者对象
- 例如UIApplication、UIViewController、UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件
UIResponder内部提供了以下方法来处理事件
触摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
加速计事件
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
远程控制事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;
GestureReconizer
长按
UILongPressGestureRecognizer
有4个自有的属性
@property (nonatomic) NSUInteger numberOfTapsRequired; // Default is 0. The number of full taps required before the press for gesture to be recognized
@property (nonatomic) NSUInteger numberOfTouchesRequired; // Default is 1. Number of fingers that must be held down for the gesture to be recognized
@property (nonatomic) CFTimeInterval minimumPressDuration; // Default is 0.5. Time in seconds the fingers must be held down for the gesture to be recognized
@property (nonatomic) CGFloat allowableMovement;
轻扫
UISwipeGestureRecognizer
有2个自有的属性,主要是轻扫方向。而且,一个轻扫手势只可以有一个方向,想监听多个方向请添加多个手势
@property(nonatomic) NSUInteger numberOfTouchesRequired; // default is 1. the number of fingers that must swipe
@property(nonatomic) UISwipeGestureRecognizerDirection direction;
点击
UITapGestureRecognizer
因为这个,即使一些本身不是继承uicontrol的控件也可以监听点击事件
有2个自有的属性,关键是第一个属性,手机开发中最好不要使用多次点击,就是说点击次数最好为1
@property (nonatomic) NSUInteger numberOfTapsRequired; // Default is 1. The number of taps required to match
@property (nonatomic) NSUInteger numberOfTouchesRequired; ```
###拖拽
UIPanGestureReconizer
有3个属性
```objc
- (CGPoint)translationInView:(UIView *)view; // translation in the coordinate system of the specified view
- (void)setTranslation:(CGPoint)translation inView:(UIView *)view;
- (CGPoint)velocityInView:(UIView *)view;
具体使用:
//这个是获取手指从初始位置移动的偏移量
CGPoint offset = [tap translationInView:self.mainV];
//取出要改变的控件,重新设置frame
CGRect temp = self.mainV.frame;
temp.origin.x += offset.x;
temp.origin.y += offset.y;
self.mainV.frame = temp;
//再把形变设置为0,不然会因为不断调用这个方法,值会不断叠加,,
//例如从0 - 3的距离应该只移动3,但是却变1+2+3 = 6,移动了6的距离
[tap setTranslation:CGPointZero inView:self.mainV];
pan手势的开始需要手指滑动一定的量后,才会把移动后的点当做起始点,所以会有一个值的丢失,需要配合touchbegan进行使用
- 如何同时支持旋转和缩放?默认不支持多个手指,
Simultaneously:同时
当使用一个手势的时候会调用代理的Simultaneously方法,询问是否支持多个手势- pan
获取平移的位置:translationInView
复位:setTranslation:inView: 需要传一个view,因为点的位置跟坐标系有关系,看他是基于哪个坐标系被清空的。
- pan
translationInView locationInView velocityInView
locationView是UIGestureRecognizer的属性
translationInView是UIPanGestureRecognizer的属性
这两个的区别就是
locationView的话,最好是在控制器里面使用,然后传值设置为控制器的view。得出一个新的点,这个点就是移动后的点
移动控件的中心点的话,直接给他赋值这个新的point就好了
translationInView的话,设置传值为控件本身,得到的点,是指手指在控件上移动的offset。所以给控件的中心点加上这个offset,就可以使他准确的移动
- (void)translationInViewTest:(UIPanGestureRecognizer *)panGesture{
CGPoint currentP = [panGesture translationInView:self];
self.center = CGPointMake(self.center.x + currentP.x, self.center.y + currentP.y);
[panGesture setTranslation:CGPointZero inView:self];
}
- (void)locationInView:(UIPanGestureRecognizer *)panGesture{
CGPoint currentLIV = [panGesture locationInView:self.superview];
NSLog(@"currentTPPp%@",NSStringFromCGPoint(currentLIV));
self.center = currentLIV;
[self translationInViewTest:panGesture];
}