将spring添加到pageCurl CATransition

时间:2018-04-30 14:56:29

标签: ios swift core-animation caanimation

我已经管理(通过StackOverflow的帮助)以编程方式向我的UIPageViewController添加部分页面curl。这用于向用户提示他们已完成该页面/屏幕并可以继续。

代码如下所示:

let animation = CATransition()
animation.duration = 0.3
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionDefault)
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
animation.endProgress = 0.2
animation.type = "pageCurl"

myPageViewController.view.layer.add(animation, forKey: "MyTransition")

这就是它的样子(对质量差的道歉 - mp4到gif不太好) enter image description here

我现在真正想做的是给动画添加一些弹簧,所以它看起来更好一些。但我发现很难从谷歌那里获取任何信息(可能甚至不可能?)。

这就是我尝试过的:

// Can't see an animation type on here, maybe keyPath is used as the type?
let x = CASpringAnimation(keyPath: "pageCurl")

// No endProgress either, just trying some random values now
x.fromValue = 0
x.toValue = 1

// No idea - found on Google - can play with the settings later
x.damping = 5
x.duration = x.settlingDuration

// These look familiar from my working animation, so i'll set these!
x.fillMode = kCAFillModeForwards
x.isRemovedOnCompletion = false

myPageViewController.view.layer.add(x, forKey: "MyNewTransition")

......它绝对没有。甚至没有崩溃。我希望它是因为你不能使用pageCurl作为keyPath,iOS不知道如何处理它,所以它被忽略了。

这是一段视频,大致展示了我对此的看法......

enter image description here

那么有没有人知道是否有可能做这个页面的曲目动画,但有一些春天?

1 个答案:

答案 0 :(得分:4)

编辑新的GIF:

class ViewController: UIViewController {

var animated:Bool = false
var counter: Int = 0
var transition = CATransition()

override func viewDidLoad() {
    super.viewDidLoad()

    view.layer.backgroundColor = UIColor.white.cgColor
}

@IBAction func animate(_ sender: Any) {
    animated = false
    counter = 0
    animateCurlPage(start: 0.0, end: 0.35, duration: 0.4)
}

func animateCurlPage(start: Float, end: Float, duration: CFTimeInterval) {


    if (counter == 3) { return }

    UIView.animate(withDuration: duration, animations: {
        if (self.animated) { return }
        self.transition.type = "pageCurl"
        self.transition.subtype = kCATransitionFromBottom
        self.transition.duration = duration
        self.transition.startProgress = start
        self.transition.endProgress = end
        self.transition.delegate = self
        self.transition.fillMode = kCAFillModeForwards
        self.transition.isRemovedOnCompletion = false

        self.view.layer.add(self.transition, forKey: "transition1")
    })

    if animated {
        animateRepeatCurlPage()
    }
}

func animateRepeatCurlPage() {

    UIView.animate(withDuration: 0.15, animations: {
        self.transition.type = "pageCurl"
        self.transition.subtype = kCATransitionFromBottom
        self.transition.duration = 0.15
        self.transition.startProgress = 0.32
        self.transition.endProgress = 0.31
        self.transition.delegate = self
        self.transition.fillMode = kCAFillModeForwards
        self.transition.isRemovedOnCompletion = (self.counter == 2) ? true : false

        self.view.layer.add(self.transition, forKey: "transition2")
    })

    counter += 1
}
}

extension ViewController: CAAnimationDelegate {

func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
    animateCurlPage(start: 0.35, end: 0.32, duration: 0.2)
    animated = true
}
}

以下是代码的结果:

enter image description here

我希望你不会再添加一个新的GIF:D!干杯!

编辑:您应该在我的回答之前添加GIF,现在看起来我为您的好问题添加了错误的解决方案。

对于页面卷曲动画和弹簧动画,您需要同时执行两个动画,因此您需要一个动画组才能处理它们,而且最重要的一个是不同的键< / strong>对于每个动画。在当前示例中,如果您对两个动画使用相同的键:transition,则只会执行一个动画,通常是最后一个。

你可以播放动画参数,我在这里添加了一些任意值。

@IBAction func animate(_ sender: Any) {

    let animationGroup = CAAnimationGroup()
    animationGroup.duration = 5
    animationGroup.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionLinear)
    animationGroup.animations = [animateCurlPage(), animateSpringTransition()]

    view.layer.add(animationGroup, forKey: "animation")
}

func animateCurlPage() -> CAAnimation {
    let transition = CATransition()
    transition.type = "pageCurl"
    transition.subtype = kCATransitionFromRight
    transition.duration = 1
    view.layer.add(transition, forKey: "transition")
    return transition
}

func animateSpringTransition() -> CAAnimation{
    let transitionX = CASpringAnimation(keyPath: "position.y")
    transitionX.duration = 0.5
    transitionX.fromValue = view.layer.position.y
    transitionX.toValue = view.layer.position.y - 60

    transitionX.damping = 5.0
    transitionX.initialVelocity = 1.0
    transitionX.stiffness = 20.0
    transitionX.beginTime = CACurrentMediaTime()
    view.layer.add(transitionX, forKey: "transitionX")
    return transitionX
}

结果如下:enter image description here