在iOS中事件分为三类:
触摸事件:通过触摸。手势进行触发事件(七大手势)
运动时间:通过加速器进行触发的事件(摇一摇)
远程控制事件:通过其他远程设备进行触发(如:遥控)
在iOS中只有继承自UIResponder类的对象才能处理事件(如UIView.UIViewController.UIApplication这些继承自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 NS_AVAILABLE_IOS(3_0); 运动开始时执行;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0); 运动结束后执行;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0); 运动被意外取消时执行;
远程控制事件 - (void)remoteControlReceivedWithEvent:(UIEvent *)event NS_AVAILABLE_IOS(4_0); 接收到远程控制消息时执行;
#import "TouchView.h"
#import "UIColor+RandomColor.h"
// 延展
@interface TouchView ()
@property(nonatomic,strong)UIView *textView;
@property(nonatomic,assign)CGFloat time1;
@property(nonatomic,assign)CGFloat time2;
@end
@implementation TouchView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.textView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
self.textView.backgroundColor = [UIColor greenColor];
[self addSubview:self.textView];
}
return self;
}
// 触摸开始
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"%s -- %d",__FUNCTION__,__LINE__);
UITouch *touch = [touches anyObject];
// 获取时间戳
_time1 = touch.timestamp;
}
// 触摸移动
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"%s -- %d",__FUNCTION__,__LINE__);
self.textView.backgroundColor = [UIColor randomColor];
// 取任意手指
UITouch *touch = [touches anyObject];
// 获取先前的点
CGPoint point1 = [touch previousLocationInView:_textView];
// 获取现在的点
CGPoint point2 = [touch locationInView:_textView];
// 偏移量
CGFloat dx = point2.x - point1.x;
CGFloat dy = point2.y - point1.y;
// 重新计算center
CGPoint newCenter = CGPointMake(_textView.center.x+dx, _textView.center.y+dy);
// 改变textView的center
_textView.center = newCenter;
}
// 触摸结束
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
_time2 = touch.timestamp;
//时间差
NSLog(@"总计时间:%lf",_time2 - _time1);
NSLog(@"%s -- %d",__FUNCTION__,__LINE__);
}
// 触摸取消
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"%s -- %d",__FUNCTION__,__LINE__);
}
这样点击事件我们知道了
触摸的七大手势:tap(轻拍),rotation(旋转),swipe(轻扫),pinch(捏合),pan(平移),longPress(长按),screenEdgePan(屏幕边界平移);
// 1创建手势,并初始化,使用initWithTarget:antion:的方法创建
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)];
// 2设置属性
// 轻拍次数
tap.numberOfTapsRequired = 2;
// 手指个数
tap.numberOfTouchesRequired = 2;
// 3添加到视图上
[_redView addGestureRecognizer:tap];
UIScreenEdgePanGestureRecognizer需要注意两点
UIScreenEdgePanGestureRecognizer *screEdgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screeEdgePan:)];
// 属性设置
// 注意: 使用屏幕边缘平移,注意两点
// 1. 视图的位置
// 2. 设置edges的属性
screEdgePan.edges = UIRectEdgeLeft;
[_redView addGestureRecognizer:screEdgePan];