tabbar點選動畫
阿新 • • 發佈:2019-01-29
因為是剛學習swift4,所以程式碼工程相對規範一點,考慮長期。有需要的朋友可以直接去github下載
前言:本人剛接觸swift語法,之前一直從事開發Object-C。所以對於swift還是小白一枚。demo如果能幫到大家還希望點個星星謝謝。
直接上實現程式碼
首先為了滿足我們tabbar點選時,只有圖片做放大縮小動畫。那麼我們需要考慮幾個問題
1.找到指定點選tabbar按鈕
2.尋找tabbr上邊的圖片
基於這些思路,我們就可以開動程式碼了。
首先建立tabbarController,然後建立三個vc,這裡建立不做細講,主要看實現動畫程式碼
1.自定義tabbar繼承系統UITabBar,為了我們後續動畫做鋪墊,這裡自定義tabbar我們命名為SignTabbar
2.建立好我們的signTabbar後,通過系統KVC方式,替換掉系統原生tabbar使用我們自定義的signTabbar
let tab = SignTabbar()
setValue(tab, forKey: "tabBar")//KVC替換
3.替換成我們自定義的signTabbar之後,我們先列印一下UITabbar的檢視
[<_UIBarBackground: 0x10278c530; frame = (0 0; 320 49); userInteractionEnabled = NO; layer = <CALayer: 0x10278a3f0>>,
<UITabBarButton: 0x1027aa410; frame = (2 1; 103 48); opaque = NO; layer = <CALayer: 0x1027b3b80>>,
<UITabBarButton: 0x10652b630; frame = (109 1; 102 48); opaque = NO; layer = <CALayer: 0x10652c180>>,
<UITabBarButton: 0x1027bab10 ; frame = (215 1; 103 48); opaque = NO; layer = <CALayer: 0x10652ddc0>>]
[<_UIBarBackground: 0x10278c530; frame = (0 0; 320 49); userInteractionEnabled = NO; layer = <CALayer: 0x10278a3f0>>,
<UITabBarButton: 0x1027aa410; frame = (2 1; 103 48); opaque = NO; layer = <CALayer: 0x1027b3b80>>,
<UITabBarButton: 0x10652b630; frame = (109 1; 102 48); opaque = NO; layer = <CALayer: 0x10652c180>>,
<UITabBarButton: 0x1027bab10; frame = (215 1; 103 48); opaque = NO; layer = <CALayer: 0x10652ddc0>>]
列印tabbar的所有檢視即可出現以上列印結果
print(subviews)
4.請大家自動忽略走了兩次的問題,直接看今天的主題UITabBarButton,沒錯我們今天搞事情的物件就是這位大大
5.找到當前的UITabBarButton,自己增加點選事件。上程式碼
override func layoutSubviews() {
super.layoutSubviews()
print(subviews)
for tabBarButton in subviews {
if NSStringFromClass(tabBarButton.classForCoder) == "UITabBarButton"{
let ctr = tabBarButton as! UIControl
ctr.addTarget(self,
action: #selector(self.barButtonActin(sender:)),
for: UIControlEvents.touchUpInside)
}
}
}
6.實現點選方法
@objc func barButtonActin(sender:UIControl) {
}
7.這裡我們就要找到當前的UIImageView了,因為我們要給圖片做動畫,所以,列印UIControl的所有view,即subViews
我們看下列印的控制檯輸出
print(sender.subviews)
[<UITabBarSwappableImageView: 0x101adfdf0; frame = (40 13; 22 22); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x101ade900>>,
<UITabBarButtonLabel: 0x101ad8660; frame = (41 35; 20.5 12); text = '推薦'; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x101ab99e0>>]
當我們點選按鈕的時候,看下列印的控制檯,出現了兩個屬性。一個UITabBarSwappableImageView,我們要做動畫的檢視,一個就是UITabBarButtonLabel,暫且不管
8.那麼我們拿到了ImageView了,是不是再做動畫,就簡單多了呢,這裡舉例做一個放大縮小的動畫
@objc func tabBarAnimationWithView(view:UIView){
let scaleAnimation = CAKeyframeAnimation()
scaleAnimation.keyPath = "transform.scale"
scaleAnimation.values = [1.0,1.3,1.5,1.25,0.8,1.25,1.0]
scaleAnimation.duration = 0.5
scaleAnimation.calculationMode = kCAAnimationCubic
scaleAnimation.repeatCount = 1
view.layer.add(scaleAnimation, forKey: "123")
}
OK,到這裡動畫實現就完畢了,我們來看下完整的SignTabbar程式碼
class SignTabbar: UITabBar {
override func layoutSubviews() {
super.layoutSubviews()
for tabBarButton in subviews {
if NSStringFromClass(tabBarButton.classForCoder) == "UITabBarButton"{
let ctr = tabBarButton as! UIControl
ctr.addTarget(self,
action: #selector(self.barButtonActin(sender:)),
for: UIControlEvents.touchUpInside)
}
}
}
@objc func barButtonActin(sender:UIControl) {
print(sender.subviews)
for imageView in sender.subviews {
if NSStringFromClass(imageView.classForCoder) == "UITabBarSwappableImageView"{
self.tabBarAnimationWithView(view: imageView)
}
}
}
@objc func tabBarAnimationWithView(view:UIView){
let scaleAnimation = CAKeyframeAnimation()
scaleAnimation.keyPath = "transform.scale"
scaleAnimation.values = [1.0,1.3,1.5,1.25,0.8,1.25,1.0]
scaleAnimation.duration = 0.5
scaleAnimation.calculationMode = kCAAnimationCubic
scaleAnimation.repeatCount = 1
view.layer.add(scaleAnimation, forKey: "123")
}
}