ios - UIImageView 带有持续时间的动画,以及动画图像

标签 ios xcode swift

我刚开始 iOS 开发,正在学习 Swift 和 Xcode。我在 View Controller 中有一个 UIImageView 对象,并且想在它在两个图像之间切换时在屏幕上移动这个 UIImageView 。这是代码:

@IBOutlet weak var anImageView: UIImageView!

@IBAction func beginAnimation(sender: AnyObject) {

    let imgListArray:[AnyObject] = [UIImage(named: "img1.png")!, UIImage(named: "img2.png")!]
    anImageView.animationImages = imgListArray
    anImageView.animationDuration = 0.5
    anImageView.image = UIImage(named: "img1.png")
    anImageView.startAnimating()

    UIView.animateWithDuration(4.0, animations: {
        self.anImageView.center = CGPoint(x: 600, y: 600)
        }, completion: {finished in
            self.anImageView.center = CGPoint(x:80,y:80)})
}

问题出在坐标上。 UIImageView 从屏幕外开始并且不在 (80,80) 处结束。切换很好。

我想我必须将坐标转换到 superview(?) 空间,但我尝试这样做并没有成功。例如,我尝试了 convertPoint 函数,但没有成功。

非常感谢任何指导!

谢谢。

最佳答案

您还没有明确指定您想要做什么,所以让我们专注于这段代码做什么:

UIView.animateWithDuration(4.0, animations: {
    self.anImageView.center = CGPoint(x: 600, y: 600)
}, completion: {finished in
    self.anImageView.center = CGPoint(x:80,y:80)
})

该代码表示​​:

  1. 无论现在在哪里,都开始将 ImageView 从该点移动到 (600,600)(除非您使用的是 iPad,否则它将在屏幕外)。

  2. 当它到达那里时,让它从 (600,600)到 (80,80)。

这就是你所说的。所以,如果这不是您想要的,请不要那样说!

关于ios - UIImageView 带有持续时间的动画,以及动画图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30739360/

相关文章:

ios - 对于在 cellForItemAt 中将 isSelected 设置为 true 的单元格,为什么 UICollectionView 未将 isSelected 设置为 false?

ios - 将字符串从 CoreData 转换为 CLLocationDegrees/CLLocationCoordinate2D

ios - 无法在 Xcode 的预览 Pane 中容纳多个模拟器

python - XCode 3.2 Ruby 和 Python 模板

swift - 访问字符串中的字符,Swift 3.x

ios - Swift:计算属性的属性观察者

ios - 如何使用导航 Controller 对 Segue 进行单元测试

ios - 为什么新类必须是某些东西的子类?

插入时 Swift 4 + SQLite

ios - 根据另一个 UIViews 高度动态更改 Storyboard 中 UIView 的高度