haitham
haitham

Reputation: 3536

Swift CATransition Between Two Views - Slide over effect previous screen turns white

I am trying to animate between two full screen views (same view controller) in swift using CATransition. Looking to replicate (or get close to) navigation view controller push transition. Running into issue where view2 (the new view to present) is sliding over correctly, but view1 (the view underneath) disappears. Basically the view that is being animated is sliding over a white screen, but I want it to slider over the previous screen.

var pushAnimationEffect = CATransition()

override func viewDidLoad() {
    super.viewDidLoad()
    pageToUIView[i] = UIView()
    view1.frame = CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height)
    view1.center = CGPointMake(screenSize.width/2, screenSize.height/2)
    view2.frame = CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height)
    view2.center = CGPointMake(screenSize.width/2, screenSize.height/2)
    view1.layer.addAnimation(pushAnimationEffect, forKey: nil)
    view2.layer.addAnimation(pushAnimationEffect, forKey: nil)
    self.view.addSubview(view1)


    pushAnimationEffect.delegate = self
    pushAnimationEffect.duration = 0.4
    pushAnimationEffect.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
}

func nextPage() {
    pushAnimationEffect.type = kCATransitionMoveIn
    pushAnimationEffect.subtype = kCATransitionFromRight
    self.view.addSubview(view2)
    view1.removeFromSuperview()
}

func prevPage() {
    pushAnimationEffect.type = kCATransitionMoveIn
    pushAnimationEffect.subtype = kCATransitionFromLeft
    self.view.addSubview(view1)
    view2.removeFromSuperview()
}

Simplified the code a bit so its easier to read.

Is there a better way to do this? Is there something wrong with the code?

Upvotes: 0

Views: 1222

Answers (1)

MongoTheGeek
MongoTheGeek

Reputation: 294

You need to do the view remove on after the animation completes. Use the animationDidStop:finished: delegate method.

Upvotes: 0

Related Questions