Reputation: 7948
I am trying to create custom animation for UINavigationItem
. I have navigation bar with 4 items, but when I scroll down I want to put search bar in navigation. I would like to have animation bottom-top, but everything I tried is not working.
Here are some stuff I tried:
setLeftBarButtonItem:animated:
(it is working, but it is jerky - not fluid at all. Kinda looks like gif...I should also note that I have one button and search bar in navigation after scroll.
My question is - Is it possible to change default animation and make my own? And, if so, how can I achieve that?
Upvotes: 0
Views: 579
Reputation: 86
I'm not sure I understand what you're trying to do exactly, but you could place a custom view into leftBarButtonItem
and animate it however you want (you'll need to implement touch handling in the view, though)...
Depending on the animation you need, you could use a UIScrollView
as the custom view and set it's contentOffset
depending on the contentOffset
of your views scrollview (creating a similar effect as the Twitter app uses when viewing a users main page).
I tried the first method (custom view with animations) and it seems to work quite nicely:
class itemView : UIView {
var active : Bool {
set {
UIView.animateWithDuration(1, animations: {
if newValue {
self.backgroundColor = UIColor.greenColor()
self.transform = CGAffineTransformMakeRotation(3.1415/2)
} else {
self.backgroundColor = UIColor.redColor()
self.transform = CGAffineTransformMakeRotation(-3.1415/2)
}
})
}
get {
if let color = backgroundColor {
return color.isEqual(UIColor.greenColor())
}
return false
}
}
}
I then used it like this (I'm changing the active state with a simple event handler, linked from the rightBarButtonItem
, but you can use just about anything here, including scrollViewDidScroll:
):
let item = itemView(frame: CGRectMake(0, 0, 30, 20))
override func viewDidLoad() {
super.viewDidLoad()
item.active = false
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(customView: item), animated: false)
}
@IBAction func itemClicked(sender: UIBarButtonItem) {
item.active = !item.active
}
Upvotes: 1