您的位置:首页 > 产品设计 > UI/UE

UIGestureRecognizer 手势识别器

2015-10-12 09:16 513 查看
概述:
UIGestureRecognizer(手势识别器)继承与NSObject类, 作为一个抽象类, 为一系列基础的手势识别器提供了基础定义,其子类化手势识别器如下:
* UITapGestureRecognizer 轻拍手势识别器
* UIPinchGestureRecognizer 捏合手势识别器
* UIRotationGestureRecognizer 旋转手势识别器
* UISwipeGestureRecognizer 清扫手势识别器
* UIPanGestureRecognizer 平移手势识别器
* UIScreenEdgePanGestureRecognizer 屏幕边缘轻扫收拾识别器
* UILongPressGestureRecognizer 长按手势识别器

自定义手势,需要实现以下方法:

11、自定义手势

自定义手势继承:UIGestureRecognizer,实现下面的方法:

[cpp] view plaincopy

1 – touchesBegan:withEvent:

2 – touchesMoved:withEvent:

3 – touchesEnded:withEvent:

4 - touchesCancelled:withEvent:

新建一个类,继承UIGestureRecognizer,代码如下:
http://blog.csdn.net/totogo2010/article/details/8615940/
UIGestureRecognizer 属性方法

初始化方法
- (instancetype)initWithTarget:(id)
target

action:(SEL)
action

添加或者移除动作
- (void)addTarget:(id)
target

action:(SEL)
action

- (void)removeTarget:(id)
target

action:(SEL)
action

获取触摸/位置
- (CGPoint)locationInView:(UIView *)
view

- (CGPoint)locationOfTouch:(NSUInteger)
touchIndex

inView:(UIView *)
view

- (NSUInteger)numberOfTouches
手势在指定视图上的位置
指定手势 在指定视图上的位置
Returns the number of touches involved in the gesture represented by the receiver.
获取手势的状态和所在视图
@property(nonatomic, readonly) UIGestureRecognizerState state
@property(nonatomic, readonly) UIView *view
@property(nonatomic, getter=isEnabled) BOOL enabled
获取手势状态
获取手势所属视图
设置手势是否可用
取消或者延迟手势

建立一个关联手势
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer
*)
otherGestureRecognizer

当前手势失败时, 会执行与之相关联的手势, 比如轻拍于 从长安 轻扫于屏幕边缘轻扫
代理
@property(nonatomic, assign) id< UIGestureRecognizerDelegate > delegate
协议方法

.- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer
*)gestureRecognizer

• - (BOOL)gestureRecognizer:(UIGestureRecognizer
*)gestureRecognizer

shouldReceiveTouch:(UITouch
*)touch

· 协议方法
之是否允许某个指定手势可以同时识别多个动作;

- (BOOL)gestureRecognizer:(UIGestureRecognizer
*)gestureRecognizer

shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer
*)otherGestureRecognizer

• - (BOOL)gestureRecognizer:(UIGestureRecognizer
*)gestureRecognizer

shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer
*)otherGestureRecognizer

边缘平扫
和平扫的识别

• - (BOOL)gestureRecognizer:(UIGestureRecognizer
*)gestureRecognizer

shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer
*)otherGestureRecognizer
同时识别两个手势

只需要将其返回YES即可同时执行多种手势;如果手势之间或手势对应的视图有冲突或者有不同的需求,可以在这个方法里面添加一些相应的判断,例如一个uiview里面有3个手势分别是移动、缩放、旋转,而我只需要同时执行缩放和旋转的话:

// called when the recognition of one of gestureRecognizer or otherGestureRecognizer would be blocked by the other

// return YES to allow both to recognize simultaneously. the default implementation returns NO (by default no two gestures can be recognized simultaneously)

//

// note: returning YES is guaranteed to allow simultaneous recognition. returning NO is not guaranteed to prevent simultaneous recognition, as the other gesture's delegate may return YES

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

if ([gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]
&& [otherGestureRecognizer isKindOfClass:[UIRotationGestureRecognizer class]]) {

return YES;

}

returnNO;
}
1. 轻拍手势识别器(UITapGestureRecognizer)
基本属性

@property(nonatomic) NSUInteger numberOfTapsRequired
@property(nonatomic) NSUInteger numberOfTouchesRequired
实例

创建轻拍手势识别器
UITapGestureRecognizer *tap =[ [UITapGestureRecognizer alloc] initWithTarget:self action:@Selector(handleTap:)];
[aView addGestureRecognizer: tap];
[tap release];

