【问题标题】:UILabel move to point without snappingUILabel 移动到点而不捕捉
【发布时间】:2013-06-07 04:44:30
【问题描述】:

我有一个 UILabel,我在其中添加了一个 GestureRecognizer。我可以在屏幕上很好地拖动它,但我想要做的是当标签在一个区域内时,我希望它停止拖动并让它平滑地移动到该区域内的指定点。

因此,如果它的 x 值大于 150,则将其移动到 200 的 x 和 150 的 y

它确实有效,但它弹出到该位置,而不是平滑地移动到该位置。

这是我的拖动方法:

- (void)labelDragged:(UIPanGestureRecognizer *)gesture {
UILabel *label = (UILabel *)gesture.view;
CGPoint translation = [gesture translationInView:label];

// move label
label.center = CGPointMake(label.center.x + translation.x,
                           label.center.y + translation.y);

[gesture setTranslation:CGPointZero inView:label];

if (label.center.x > 150){

    [label setUserInteractionEnabled:NO];

    [UIView animateWithDuration:1.5 animations:^{
        label.center = CGPointMake(200 , 150);
    }];
}

}

任何帮助将不胜感激。我是使用动画和点的新手。

【问题讨论】:

    标签: ios xcode animation uilabel


    【解决方案1】:

    您必须等到 panGesture 状态。因此需要条件。使用平移手势时,您从标签中删除了 userInteraction。通过删除用户交互,它不会从 UILabel 中删除手势,这就是发生这种情况的原因。要在 UILabel 上工作动画,您有拖动后离开UILabel。从标签上移除手指,它会动画。使用以下代码。

    - (void)labelDragged:(UIPanGestureRecognizer *)gesture {
        UILabel *label = (UILabel *)gesture.view;
        CGPoint translation = [gesture translationInView:label];
    
        // move label
        label.center = CGPointMake(label.center.x + translation.x,
                                   label.center.y + translation.y);
        [gesture setTranslation:CGPointZero inView:label];
    
        if (label.center.x > 150){
            [label setUserInteractionEnabled:NO];
            if(gesture.state == UIGestureRecognizerStateEnded)
            {
                CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
                [animation setFromValue:[NSValue valueWithCGPoint:label.center]];
                [animation setToValue:[NSValue valueWithCGPoint:CGPointMake(200.0f, 400.0f)]];
                [animation setDuration:2.0f];
    
                [label.layer setPosition:CGPointMake(200.0f, 400.0f)];
                [label.layer addAnimation:animation forKey:@"position"];
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果您确实想在 UIPanGestureRecognizer 仍在执行时取消它,您可以这样做。将 enabled 属性设置为 NO,并在动画完成后重新启用它。

      if (label.center.x > 150){
          gesture.enabled = NO;
          [label setUserInteractionEnabled:NO];
      
          [UIView animateWithDuration:1.5 animations:^{
              label.center = CGPointMake(200 , 150);
          } completion:^(BOOL finished) {
              gesture.enabled = YES;
          }];
      }
      

      但是一旦标签被捕捉,您将无法拖动它,因为由于标签的位置,手势将始终被禁用。最好在手势结束时捕捉标签。

      if (gesture.state == UIGestureRecognizerStateEnded) {
          //check the position and snap the label        
      }
      

      【讨论】:

      • 感谢您的提示。有什么方法可以让标签移动而不是仅仅跳到该位置?就像磁铁一样,如果您将其拖动到将被拉入到位的位置附近。
      • @TazmanNZL 移动而不是跳跃是什么意思?上面的代码会将标签移动到指定的位置。
      • 是的,它确实会移动到该位置,但如果是 150 X,它将跳转到 200 X,而不是动画从 150 移动到 200。
      猜你喜欢
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 2012-03-02
      相关资源
      最近更新 更多