Hi,
I have two views in my view hierarchy searchButtonView and searchBarView. I am trying to fade out the searchButtonView while animating the change in the frame of searchBarView simultaneously within a UIView.animate block. However, when I run the app, the frame of searchBarView resizes correctly while the alpha of searchButtonView does not animate to 0 as expected.
How can I fix this so that both view animate simultaneously?
Please see my code below.
Thank you
class MovieTitlesViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
var searchButtonView: UIView!
var searchBarView: UIView!
override func viewDidLayoutSubviews() {
createSearchButtonView()
createSearchBarView()
}
func createSearchButtonView() {
searchButtonView = UIView(frame: CGRect(x: 23, y: view.safeAreaInsets.top, width: 48, height: 48))
let searchImageView = UIImageView(image: UIImage(named: "Search Icon")!)
searchImageView.frame = CGRect(x: searchButtonView.frame.width / 2 - 16,
y: searchButtonView.frame.height / 2 - 16,
width: 32,
height: 32)
searchButtonView.addSubview(searchImageView)
searchButtonView.backgroundColor = .blue
searchButtonView.layer.cornerRadius = 24
searchButtonView.layer.borderColor = UIColor.gray.cgColor
searchButtonView.layer.borderWidth = 0.5
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tappedSearchView))
tapGesture.numberOfTapsRequired = 1
searchButtonView.addGestureRecognizer(tapGesture)
searchButtonView.isUserInteractionEnabled = true
view.addSubview(searchButtonView)
}
func createSearchBarView() {
searchBarView = UIView(frame: CGRect(x: 23,
y: view.safeAreaInsets.top,
width: 0,
height: 48))
searchBarView.backgroundColor = .red
// searchBarView.backgroundColor = UIColor(red: 217/255, green: 217/255, blue: 217/255, alpha: 1.0)
searchBarView.layer.cornerRadius = 24
searchBarView.layer.borderColor = UIColor.gray.cgColor
searchBarView.layer.borderWidth = 0.5
view.addSubview(searchBarView)
}
func animateExpandSearchView() {
UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveEaseInOut) {
self.searchButtonView.alpha = 0.0
self.searchBarView.frame = CGRect(x: 23, y: self.view.safeAreaInsets.top, width: UIScreen.main.bounds.width - 46, height: 48)
}
}
@objc func tappedSearchView() {
print("tapped search view")
animateExpandSearchView()
}
}
Topic:
UI Frameworks
SubTopic:
UIKit