手势操作知识大餐 ——iOS

来源:互联网 发布:不知道睡没睡着 知乎 编辑:程序博客网 时间:2024/05/23 19:18

1.点击(非连续型)

   UITapGestureRecognizer

Eg:

1)定义元素

   UIView* view = [[UIView alloc]initWithFrame:CGRectMake(0, 100, 300, 200)];
    view.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:view];


注意:当元素为UIImageView时,一定要设置它的userInteractionEnabled值为YES.


2)元素上添加手势

    UITapGestureRecognizer* clickRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureRecognizer:)];
    clickRecognizer.numberOfTouchesRequired = 2;//1.一根手指头  2.两根手指头 。。。(指头个数)
    clickRecognizer.numberOfTapsRequired = 2;//1.单击 2.双击(敲击次数)
//    clickRecognizer.numberOfTouches = 1;//暂时未知功能
    [view addGestureRecognizer:clickRecognizer];


2.拖动(连续型)

   UIPanGestureRecognizer

Eg:

1)定义元素(同上)

2)元素上添加手势

 UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [imageView addGestureRecognizer:recognizer];

3)拖动方法

-(void)handlePan:(UIPanGestureRecognizer*)recognizer{//可以根据自己需求修改拖动元素时的算法

    
    //视图前置操作
     [recognizer.view.superview bringSubviewToFront:recognizer.view];

         CGPoint center = recognizer.view.center;
         CGFloat cornerRadius = recognizer.view.frame.size.width / 2;
         CGPoint translation = [recognizer translationInView:self.view];
         //NSLog(@"%@", NSStringFromCGPoint(translation));
         recognizer.view.center = CGPointMake(center.x + translation.x, center.y + translation.y);
         [recognizer setTranslation:CGPointZero inView:self.view];
    
         if (recognizer.state == UIGestureRecognizerStateEnded) {
                 //计算速度向量的长度,当他小于200时,滑行会很短
                 CGPoint velocity = [recognizer velocityInView:self.view];
                 CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
                 CGFloat slideMult = magnitude / 200;
                 //NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult); //e.g. 397.973175, slideMult: 1.989866
        
                 //基于速度和速度因素计算一个终点
                 float slideFactor = 0.1 * slideMult;
                 CGPoint finalPoint = CGPointMake(center.x + (velocity.x * slideFactor),
                                                                                              center.y + (velocity.y * slideFactor));
                 //限制最小[cornerRadius]和最大边界值[self.view.bounds.size.width - cornerRadius],以免拖动出屏幕界限
                 finalPoint.x = MIN(MAX(finalPoint.x, cornerRadius),
                                                                  self.view.bounds.size.width - cornerRadius);
                 finalPoint.y = MIN(MAX(finalPoint.y, cornerRadius),
                                                                  self.view.bounds.size.height - cornerRadius);
        
                 //使用 UIView 动画使 view 滑行到终点
                 [UIView animateWithDuration:slideFactor*2
                                                   delay:0
                                                 options:UIViewAnimationOptionCurveEaseOut
                                              animations:^{
                                                      recognizer.view.center = finalPoint;
                                                  }
                                              completion:nil];
             }
    
    NSLog(@"test move.");
}


待续。。。


0 0