swift - 动画在设备上滞后,但在模拟器中不会

标签 swift core-animation

我想用一个按钮触发一个CAAnimation。在 playground 和模拟器中,这完全符合我的要求。但是,当我在设备上运行相同的代码时,动画只会在短暂的延迟后出现。

显然,这个问题只发生在 iOS 11.2.6 上。我更新了我的设备,现在无法再重现该问题。任何人都可以确认或找出它在 iOS 11.2.6 上的工作方式吗?

import UIKit

class MyViewController : UIViewController {
    let animatedView = UIView()

    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        // Add a button
        let button = UIButton(type: .system)
        button.frame = CGRect(x: 150, y: 200, width: 200, height: 50)
        button.setTitle("Animate", for: .normal)
        button.addTarget(self, action: #selector(tap), for: .touchUpInside)

        // Set color and frame of the view, that is animated.
        animatedView.backgroundColor = UIColor.blue
        animatedView.frame = CGRect(x: 50, y: 50, width: 50, height: 50)

        // Add the views to the view hierarchy
        view.addSubview(animatedView)
        view.addSubview(button)
        self.view = view
    }

    /// On Tap create an animation, that changes the position of the animated view.
    @objc func tap() {
        let originalY = animatedView.layer.position.y
        let animation = CABasicAnimation(keyPath: "position.y")
        animation.fromValue = originalY
        animation.toValue = 300.0
        animation.duration = 1.0
        animatedView.layer.add(animation, forKey: "positionAnimation")
    }
}

最佳答案

Can you try this code with iOS 11.2.9, I have tested your code and it's working fine.

// MARK: Animations
public extension CALayer {

    var ani: CAAnimation {
        let startPointAnim = CABasicAnimation(keyPath: #keyPath(CAGradientLayer.startPoint))
        startPointAnim.fromValue = CGPoint(x: 0, y: 0.0)
        startPointAnim.toValue = CGPoint(x:0, y: 300)

        let endPointAnim = CABasicAnimation(keyPath: #keyPath(CAGradientLayer.endPoint))
        endPointAnim.fromValue = CGPoint(x: 0, y: 0)
        endPointAnim.toValue = CGPoint(x:2, y: 300)

        let animGroup = CAAnimationGroup()
        animGroup.animations = [startPointAnim, endPointAnim]
        animGroup.duration = 1.0
        animGroup.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)            
        return animGroup
    }

    func playAnimation(_ anim: SkeletonLayerAnimation, key: String) {
        recursiveSearch(inArray: skeletonSublayers,
                        leafBlock: { add(anim(self), forKey: key) }) {
                            $0.playAnimation(anim, key: key)
        }
    }

    func stopAnimation(forKey key: String) {
        recursiveSearch(inArray: skeletonSublayers,
                        leafBlock: { removeAnimation(forKey: key) }) {
                            $0.stopAnimation(forKey: key)
        }
    }
}

关于swift - 动画在设备上滞后,但在模拟器中不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49618592/

相关文章:

iphone - 为什么这个CALayer不显示图像?

ios - CAGradientLayer 显示为纯色

ios - CATransform3DMakeRotation 框架

ios - 为什么我不能让我的动画持续时间为 0 秒?我将如何正确地做到这一点?

ios - Core Animation to flash score(UILabel)

reflection - Swift 发现协议(protocol)方法

ios - 文件上传数据任务结束时获取json数据

ios - 改变不同变量的动画回调

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

swift - 当用户将项目插入数据库时​​如何刷新 TableView ?