【问题标题】:Make UIImageView Go Up Then Down Swift使 UIImageView 上升然后下降 Swift
【发布时间】:2014-06-08 02:05:32
【问题描述】:

我需要让 UIImageView 在屏幕被点击时向上动画,然后在它到达顶部时向下动画。

这是我目前得到的代码:

    override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
        if (CGRectIntersectsRect(self.playerImagePhone.frame, self.floorImagePhone.frame)) {
            UIView.animateWithDuration(0.4) {
                self.playerImagePhone.center = CGPointMake(self.playerImagePhone.center.x, self.playerImagePhone.center.y - 50)
                println("player jumped")
            }
        }
}

    override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
        UIView.animateWithDuration(0.4) {
            self.playerImagePhone.center = CGPointMake(self.playerImagePhone.center.x, self.playerImagePhone.center.y + 50)
            println("player jumped")
    }
}

此代码正在使其上升然后下降,但问题是它允许用户将手指放在屏幕上并且图像将保持不变。怎么让图片一上来就往下走?

谢谢

【问题讨论】:

  • 你有没有试过 userInteractionEnabled = NO 直到它完成下来?
  • 我现在试试,谢谢回复
  • 这导致用户仍然可以呆在那里,直到他们再次触摸屏幕。感谢您的尝试:)

标签: ios uiimageview swift


【解决方案1】:

touchesBegan 可以经常被调用......在那里执行动画可能不是一个好主意。您可能想要更新一个单独的对象来维护动画,然后在动画完成后(一旦持续时间结束)运行另一个动画以使图像恢复原状。

您也可以使用 animateWithDuration:animations:completion: 并在完成块中调用向下动画。

【讨论】:

  • 我现在就试试这些。会让你知道我是怎么走的。感谢回复
  • 如何检查持续时间何时结束?我将代码移到了另一个函数中,但我不知道如何检查动画的持续时间何时结束
  • 完成块中提供的任何代码都将在动画完成后运行。我会用它来运行向下动画。
  • 如何打开完成块?我目前有 UIView.animateWithDuration(0.4) { } 用于动画。完成在哪里?
  • UIView.animateWithDuration(0.2, 动画: { view.alpha = 0.0 }, 完成: { _ in view.removeFromSuperview() })
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多