Curling a UIView Up/Down with Swift

I had a problem creating the page curling effect between two views. The solution suggested on StackOverFlow is to use a container that uses auto layout for the two transitioning views, which do not use autolayout. Here’s what the problem looked like:
curl_up2

Here’s what the views should look like:

curl_up_working2

I committed the working project to my Swift Github repo: https://github.com/melling/Swift under the Xcode project TransitionWithView. Here’s the main functionality:

    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
                        //
                })

        })
    
    }

    func buildView() {

        let height = 100
        let width = 100

        container = UIView()
        container.setTranslatesAutoresizingMaskIntoConstraints(false)
        container.backgroundColor = UIColor.blackColor()
        self.view.addSubview(container)
        
        cardBack = UIView(frame: CGRectMake(0, 0, CGFloat(width), CGFloat(height)))
        
        cardBack.backgroundColor = UIColor.redColor()
        container.addSubview(cardBack)
        
        cardFront = UIView(frame: CGRectMake(0, 0, CGFloat(width), CGFloat(height)))
        
        cardFront.backgroundColor = UIColor.greenColor()
        container.addSubview(cardFront)
        

        let viewDictionary:Dictionary = ["container": container]

        let metrics:Dictionary = ["width": width, "height": height]

        let h0Constraint = "H:[container(==width)]"
        let v0Constraint = "V:[container(==height)]"
        
        addStandardConstraints(self.view, constraint: h0Constraint, viewDictionary: viewDictionary, metrics: metrics)
        addStandardConstraints(self.view, constraint: v0Constraint, viewDictionary: viewDictionary, metrics: metrics)
        
        centerViewXY(self.view, child: container)
        
        NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "curlUp", userInfo: nil, repeats: false)
        
        
    }

Advertisements