Ios 优化正在加热设备的多个连续动画
我正在做一个项目,在Ios 优化正在加热设备的多个连续动画,ios,swift,animation,optimization,uiview,Ios,Swift,Animation,Optimization,Uiview,我正在做一个项目,在ViewController上有大约16个UIViews 一些UIView包含一些处理器密集型CG图形,在加载时透明,而另一些UIView正在制作动画。特别是,以下旋转动画将连续运行。(如果我仔细观察,动画看起来并不完全平滑。) 我正在测试的设备正在预热,它不热,但正在变得非常热。我担心处理器过载 问题: 是否有更有效的方法来完成以下动画,这些动画使用更少的资源消耗和防止设备升温的方法连续运行 Xcode仪器: 加载时(最大值):CPU:56%-内存:29MB fun
ViewController
上有大约16个UIViews
一些UIView
包含一些处理器密集型CG
图形,在加载时透明,而另一些UIView
正在制作动画。特别是,以下旋转动画将连续运行。(如果我仔细观察,动画看起来并不完全平滑。)
我正在测试的设备正在预热,它不热,但正在变得非常热。我担心处理器过载
问题:
是否有更有效的方法来完成以下动画,这些动画使用更少的资源消耗和防止设备升温的方法连续运行
Xcode仪器:
加载时(最大值):CPU:56%-内存:29MB
func rotateForward() {
UIView.animateWithDuration(2, delay: 0, options: .CurveLinear, animations: { () -> Void in
self.viewA.transform = CGAffineTransformRotate(self.viewA.transform, CGFloat(M_PI_2))
self.viewB.transform = CGAffineTransformRotate(self.viewB.transform, CGFloat(M_PI_2))
self.viewC.transform = CGAffineTransformRotate(self.viewC.transform, CGFloat(M_PI_2))
self.viewD.transform = CGAffineTransformRotate(self.viewD.transform, CGFloat(M_PI_2))
}) { (finished) -> Void in
self.rotateForward()
}
}
func rotateBack() {
UIView.animateWithDuration(2, delay: 0, options: .CurveLinear, animations: { () -> Void in
self.viewW.transform = CGAffineTransformRotate(self.viewW.transform, -CGFloat(M_PI_2))
self.viewX.transform = CGAffineTransformRotate(self.viewX.transform, -CGFloat(M_PI_2))
self.viewY.transform = CGAffineTransformRotate(self.viewY.transform, -CGFloat(M_PI_2))
self.viewZ.transform = CGAffineTransformRotate(self.viewZ.transform, -CGFloat(M_PI_2))
}) { (finished) -> Void in
self.rotateBack()
}
}
运行时(最大值):CPU:1%-内存:29MB
func rotateForward() {
UIView.animateWithDuration(2, delay: 0, options: .CurveLinear, animations: { () -> Void in
self.viewA.transform = CGAffineTransformRotate(self.viewA.transform, CGFloat(M_PI_2))
self.viewB.transform = CGAffineTransformRotate(self.viewB.transform, CGFloat(M_PI_2))
self.viewC.transform = CGAffineTransformRotate(self.viewC.transform, CGFloat(M_PI_2))
self.viewD.transform = CGAffineTransformRotate(self.viewD.transform, CGFloat(M_PI_2))
}) { (finished) -> Void in
self.rotateForward()
}
}
func rotateBack() {
UIView.animateWithDuration(2, delay: 0, options: .CurveLinear, animations: { () -> Void in
self.viewW.transform = CGAffineTransformRotate(self.viewW.transform, -CGFloat(M_PI_2))
self.viewX.transform = CGAffineTransformRotate(self.viewX.transform, -CGFloat(M_PI_2))
self.viewY.transform = CGAffineTransformRotate(self.viewY.transform, -CGFloat(M_PI_2))
self.viewZ.transform = CGAffineTransformRotate(self.viewZ.transform, -CGFloat(M_PI_2))
}) { (finished) -> Void in
self.rotateBack()
}
}
您可以使用GPU来执行这些动画,而不是使用CPU来运行这些动画。 可以使用此协议使视图向前旋转。尝试对视图进行子类化以实现此协议并进行尝试
protocol Rotate{ }
extension Rotate where Self: UIView {
func rotate() {
let animation = CABasicAnimation(keyPath: "transform.rotation")
animation.duration = 2
animation.repeatCount = Float.infinity
animation.fromValue = 0.0
animation.toValue = Float(M_PI * 2.0)
layer.addAnimation(animation, forKey: "position")
}
}
您可以使用GPU来执行这些动画,而不是使用CPU来运行这些动画。 可以使用此协议使视图向前旋转。尝试对视图进行子类化以实现此协议并进行尝试
protocol Rotate{ }
extension Rotate where Self: UIView {
func rotate() {
let animation = CABasicAnimation(keyPath: "transform.rotation")
animation.duration = 2
animation.repeatCount = Float.infinity
animation.fromValue = 0.0
animation.toValue = Float(M_PI * 2.0)
layer.addAnimation(animation, forKey: "position")
}
}
谢谢,这似乎使它旋转。它现在使用更少的CPU了吗?层动画直接使用GPU来执行动画,不像直接使用CPU的视图动画。谢谢,这似乎可以旋转它。它现在使用更少的CPU了吗?与直接使用CPU的视图动画不同,层动画直接使用GPU执行动画。