Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does iOS Playgrounds supports UIView animations?

Is it possible to preview animation done for UIView with animateWithDuration in Playground? I am adding XCPShowView right after initialization of UIView and its shows everything it in a final state no matter what position on a timeline I will choose.

like image 638
Nikita Leonov Avatar asked Aug 07 '14 21:08

Nikita Leonov


People also ask

Does UIView animate run on the main thread?

The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] .

What is iOS animation?

In iOS, animations are used extensively to reposition views, change their size, remove them from view hierarchies, and hide them. You might use animations to convey feedback to the user or to implement interesting visual effects. -


2 Answers

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)
})
like image 59
Joseph Chen Avatar answered Sep 30 '22 16:09

Joseph Chen


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
like image 25
lazi74 Avatar answered Sep 30 '22 16:09

lazi74