美文网首页
手势事件基础知识和例子

手势事件基础知识和例子

作者: 怪兽密保 | 来源:发表于2016-11-17 15:12 被阅读0次

点击手势UITapGestureRecognizer

创建手势处理器

<pre>
//新建轻拍手势事件类并添加手势事件
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleTap:)];
//哪个view需要添加上手势事件
[self addGestureRecognizer:gesture];
</pre>

一些相关的属性(官方解析)

numberOfTapsRequired----设置该点击手势处理器只处理的点击次数,默认为1

numberOfTouchesRequired----默认值为1.需要处理的手指数

UIPinchGestureRecognizer 缩放

<pre>
// 创建UIPinchGestureRecognizer手势处理器,该手势处理器激发scaleImage方法
UIPinchGestureRecognizer* gesture = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:@selector(scaleImage:)];
// 为imageView添加手势处理器
[self.imageView addGestureRecognizer:gesture];
</pre>

一些相关的属性

scale----相对于屏幕坐标中的触摸点的比例

velocity----缩放速度/秒

例子-通过捏合手势缩放图片

<pre>

  • (void) scaleImage:(UIPinchGestureRecognizer*)gesture
    {
    CGFloat scale = gesture.scale;
    // 如果捏合手势刚刚开始
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
    // 计算当前缩放比
    currentScale = self.imageView.image.size.width / srcImage.size.width;
    }
    // 根据手势处理的缩放比计算图片缩放后的目标大小
    CGSize targetSize = CGSizeMake(srcImage.size.width * scale * currentScale,
    srcImage.size.height * scale * currentScale);
    // 对图片进行缩放
    self.imageView.image = [srcImage imageByScalingToSize:targetSize];
    }
    </pre>

不多说,直接上Demo例子https://git.oschina.net/qjz.com/pinchImage/tree/master

旋转手势UIRotationGestureRecognizer

<pre>
//新建旋转手势事件类并添加手势事件
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotatePiece:)];
[self.viewPhoth addGestureRecognizer:rotationGesture];
</pre>

一些相关的属性

rotation----旋转的弧度

velocity----缩放速度/秒

旋转图片的例子

<pre>

  • (void) rotateImage:(UIRotationGestureRecognizer*)gesture
    {
    // 获取手势旋转的弧度
    CGFloat rotation = gesture.rotation;
    // 根据当前缩放比计算图片缩放后的目标大小
    CGSize targetSize = CGSizeMake(srcImage.size.width * currentScale,
    srcImage.size.height * currentScale);
    // 对图片进行缩放、旋转
    self.imageView.image = [[srcImage imageByScalingToSize:targetSize]
    imageRotatedByRadians:currentRotation + rotation];
    // 如果旋转手势结束
    if(gesture.state == UIGestureRecognizerStateEnded)
    {
    currentRotation = currentRotation + rotation;
    }
    }
    </pre>
    不多说,直接上demo栗子https://git.oschina.net/qjz.com/pinchImage/tree/master

长按手势UILongPressGestureRecognizer

创建手势处理器

<pre>
//新建长按手势事件类并添加手势事件
UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(viewLongPressGesture:)];
//哪个view需要添加上手势事件
[self addGestureRecognizer:longGesture];
</pre>

一些相关的属性(官方解析)

numberOfTapsRequired----默认值为0.在按下手势识别之前所需的全部点击数

numberOfTouchesRequired----默认值为1.要识别的手势必须按住的手指数

minimumPressDuration----默认值为0.5。 以秒为单位的手指必须按住手势才能识别手势

allowableMovement----默认值为10.手势失败前允许的最大移动(以像素为单位)。 一旦识别(在minimumPressDuration之后),对于触摸跟踪的剩余部分,手指移动没有限制

长按手势中的一些事件

<pre>
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
UIGestureRecognizerStatePossible, //识别器尚未识别其姿势,但可以是评估触摸事件。 这是默认状态
UIGestureRecognizerStateBegan, // 识别器已经接收到被识别为手势的触摸。 action方法将在下一个运行循环中调用
UIGestureRecognizerStateChanged, //识别器已经接收到被识别为对手势的改变的触摸。 action方法将在下一个运行循环中调用
UIGestureRecognizerStateEnded, // 识别器已经接收到被识别为手势的结束的触摸。 action方法将在下一个运行循环中被调用,识别器将被重置为UIGestureRecognizerStatePossible
UIGestureRecognizerStateCancelled, // 识别器已经接收到导致手势的取消的触摸。 action方法将在下一个运行循环中调用。 识别器将被重置为UIGestureRecognizerStatePossible

UIGestureRecognizerStateFailed,     // 识别器已经接收到不能被识别为手势的触摸序列。 将不会调用操作方法,并且识别器将重置为UIGestureRecognizerStatePossible
// 离散手势 - 识别离散事件但不报告更改的手势识别器(例如,轻敲)不会通过“开始”和“已更改”状态进行切换,也不会失败或被取消
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 识别器已经接收到被识别为手势的触摸。 action方法将在下一个运行循环中被调用,识别器将被重置为UIGestureRecognizerStatePossible

};
</pre>

移动(轻扫)手势UISwipeGestureRecognizer

<pre>

for (int i = 0 ; i < 4 ; i++)
{
// 创建手势处理器,指定使用该控制器的handleSwipe:方法处理轻扫手势
UISwipeGestureRecognizer* gesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipe:)];
// 设置该点击手势处理器只处理i个手指的轻扫手势
gesture.numberOfTouchesRequired = 1;
// 指定该手势处理器只处理1 << i方向的轻扫手势
gesture.direction = 1 << i;
// 为gv控件添加手势处理器。
[self.gv addGestureRecognizer:gesture];
}

</pre>

一些相关的属性值

numberOfTouchesRequired----默认值为1.必须滑动的手指数

@property(nonatomic) UISwipeGestureRecognizerDirection direction ----默认为UISwipeGestureRecognizerDirectionRight。 所需的滑动方向。 可以指定多个方向,如果它们将导致相同的行为(例如,UITableView滑动删除)

补充UISwipeGestureRecognizerDirection一共四个值,分别是右、左、上、下

typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
};

使用例子:
<pre>
// 实现手势处理器的方法,该方法应该声明一个形参。
// 当该方法被激发时,手势处理器会作为参数传给该方法的参数。

  • (void) handleSwipe:(UISwipeGestureRecognizer)gesture
    {
    // 获取轻扫手势的方向
    NSUInteger direction = gesture.direction;
    // 根据手势方向的值得到方向字符串
    NSString
    dirStr = direction == UISwipeGestureRecognizerDirectionRight
    ? @"向右" : (direction == UISwipeGestureRecognizerDirectionLeft
    ? @"向左" : (direction == UISwipeGestureRecognizerDirectionUp
    ? @"向上" :@"向下"));
    NSUInteger touchNum = gesture.numberOfTouchesRequired;
    self.label.text = [NSString stringWithFormat:
    @"用户使用%d个手指进行轻扫,方向为:%@" , touchNum , dirStr];
    // 指定2秒后清除label的文本
    [self.label performSelector:@selector(setText:)
    withObject:@"" afterDelay:2];
    }
    </pre>

不多说,直接上demo栗子https://git.oschina.net/qjz.com/pinchImage/tree/master

一个仿支付宝“更多”长按移动View,动态动画的参考Demo https://github.com/wolfhous/HSUpdateApp

相关文章

网友评论

      本文标题:手势事件基础知识和例子

      本文链接:https://www.haomeiwen.com/subject/hwrdpttx.html