【发布时间】:2014-08-07 21:41:00
【问题描述】:
是否可以在 Playground 中使用 animateWithDuration 预览为 UIView 完成的动画?我在 UIView 初始化后立即添加 XCPShowView ,无论我选择时间轴上的哪个位置,它都会以最终状态显示所有内容。
【问题讨论】:
标签: ios uikit swift-playground
是否可以在 Playground 中使用 animateWithDuration 预览为 UIView 完成的动画?我在 UIView 初始化后立即添加 XCPShowView ,无论我选择时间轴上的哪个位置,它都会以最终状态显示所有内容。
【问题讨论】:
标签: ios uikit swift-playground
是的(在 Xcode 6.1.1 上检查过,但可能适用于早期版本)。
你应该:
选择“在完整模拟器中运行”选项(请参阅this answer 了解分步操作)。
在容器视图中添加您希望设置动画的视图。
例如,这显示了一个向左下方移动的黑色方块:
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)
})
【讨论】:
Xcode 7 更新:XCPShowView 已弃用,但您可以在 Playground liveView 中看到动画。还有更多即将推出:Interactive Playgrounds
这是 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
【讨论】: