两个视图之间的快速CATransition - 滑过效果前一个屏幕变为白色

时间:2015-07-16 19:30:14

标签: ios swift uiview caanimation catransition

我正在尝试使用CATransition在swift中使用两个全屏视图(相同的视图控制器)进行动画制作。希望复制(或接近)导航视图控制器推送转换。遇到问题,其中view2(要呈现的新视图)正确滑动,但view1(下面的视图)消失了。基本上,正在动画的视图在白色屏幕上滑动,但我希望它在上一个屏幕上滑动。

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

简化了代码,使其更易于阅读。

有更好的方法吗?代码有问题吗?

1 个答案:

答案 0 :(得分:0)

动画完成后,您需要执行视图移除操作。使用animationDidStop:finished:委托方法。