在我们的应用程序中,我们有一个自定义警报通知,它基本上是 UITabBarItem
中的一个圆圈。我通过获取 UITabBarItem
的 View 然后在适当的位置创建一个 CAShapeLayer
来添加它。不幸的是,无论我尝试什么,我都无法使形状动画化。我试过为各种属性设置动画,例如 path 和 fillColor 但没有成功。
这是一段代码摘录:
var lyrShape = CAShapeLayer()
lyrShape.fillColor = UIColor.red.cgColor
lyrShape.path = UIBezierPath(overIn: CGRect(x: 5, y: 5, width: 10, height: 10)).cgPath
vwTabBar.layer.addSublayer(lyrShape)
var aniShape = CABasicAnimation(keyPath: "fillColor")
aniShape.duration = 5
aniShape.toValue = UIColor.blue.cgColor
aniShape.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
aniShape.fillMode = kCAFillModeBoth
aniShape.autoreverses = true
aniShape.repeatCount = 20
lyrShape.add(aniShape, forKey: aniShape.keyPath)
我已经在常规 View 中尝试过这段代码并且效果很好,但是无论我尝试什么我都无法让它在 UITabBarItem
中工作。
更新:我已经找到了重现此错误的确切步骤。使用带有按钮的普通旧 View Controller 启动您的应用程序。单击该按钮,显示选项卡 Controller View 。您在 TabBarItem 中绘制的形状此时不会设置动画。但是:如果 TabBarController 作为第一个 View 启动,则动画效果很好。
有没有人有什么想法?
谢谢!
最佳答案
首先我会说你问了一个很好的问题。我什至在寻找类似的东西,我得到了答案。您需要制作一个UITabBarController 的自定义类,然后您可以执行以下操作:
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
if item.tag == 1{
//do our animations
self.secondItemImageView.transform = CGAffineTransformIdentity
UIView.animateWithDuration(0.7, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: .CurveEaseInOut, animations: { () -> Void in
let rotation = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
self.secondItemImageView.transform = rotation
}, completion: nil)
}
}
我从最近阅读的一篇博文中得到了这个答案。为了进一步说明这一点,我建议您引用此博客: https://medium.com/@werry_paxman/bring-your-uitabbar-to-life-animating-uitabbaritem-images-with-swift-and-coregraphics-d3be75eb8d4d
希望对你有帮助:)
关于UITabBarItem 中的 iOS 自定义动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45103453/