【问题标题】:Rotating CALayer more than 180 degrees using transform使用变换将 CALayer 旋转超过 180 度
【发布时间】:2015-02-19 21:44:46
【问题描述】:

我试图将 CALayer 旋转略小于 360 度,但变换将其沿相反方向旋转(即两个角度中较小的一个)。鉴于我还在图层上应用了平移变换,如何实现这种旋转?另外,我希望能够控制旋转方向。似乎 CAAnimation 不会注意并采用较短的变换角度进行旋转。看来这两个问题可能是相关的。这是我的 swift 代码的一部分:

func addAnimation() {
    var animation : CABasicAnimation = CABasicAnimation(keyPath: "transform")

    animation.delegate = self

    let translateDistY = CGRectGetMidY(self.view.bounds) - CGRectGetMidY(self.displayLayer.bounds)
    let rotationAngle = (CGFloat(M_PI*2.0) - 0.1)

    var transform : CATransform3D = CATransform3DMakeTranslation(0, translateDistY, 0)
    transform = CATransform3DRotate(transform, rotationAngle, 0, 0, 1)

    animation.fromValue = NSValue(CATransform3D: CATransform3DIdentity)
    animation.toValue = NSValue(CATransform3D: transform)

    animation.duration = 1.3
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)

    let animStruct = animationStruct(animLayer: self.displayLayer, animation: animation, layerTransform: transform)

    self.animationSequence.append(animStruct)
}

func doAnimation() {
    if  animationSequence.count > 0 {
        let animStruct = animationSequence.removeAtIndex(0)
        animStruct.animLayer.transform = animStruct.layerTransform
        animStruct.animLayer.addAnimation(animStruct.animation, forKey: nil)
    }
}

override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
    self.doAnimation()
}

【问题讨论】:

    标签: rotation core-animation calayer cabasicanimation catransform3d


    【解决方案1】:

    似乎没有任何方法可以旋转,但是您喜欢使用普通变换动画。一种方法是使用自定义图层属性。请参阅 Nick Lockwood 的这篇精彩文章。

    http://www.objc.io/issue-12/animating-custom-layer-properties.html

    【讨论】:

    • 感谢万的回复!我将浏览您提供的链接,这对我理解该主题肯定有很大帮助。我确实为我在下面发布的问题提出了一些解决方案。
    【解决方案2】:

    我对此问题有一些解决方案,但我不确定这是否正确或是否有更好的解决方案。尽管如此,下面是 swift 代码:

    func addAnimation() {
        let translateByValueY = CGRectGetMidY(self.view.bounds) - CGRectGetMidY(self.diaplayLayer.bounds)
    
        var animRotateZ : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        animRotateZ.duration = 1.5
        animRotateZ.toValue = NSNumber(double: -0.2)
        animRotateZ.byValue = NSNumber(double: 2.0 * M_PI)
        animRotateZ.delegate = self
        animRotateZ.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
        animRotateZ.setValue(self.diaplayLayer, forKey: "animationLayer")
        self.animationSequence.append(animRotateZ)
    
        var animTranslateY : CABasicAnimation = CABasicAnimation(keyPath: "transform.translation.y")
    
        animTranslateY.duration = 1.5
        animTranslateY.toValue = NSNumber(double: Double(CGRectGetMidY(self.view.bounds)))
        animTranslateY.byValue = NSNumber(double: Double(translateByValueY))
        animTranslateY.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
        animTranslateY.setValue(self.diaplayLayer, forKey: "animationLayer")
        self.animationSequence.append(animTranslateY)
    
    }
    
    func doAnimation() {
        if animationSequence.count > 0 {
            let animRotateZ = animationSequence.removeAtIndex(0)
            let animationRotateZLayer = animRotateZ.valueForKey("animationLayer") as CALayer
    
            animationRotateZLayer.setValue(animRotateZ.toValue, forKeyPath: "transform.rotation.z")
            animationRotateZLayer.addAnimation(animRotateZ, forKey: animRotateZ.keyPath)
    
            let animTranslateY = animationSequence.removeAtIndex(0)
            let animationTranslateYLayer = animTranslateY.valueForKey("animationLayer") as CALayer
    
            animationTranslateYLayer.setValue(animTranslateY.toValue, forKeyPath: "transform.translation.y")
            animationTranslateYLayer.addAnimation(animTranslateY, forKey: animTranslateY.keyPath)
        }
    }
    
    override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
        self.doAnimation()
    }
    

    我从stackoverflow上的以下帖子中提取了很多信息,可能要感谢他们:

    Using CABasicAnimation to rotate a UIImageView more than once

    How to chain different CAAnimation in an iOS application

    【讨论】:

      猜你喜欢
      • 2018-03-04
      • 2011-09-20
      • 2011-03-22
      • 1970-01-01
      • 2016-11-02
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多