I am following tutorial to animate tab bar images. The given line of code doesn't work for me
secondItemView.subviews.first as! UIImageView
as it gets data of type UITabBarButtonLabel and not UIImageView.
I added tag value 1 so that I can get UIImageView using tag value
secondItemView.viewWithTag(1)
returns nil. What is the other way to get UIImageView's reference?
Code
@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)
}
}
}