1

I'm trying to do a simple flip animation between two views in a Playground using UIView.transitionFromView. As described in this question, older versions of Xcode simply required the option "Run in Full Simulator" to be selected, but that is no longer available in Playground Settings under Platform for Xcode 7.1.

In my working example, the bottom view is always immediately shown. The transition is performed, but with any animation:

import UIKit
import XCPlayground

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

func flipFrontToBack(frontView: UIView, backView: UIView) {

   let transitionOptions = UIViewAnimationOptions.TransitionFlipFromLeft

   var views : (frontView: UIView, backView: UIView)

   views = (frontView: frontView, backView: backView)

   UIView.transitionFromView(views.frontView,
      toView: views.backView,
      duration: 2.0,
      options: transitionOptions,
      completion: { _ in
   })
}

func smallSquare(backgroundColor: UIColor, borderColor: UIColor) ->     UIView {
    let bounds = CGRect(x: 50, y: 50, width: 50, height: 50)
    let view = UIView(frame: bounds)
    view.backgroundColor = backgroundColor
    view.layer.borderColor = borderColor.CGColor
    view.layer.borderWidth = 5
    view.layer.cornerRadius = 10

    return view
}

func makeView() -> UIView {
    let bounds = CGRect(x: 0, y: 0, width: 400, height: 200)
    let view = UIView(frame: bounds)
    view.backgroundColor = UIColor.lightGrayColor()
    view.layer.borderColor = UIColor.purpleColor().CGColor
    view.layer.borderWidth = 5
    view.layer.cornerRadius = 10

    return view
}

let view = makeView()

var frontCard = smallSquare(UIColor.blackColor(), borderColor: UIColor.whiteColor())
var backCard = smallSquare(UIColor.whiteColor(), borderColor: UIColor.redColor())

view.addSubview(backCard)
view.addSubview(frontCard)
XCPlaygroundPage.currentPage.liveView = view

flipFrontToBack(frontCard, backView: backCard)

My code works in my project in Xcode. I'm simply trying to migrate it to a Playground so I can prototype some additional changes. By commenting out the ** flipFrontToBack()** method, you can see the unflipped and "flipped" image. So, I get the flip, but no animation.

Unflipped

enter image description here

Community
  • 1
  • 1
h4labs
  • 765
  • 1
  • 10
  • 21
  • What are you actually trying to do? What was it that you expected Run In Full Simulator to do for you? – matt Oct 26 '15 at 04:09
  • I'm trying to do UIView animations: http://stackoverflow.com/questions/25192741/does-ios-playgrounds-supports-uiview-animations – h4labs Oct 26 '15 at 04:18
  • Did you read the docs? This has all changed in 7.1: https://developer.apple.com/library/prerelease/ios/documentation/Miscellaneous/Reference/XCPlaygroundModuleRef/XCPlayground.html – matt Oct 26 '15 at 04:33
  • Why yes I did. This line should have indicated that I read it: XCPlaygroundPage.currentPage.liveView = view ... I didn't include needsIndefiniteExecution = true in my example but I did set it in my playground – h4labs Oct 26 '15 at 04:34
  • Well, as you probably know, I think playgrounds are the work of the devil, so it's not something I'm going to get my knickers in a twist about. I stick with actual apps, and I don't quite see why you'd want this to work in a playground, esp. since you have it working in real life already. :) – matt Oct 26 '15 at 04:52
  • 1
    You're doing that thing where you're telling me that I shouldn't want to do what I want to do. I agree Playgrounds are a little limited but they are still kind of cool. I'm building out my own little Swift Cookbook and I'd like to do an iOS one too: http://www.h4labs.com/dev/ios/swift_cookbook.html – h4labs Oct 26 '15 at 04:59
  • Not at all. I'm doing that thing where I couldn't figure it out and I had no expectation or desire that it would work in the first place so I stopped caring and walked away. – matt Oct 26 '15 at 12:29
  • It has been figured out. I'll blog a better answer at some point. – h4labs Oct 28 '15 at 13:33

1 Answers1

2

I made a little progress. I had to use an NSTimer to schedule the flip for at least 0.1 seconds. The container flips, and not the two cards, but that's another issue. I also refactored into a class because they seem to be easier to work with once you get beyond a few lines. Anyway, some "working" code:

import UIKit
import XCPlayground

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

class MyView: UIView {

    var frontCard:UIView!
    var backCard:UIView!

    func makeView() {

        self.backgroundColor = UIColor.lightGrayColor()
        self.layer.borderColor = UIColor.purpleColor().CGColor
        self.layer.borderWidth = 5
        self.layer.cornerRadius = 10

    }


    func flipFrontToBack(frontView: UIView, backView: UIView) {

        let transitionOptions = UIViewAnimationOptions.    TransitionFlipFromLeft

        var views : (frontView: UIView, backView: UIView)

        views = (frontView: frontView, backView: backView)
        /*
        http://stackoverflow.com/questions/24413539/uiview-    transitionwithview-swift-syntax
        */

        UIView.transitionFromView(views.frontView,
            toView: views.backView,
            duration: 2.0,
            options: transitionOptions,
            completion: { _ in
        })
    }

    func smallSquare(backgroundColor: UIColor, borderColor:     UIColor) -> UIView {
        let bounds = CGRect(x: 50, y: 50, width: 50, height: 50)
        let view = UIView(frame: bounds)
        view.backgroundColor = backgroundColor
        view.layer.borderColor = borderColor.CGColor
        view.layer.borderWidth = 5
        view.layer.cornerRadius = 10

        return view
    }

    override init(frame: CGRect) {

        super.init(frame: frame)

        frontCard = smallSquare(UIColor.blackColor(), borderColor:     UIColor.whiteColor())
        backCard = smallSquare(UIColor.whiteColor(), borderColor:     UIColor.redColor())

        makeView()

        self.addSubview(backCard)
        self.addSubview(frontCard)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func update() {
        flipFrontToBack(frontCard, backView: backCard)
    }

    func flipIt() {
        NSTimer.scheduledTimerWithTimeInterval(0.1, target: self,     selector: "update", userInfo: nil, repeats: false)

    }
}

let bounds = CGRect(x: 0, y: 0, width: 400, height: 200)
let view = MyView(frame: bounds)

XCPlaygroundPage.currentPage.liveView = view
view.flipIt() // Works if NSTimer > 0
//view.update() // Doesn't work.  Flips immediately
h4labs
  • 765
  • 1
  • 10
  • 21