项目开发中有个需求,须要给启动页加一个正在加载的动画,先上一个效果图。git
上图最底层浅色圆圈,咱们定义为浅A,转动的为深B,能够看到,深B是围绕着浅A圆圈的边缘旋转的。 下面对实现思想进行分析。github
核心的难点是如何让深B紧凑沿着浅A的圆边作轨迹运动,为此,咱们须要肯定一个圆轨迹C,而后让深B在轨迹C上作圆周运动动画
红色圆圈是轨迹C,它的圆点就是浅A的圆点,而后就是肯定半径,为了让深B沿着浅A的边缘运动,因此轨迹C的半径应该是(浅A直径 - 深B直径 )/ 2。spa
肯定轨迹C的圆点和半径code
let centerX = launchBottomView.center.x let centerY = launchBottomView.center.y let radius = (launchBottomView.bounds.size.width - launchTopView.bounds.size.width) / 2
建立椭圆轨迹Corm
let boundingRect = CGRect(x: centerX - radius, y: centerY - radius, width: radius * 2, height: radius * 2) let path = CGPath(ellipseIn: boundingRect, transform: nil)
深B作动画图片
let animation = CAKeyframeAnimation(keyPath:"position") animation.duration = 3 animation.path = path animation.calculationMode = kCAAnimationPaced animation.repeatCount = HUGE launchTopView.layer.add(animation, forKey:"Move")
调用肯定圆点、半径、动画的位置要写在 viewDidAppear方法中,不然会致使获取的控件大小不许确。