我想在用户按下UIBarButtonItem时实现缩放动画。 当tabBar中的tabBarItem被按下时,我如何检测?
到目前为止我的想法是,也许在UITabBarControllerDelege
中有一个方法?
我还没见过这样的问题。。。
谢谢,这个问题耽误了我好几个小时!
一般的想法是,您需要创建自己的自定义UIView
,然后将其传递到这个初始值设定器中。
let customView = MyCustomBarButtonItem()
let barButtonItem = UIBarButtonItem(customView: customView)
至于您如何实现自定义视图,以便您可以检测触控,您有很多选择。
您可以使用TouchesBegined
来检测向下触摸,也可以使用TouchesEnded
来检测条形按钮项上的“点击”。
class MyCustomBarButtonItem: UIView {
// ...
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// ...
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
// ...
}
}
另一种方法是将UIButton
子类化,并为.Touchdown
/.TouchupInside
控件事件添加目标/操作对。