我正在關注tutorial以爲標籤欄圖像設置動畫。代碼給定的行,因爲它得到類型UITabBarButtonLabel而不是UIImageView的數據不適合我無法從UITabBar快速獲取UIImageView
secondItemView.subviews.first as! UIImageView
工作。 我加了標籤值1,這樣我可以得到的UIImageView使用標籤值
secondItemView.viewWithTag(1)
返回nil。獲得UIImageView參考的另一種方式是什麼?
代碼
@objc public class MTMainScreenTabsController : UITabBarController {
var secondItemImageView: UIImageView!
public override func viewDidLoad() {
super.viewDidLoad()
let secondItemView = self.tabBar.subviews[0]
print(secondItemView.subviews)
print(secondItemView.viewWithTag(1))
//self.secondItemImageView = secondItemView.subviews.secon
var item: UITabBarItem! = self.tabBar.items![0]
//self.secondItemImageView = secondItemView.viewWithTag(1) as! UIImageView
}
public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
public override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if item.tag == 1 {
self.secondItemImageView.transform = .identity
UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
() -> Void in
//let rotation = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
let rotation = CGAffineTransform.init(rotationAngle: CGFloat(Double.pi/2))
self.secondItemImageView.transform = rotation
}, completion: nil)
}
}
}
給標籤欄標籤1並檢查它 –
你能用更多的代碼更新你的問題嗎? –
@LuanTran添加的代碼 –