iOS Playgrounds是否支持UIView动画?

Nik*_*nov 14 uikit ios swift-playground

是否可以使用Playground中的animateWithDuration预览为UIView完成的动画?我在UIView初始化之后立即添加了XCPShowView,并且无论我选择在时间轴上的什么位置,它都会显示它处于最终状态的所有内容.

Jos*_*hen 6

是的,它是(在Xcode 6.1.1上检查,但可能适用于早期版本).

你应该:

  1. 选择"在完全模拟器中运行"选项(有关详细步骤,请参阅此答案).

  2. 在容器视图中添加要设置动画的视图.

例如,这显示了以左下方运动的黑色方块:

import UIKit
import XCPlayground

let container = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))
XCPShowView("container", container)

let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 50.0, height: 50.0))
view.backgroundColor = UIColor.blackColor()
container.addSubview(view)

UIView.animateWithDuration(5.0, animations: { () -> Void in
    view.center = CGPoint(x: 75.0, y: 75.0)
})
Run Code Online (Sandbox Code Playgroud)


laz*_*i74 4

Xcode 7 更新:XCPShowView已弃用,但您可以在 Playground liveView 中看到动画。即将推出更多内容:互动游乐场

这是 Joseph Chen 示例代码的更新。我还将颜色更改为绿色,因为容器默认背景是黑色:

import UIKit
import XCPlayground

let container = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))
// XCPShowView("container", view: container)  // -> deprecated

let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 50.0, height: 50.0))
view.backgroundColor = UIColor.greenColor()
container.addSubview(view)

UIView.animateWithDuration(5) {
    view.center = CGPoint(x: 75.0, y: 75.0)
}

XCPlaygroundPage.currentPage.liveView=container
Run Code Online (Sandbox Code Playgroud)