加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 动画滞后于设备,但不在模拟器中

发布时间:2020-12-14 05:28:41 所属栏目:百科 来源:网络整理
导读:我想用Button触发CAAnimation.在游乐场和模拟器中,这完全符合我的要求.但是,当我在设备上运行相同的代码时,动画仅在短暂延迟后发生. 显然,这个问题只发生在iOS 11.2.6上.我更新了我的设备,现在无法重现该问题.任何人都可以确认或了解它如何在iOS 11.2.6上运
我想用Button触发CAAnimation.在游乐场和模拟器中,这完全符合我的要求.但是,当我在设备上运行相同的代码时,动画仅在短暂延迟后发生.

显然,这个问题只发生在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)
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读