码迷,mamicode.com
首页 > 移动开发 > 详细

【iOS开发-手势】iOS中各种手势总结

时间:2015-05-19 19:06:33      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:手势触摸

iOS中支持的手势事件

  1. UITapGestureRecognizer(敲击)
  2. UIPinchGestureRecognizer(捏合,用于缩放)
  3. UIPanGestureRecognizer(拖拽)
  4. UISwipeGestureRecognizer(轻扫)
  5. UIRotationGestureRecognizer(旋转)
  6. UILongPressGestureRecognizer(长按)

UITapGestureRecognizer(敲击)

//创建UITapGestureRecognizer对象,并且初始化的时候就绑定事件
  UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)];

 //连击几次才触发事件   
recognizer.numberOfTapsRequired = 2;
 //在对应的View上添加这个手势监听   
[self.myView addGestureRecognizer:recognizer];

UILongPressGestureRecognizer(长按)

//创建UILongPressGestureRecognizer对象,并初始化的时候就绑定事件
    UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)];

recognizer.minimumPressDuration = 2;//2秒后触发事件

[self.myView addGestureRecognizer:recognizer];

UISwipeGestureRecognizer(轻扫)

 //创建UISwipeGestureRecognizer对象
    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)];
    //手势扫的方向
recognizer.direction = UISwipeGestureRecognizerDirectionLeft;

[self.myView addGestureRecognizer:recognizer];

UIRotationGestureRecognizer(旋转)

例子:旋转一个view

- (void)Rotate
{
//创建对象
    UIRotationGestureRecognizer *recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
    recognizer.delegate = self;//每一个View要想支持多个手势,就要实现这个代理
    [self.iconView addGestureRecognizer:recognizer];
}

//执行这个方法时候,会默认把旋转对象传过去
- (void)rotateView:(UIRotationGestureRecognizer *)recognizer
{
//recognizer.rotation等于旋转角度,加上前一个他的前一个角度,所以每次把前一个角度清0,这个值就等于旋转角度
//每次要把当前的transform+旋转的角度
    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
    //要把旋转的角度设为0
    recognizer.rotation = 0; 
}

UIPinchGestureRecognizer(捏合)

- (void)Pinch
{
    //创建对象
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
    pinch.delegate = self;
    [self.iconView addGestureRecognizer:pinch];
}

- (void)pinchView:(UIPinchGestureRecognizer *)pinch
{
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    pinch.scale = 1; 
}

UIPanGestureRecognizer(拖拽)

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
    [self.purpleView addGestureRecognizer:pan];
}

- (void)panView:(UIPanGestureRecognizer *)pan
{

    switch (pan.state) {
        case UIGestureRecognizerStateBegan: // 开始触发手势

            break;

        case UIGestureRecognizerStateEnded: // 手势结束

            break;

        default:
            break;
    }

    // 1.在view上面挪动的距离
    CGPoint translation = [pan translationInView:pan.view];
    CGPoint center = pan.view.center;
    center.x += translation.x;
    center.y += translation.y;
    pan.view.center = center;

    // 2.清空移动的距离
    [pan setTranslation:CGPointZero inView:pan.view];
}

多手势的支持

目标View要想支持多个手势的必须实现UIGestureRecognizerDelegate代理
然后实现下面的方法


/**
 *  是否允许多个手势识别器同时有效
 *  Simultaneously : 同时地
 */
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

【iOS开发-手势】iOS中各种手势总结

标签:手势触摸

原文地址:http://blog.csdn.net/ttf1993/article/details/45847323

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!