我想在旋转UITabBarItem
图像时旋转它。我在viewDidLoad
的
UITabBarController
方法中使用了以下代码
let plusView2 = self.tabBar.subviews[1]
self.SecondItemImageView = plusView2.subviews.first as? UIImageView
self.SecondItemImageView.contentMode = .center
let pluseView3 = self.tabBar.subviews[2]
self.ThirdItemImageView = pluseView3.subviews.first as? UIImageView
self.ThirdItemImageView.contentMode = .center
,并在下面的代码中使用了旋转动画
public override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if item.tag == 2 {
UIView.animate(withDuration: 0.2, delay: 0.1, options: .curveEaseIn, animations: {
self.SecondItemImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
UIView.animate(withDuration: 0.1, delay: 0.1, options: .curveEaseIn, animations: {
self.SecondItemImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi * 2))
}, completion: nil)
}) { (done) in
}
}
else if item.tag == 3 {
UIView.animate(withDuration: 0.2, delay: 0.1, options: .curveEaseIn, animations: {
self.ThirdItemImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
UIView.animate(withDuration: 0.1, delay: 0.1, options: UIView.AnimationOptions.curveEaseIn, animations: { () -> Void in
self.ThirdItemImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi * 2))
}, completion: nil)
}) { (done) in
}
}
}
在故事板中设置标签2和3 现在,当点击标签号3时,按钮图像旋转可以正常工作,但是对于标签号2时,按钮图像不旋转。两个标签栏项目使用的相同代码不能工作。另外,如果条件正常但图像未旋转时,请检查何时点击2号标签按钮。