Does iOS Playgrounds supports UIView animations?

旧时模样 提交于 2019-12-03 23:39:32
lazi74

Xcode 7 update : XCPShowView is deprecated but you can see the animation in the playground liveView. And there is more coming soon: Interactive Playgrounds

Here is an update to Joseph Chen sample code. I also changed the color to green as the container default background is black :

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
Joseph Chen

Yes, it is (checked on Xcode 6.1.1, but may apply to earlier versions).

You should:

  1. Select the "Run in Full Simulator" option (see this answer for a step-by-step).

  2. Add the view you wish to animate within a container view.

For example, this shows a black square moving in a down-left motion:

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)
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!