0

Using auto layout I've centered two UIView's on the screen and now I'm trying to transition with a CurlUp from one to the other then CurlDown using this function:

func curlUp() {
    let transitionOptions = UIViewAnimationOptions.TransitionCurlUp

    UIView.transitionFromView(cardFront,
        toView: cardBack,
        duration: 5.0,
        options: transitionOptions,
        completion: { _ in

            let transitionOptions = UIViewAnimationOptions.TransitionCurlDown

            UIView.transitionFromView(self.cardBack,
                toView: self.cardFront,
                duration: 5.0,
                options: transitionOptions,
                completion: { _ in
                    //
            })

    })

}

What's happening is that the entire screen is curling up as shown in this image:

enter image description here

The entire source for the View Controller is here: https://github.com/melling/Swift/blob/master/TransitionWithView/TransitionWithView/ViewController.swift

The Github project builds and runs with Xcode 6.3.1

Here's the code used to add the subviews:

func buildView() {

    cardBack = UIView()
    cardBack.setTranslatesAutoresizingMaskIntoConstraints(false)
    cardBack.backgroundColor = UIColor.redColor()
    self.view.addSubview(cardBack)

    cardFront = UIView()
    cardFront.setTranslatesAutoresizingMaskIntoConstraints(false)
    cardFront.backgroundColor = UIColor.greenColor()
    self.view.addSubview(cardFront)

    let height = 50
    let width = 50

    let viewDictionary:Dictionary<String,UIView> = ["cardFront": cardFront, "cardBack": cardBack]

    let metrics:Dictionary<String,Int> = ["width": width, "height": height]
    let hConstraint = "H:[cardFront(==width)]"
    let vConstraint = "V:[cardFront(==height)]"

    addStandardConstraints(hConstraint, viewDictionary: viewDictionary, metrics: metrics)
    addStandardConstraints(vConstraint, viewDictionary: viewDictionary, metrics: metrics)

    centerViewXY(self.view, child: cardFront)

    let h1Constraint = "H:[cardBack(==width)]"
    let v1Constraint = "V:[cardBack(==height)]"

    addStandardConstraints(h1Constraint, viewDictionary: viewDictionary, metrics: metrics)
    addStandardConstraints(v1Constraint, viewDictionary: viewDictionary, metrics: metrics)

    centerViewXY(self.view, child: cardBack)

    NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "curlUp", userInfo: nil, repeats: false)


}

func centerViewXY(parent: UIView, child: UIView) {

    var constX = NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: parent, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
    parent.addConstraint(constX)

    var constY = NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: parent, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
    parent.addConstraint(constY)

}

func addStandardConstraints(aConstraint:String, viewDictionary:Dictionary<String,UIView!>, metrics:Dictionary<String, Int>) {

    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(aConstraint, options: nil, metrics: metrics, views: viewDictionary))

}

[Update]

Creating the container worked. I committed the working code to Github: https://github.com/melling/Swift

TransitionWithView is the Xcode project.

enter image description here

h4labs
  • 765
  • 1
  • 10
  • 21
  • possible duplicate of [How to flip an individual UIView (without flipping the parent view)](http://stackoverflow.com/questions/9524048/how-to-flip-an-individual-uiview-without-flipping-the-parent-view) – Ali Beadle Apr 28 '15 at 18:22

1 Answers1

1

Yes, a common problem. You need to use another view to contain the two views you are transitioning between. See this other SO answer.

Community
  • 1
  • 1
Ali Beadle
  • 4,486
  • 3
  • 30
  • 55