gpt4 book ai didi

ios - CAKeyframeAnimation 在 Nib 中不起作用

转载 作者:行者123 更新时间:2023-11-28 08:16:55 25 4
gpt4 key购买 nike

我有一个 nib 文件,它必须为 View 设置动画以在圆形路径中移动。但是当我运行我的代码时,我看到 View 没有动画并且在我 View 的顶角

这是我的代码

override func awakeFromNib() {

let circlePath = UIBezierPath(arcCenter: CGPointMake(CGRectGetMidX(loadingview.bounds), CGRectGetMidY(loadingview.bounds)), radius: 30, startAngle: 0, endAngle:CGFloat(M_PI)*2, clockwise: true)

let animation = CAKeyframeAnimation(keyPath: "position")
animation.beginTime = 0.0
animation.duration = 1
animation.repeatCount = Float.infinity
animation.path = circlePath.CGPath

let squareView = UIView()
squareView.frame = CGRectMake(0, 0, 10, 10);
squareView.backgroundColor = UIColor.grayColor()

self.addSubview(squareView)
// You can also pass any unique string value for key


// circleLayer is only used to locate the circle animation path
let circleLayer = CAShapeLayer()
circleLayer.path = circlePath.CGPath
circleLayer.strokeColor = UIColor.blackColor().CGColor
circleLayer.fillColor = UIColor.clearColor().CGColor
loadingview.layer.addSublayer(circleLayer)

squareView.layer.addAnimation(animation, forKey: "position")

self.layer.shadowColor = UIColor.blackColor().CGColor
self.layer.shadowOpacity = 1
self.layer.shadowOffset = CGSize.zero
self.layer.shadowRadius = 1
self.layer.cornerRadius = 10.0
}

最佳答案

您不能在 awakeFromNib 中开始动画。现在还为时过早。你只能动画一个实际在你界面的view/layer hierarchy中的layer,而刚刚从nib出来的view的layer,比如loadingview,还没有被放到完整的界面中还没有。

此外,这看起来是 CABasicAnimation,而不是 CAKeyframeAnimation。你需要解决这个问题。

关于ios - CAKeyframeAnimation 在 Nib 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42363804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com