【问题标题】:iOS - How to drag element during animationiOS - 如何在动画期间拖动元素
【发布时间】:2012-10-25 13:39:37
【问题描述】:

我需要在动画期间拖动一个元素。元素从屏幕顶部落下,我需要用户可以将其拖动到任何他想要的地方,即使在动画期间也是如此 谢谢

【问题讨论】:

  • 你的动画效果如何?
  • 使用 [UIView animateWithDuration:_DURATION_ animations:_BLOCK_]

标签: iphone ios animation drag-and-drop


【解决方案1】:

您可以使用 touchesBegan 方法来检测用户何时触摸元素。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if (touch != nil && (touch.view == elementView))
        //do your stuff
}

然后将元素的位置设置为触摸位置,并移除动画。

elementView.center = [touch locationInView:self.view];
[elementView.layer removeAllAnimations];

这应该可行。然后你可以使用类似的 touchesMoved 方法在拖动过程中更新位置。

【讨论】:

  • 是的,但如果在动画期间可以触摸元素,则此方法有效!
【解决方案2】:

由于您使用的是 UIView 基于块的动画,请尝试使用:

animateWithDuration:delay:options:animations:completion:

使用UIViewAnimationOptionAllowUserInteraction 选项。

【讨论】:

  • 这不起作用,我不知道为什么。我写了这段代码: [UIView animateWithDuration:4.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction & UIViewAnimationCurveLinear 动画:^ (void) { [ingredient setFrame:CGRectMake(20, 200, 34, 45)]; } 完成:^ (BOOL 完成) { NSLog(@"Completed"); }];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多