执行方法
2. 捏合手势识别器(UIPinchGestureRecognizer)

属性

@property(nonatomic) CGFloat scale
@property(nonatomic, readonly) CGFloat velocity
两个手指间的坐标差
缩放速度
实例

//创建缩放手势识别器视图对象
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];

//为缩放手势设置代理对象

pinch.delegate
= self;

[aView
addGestureRecognizer:pinch];
[pinch release];
- (void)handlePinch:(UIPinchGestureRecognizer
*)sender

{

//获取手势作用的视图

UIView
*aView = sender.view;

//通过2D仿射变换函数中的缩放函数来修改view的transform属性

aView.transform
= CGAffineTransformScale(aView.transform, sender.scale,sender.scale);

sender.scale
= 1;
//每次缩放完成后,将缩放比例设置成100%,表示下一次缩放将以当前视图为原始比例开始缩放

}
3. 旋转手势识别器(UIRotationGestureRecognizer)

属性

@property(nonatomic) CGFloat rotation
@property(nonatomic, readonly) CGFloat velocity
旋转弧度
速度
实例

//创建旋转手势识别器对象

UIRotationGestureRecognizer
*rotation = [[UIRotationGestureRecognizer
alloc]initWithTarget:self
action:@selector(handleRotaion:)];

[aView addGestureRecognizer:rotation];
[rotation release];
//获取手势作用的视图

UIView
*aView = sender.view;

//通过2D仿射变换函数中的旋转函数修改视图中的transform属性

aView.transform
= CGAffineTransformRotate(aView.transform, sender.rotation);

//将角度差归零
sender.rotation = 0;
4. 轻扫手势识别器(UISwipeGestureRecognizer)

属性

@property(nonatomic) UISwipeGestureRecognizerDirection direction
@property(nonatomic) NSUInteger numberOfTouchesRequired
轻扫的方向
UISwipeGestureRecognizerDirectionRight

=
1
<<
0,

UISwipeGestureRecognizerDirectionLeft
=
1
<<
1,

UISwipeGestureRecognizerDirectionUp
=
1
<<
2,

UISwipeGestureRecognizerDirectionDown
=
1
<<
3
实例

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe)];
可获取轻扫方向
5. 轻拍手势识别器(UIPanGestureRecognizer)
基本属性

配置手势适配器

@property(nonatomic) NSUInteger maximumNumberOfTouches
@property(nonatomic) NSUInteger minimumNumberOfTouches

The minimum number of fingers that can be touching the view for this gesture to be recognized.
这个触摸事件能够处理的最多的手指数量 默认为最大整数

最小默认为1
实例

//创建平移手势识别器

UIPanGestureRecognizer
*pan = [[UIPanGestureRecognizer
alloc]initWithTarget:self
action:@selector(handPan:)];

//将手势识别器指定给需要识别的视图

[aView addGestureRecognizer:pan];
[pan release];
- (void)handPan:(UIPanGestureRecognizer
*)sender

{

//一旦一个识别器对象被添加到某个试图上,那么该识别器对象的view属相就会指向被作用的视图

UIView
*aView = sender.view;

//通过修改View的transform属性完成形变操作

CGPoint
currentPoint = [sender translationInView:aView.superview];

//transform可以让视图产生位移形变,借助与位移相关的2D仿射变换函数来完成操作

// aView.transform = CGAffineTransformMakeTranslation(currentPoint.x, currentPoint.y); //简化版--适用于一次平移

aView.transform
= CGAffineTransformTranslate(aView.transform, currentPoint.x,
currentPoint.y);

[sender setTranslation:CGPointZero
inView:aView.superview];
}
6. 长按手势识别器(UILongPressGestureRecognizer)

属性

@property(nonatomic) CFTimeInterval minimumPressDuration
@property(nonatomic) NSUInteger numberOfTouchesRequired
@property(nonatomic) NSUInteger numberOfTapsRequired
@property(nonatomic) CGFloat allowableMovement
最短按压时间

允许按压时移动的距离 默认时10
实例

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget: self action :@Selecor(handleLongPress)
longPress.minimumPressDuration = 3;
7. 屏幕边缘轻扫手势识别器(UIScreenEdgePanGestureRecognizer)

属性

@property(readwrite, nonatomic, assign) UIRectEdge edges轻扫的边缘
实例

实例化后可通过获取轻扫的边缘 进行操作
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: