问题:如何平滑旋转并用阴影改变 UIView
的大小?
答案:
要实现平滑旋转并用阴影改变 UIView
的大小,您可以使用以下技术:
CABasicAnimation
创建基本动画,对 UIView
的缩放、旋转和透明度进行操作。import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let container = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
container.backgroundColor = .blue
let animation = CABasicAnimation(keyPath: "transform.rotation")
animation.fromValue = 0.0
animation.toValue = CGFloat.pi/4
animation.duration = 1.0
animation.repeatCount = 1
animation.autoreverses = true
animation.timingFunction = CAMediaTimingFunction(name: .easeInOutQuart)
container.layer.add(animation, forKey: "rotationAnimation")
let gradientLayer = CAGradientLayer(frame: container.bounds)
gradientLayer.frame = container.bounds
gradientLayer.colors = [UIColor.red.cgColor, UIColor.green.cgColor]
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = CGPoint(x: 1, y: 1)
container.layer.addSublayer(gradientLayer)
UIView.animate(withDuration: 2, delay: 0, options: .curveEaseInOut, animations: {
self.container.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
self.container.layer.cornerRadius = 10
}, completion: nil)
}
}
CAGradientLayer
添加渐变效果。let gradientLayer = CAGradientLayer(frame: container.bounds)
gradientLayer.frame = container.bounds
gradientLayer.colors = [UIColor.red.cgColor, UIColor.green.cgColor]
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = CGPoint(x: 1, y: 1)
container.layer.addSublayer(gradientLayer)
UIView
的 animate
方法实现动画效果。UIView.animate(withDuration: 2, delay: 0, options: .curveEaseInOut, animations: {
self.container.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
self.container.layer.cornerRadius = 10
}, completion: nil)
以上三种方法都可以实现平滑旋转并阴影改变 UIView
的大小。可以根据您的需求选择最适合的方法。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云