手势
来源:互联网 发布:网络语 打脸 什么意思 编辑:程序博客网 时间:2024/05/01 09:49
手势:
1.UIResponder:是一个响应者(传达者),用来响应用户触摸屏幕的某些事件
2. ****手势不调用 检查是否设置了背景颜色****
3.代理方法:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;//手指触摸屏幕的时候调用
//可以通过touches获得点击视图的位置
- (CGPoint)locationInView:(nullable UIView *)view;
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;//手指触摸屏幕移动的时候调用
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;//手指离开屏幕的时候调用
- (void)touchesCancelled:(nullable NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event; //手指因外部事件取消触摸的时候调用
4.手势又分为6大手势
1⃣️点击:UITapGestureRecognizer
2⃣️点击长按:UILongPressGestureRecognizer
3⃣️拖拽:UIPanGestureRecognizer
4⃣️拖拽捏合:UIPinchGestureRecognizer
5⃣️轻扫:UISwipeGestureRecognizer
6⃣️旋转:UIRotationGestureRecognizer
所有的6大手势都继承于:UIGestureRecognizer
//初始化手势
- (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action;UIGestureRecognizer的初始化方法
//UIView中有添加手势方法
self.view addGestureRecognizer:<#(nonnull UIGestureRecognizer *)#>
//可以通过手势里面的view属性找到点击的视图
locationInView:找到点击的位置
UIGestureRecognizerStatePossible, 默认状态 UIGestureRecognizerStateBegan,开始状态
UIGestureRecognizerStateChanged, 改变状态 UIGestureRecognizerStateEnded, 手势结束UIGestureRecognizerStateCancelled,手势被取消
UIGestureRecognizerStateFailed, 手势失败UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
要想获得这些状态可以使用state--》只读
5.touchesBegan moved end
可以通过touches获得某个触摸事件
UITouch *touch =[touches anyObjects]
可以通过UITouch获得触摸的点的位置