質問編集履歴
2
あ
test
CHANGED
File without changes
|
test
CHANGED
@@ -7,133 +7,3 @@
|
|
7
7
|
Cast from 'UITabBar' to unrelated type 'RAMAnimatedTabBarController' always fails
|
8
8
|
|
9
9
|
が表示されてしまいます。
|
10
|
-
|
11
|
-
https://dev.classmethod.jp/articles/uitabbar_and_uitabbarcontroller/
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
そもそも、RAMAnimatedTabBarControllerをTabBarControllerにアニメーションがついただけのクラスと認識しているのですが、TabBarControllerに以下コードを記述する必要があるのでしょうか。
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
```ここに言語を入力
|
20
|
-
|
21
|
-
import UIKit
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
class RAMAnimatedTabBarController: UITabBarController, UITabBarControllerDelegate {
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
override func viewDidLoad() {
|
30
|
-
|
31
|
-
super.viewDidLoad()
|
32
|
-
|
33
|
-
}
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
|
38
|
-
|
39
|
-
guard let fromView = selectedViewController?.view, let toView = viewController.view else {
|
40
|
-
|
41
|
-
return false
|
42
|
-
|
43
|
-
}
|
44
|
-
|
45
|
-
if fromView != toView {
|
46
|
-
|
47
|
-
UIView.transition(from: fromView, to: toView, duration: 1, options: [.transitionCrossDissolve], completion: nil)
|
48
|
-
|
49
|
-
}
|
50
|
-
|
51
|
-
return true
|
52
|
-
|
53
|
-
}
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
func barItemImage(index: Int) -> UIImageView? {
|
58
|
-
|
59
|
-
let view = subviews[index + 1]
|
60
|
-
|
61
|
-
return view.recursiveSubviews.compactMap { $0 as? UIImageView }
|
62
|
-
|
63
|
-
.first
|
64
|
-
|
65
|
-
}
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
|
70
|
-
|
71
|
-
guard let index = tabBar.items?.firstIndex(of: item),
|
72
|
-
|
73
|
-
let customTabBar = tabBar as? RAMAnimatedTabBarController,
|
74
|
-
|
75
|
-
let imageView = customTabBar.barItemImage(index: index) else {
|
76
|
-
|
77
|
-
return
|
78
|
-
|
79
|
-
}
|
80
|
-
|
81
|
-
iconBounceAnimation(view: imageView)
|
82
|
-
|
83
|
-
}
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
func iconBounceAnimation(view: UIView) {
|
88
|
-
|
89
|
-
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
|
90
|
-
|
91
|
-
view.transform = CGAffineTransform(scaleX: 1.25, y: 1.25)
|
92
|
-
|
93
|
-
UIView.animate(withDuration: 0.5, delay: 0.2, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
|
94
|
-
|
95
|
-
view.transform = CGAffineTransform(scaleX: 1, y: 1)
|
96
|
-
|
97
|
-
}, completion: nil)
|
98
|
-
|
99
|
-
}, completion: nil)
|
100
|
-
|
101
|
-
}
|
102
|
-
|
103
|
-
}
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
extension UITabBar {
|
108
|
-
|
109
|
-
override open func sizeThatFits(_ size: CGSize) -> CGSize {
|
110
|
-
|
111
|
-
super.sizeThatFits(size)
|
112
|
-
|
113
|
-
var sizeThatFits = super.sizeThatFits(size)
|
114
|
-
|
115
|
-
sizeThatFits.height = 100
|
116
|
-
|
117
|
-
sizeThatFits.height += safeAreaInsets.bottom
|
118
|
-
|
119
|
-
return sizeThatFits
|
120
|
-
|
121
|
-
}
|
122
|
-
|
123
|
-
}
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
extension UIView {
|
128
|
-
|
129
|
-
// 再起的にsubviewsを取得
|
130
|
-
|
131
|
-
var recursiveSubviews: [UIView] {
|
132
|
-
|
133
|
-
return subviews + subviews.flatMap { $0.recursiveSubviews }
|
134
|
-
|
135
|
-
}
|
136
|
-
|
137
|
-
}
|
138
|
-
|
139
|
-
```
|
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,8 +1,18 @@
|
|
1
|
-
|
1
|
+
下記のサイトを参考に書いてみたのですが、うまくいきません。
|
2
2
|
|
3
|
-
|
3
|
+
subViewsのところで、cannnot find in scopeが表示され、
|
4
4
|
|
5
|
-
|
5
|
+
let customTabBar = tabBar as? RAMAnimatedTabBarController,に
|
6
|
+
|
7
|
+
Cast from 'UITabBar' to unrelated type 'RAMAnimatedTabBarController' always fails
|
8
|
+
|
9
|
+
が表示されてしまいます。
|
10
|
+
|
11
|
+
https://dev.classmethod.jp/articles/uitabbar_and_uitabbarcontroller/
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
そもそも、RAMAnimatedTabBarControllerをTabBarControllerにアニメーションがついただけのクラスと認識しているのですが、TabBarControllerに以下コードを記述する必要があるのでしょうか。
|
6
16
|
|
7
17
|
|
8
18
|
|
@@ -14,41 +24,7 @@
|
|
14
24
|
|
15
25
|
class RAMAnimatedTabBarController: UITabBarController, UITabBarControllerDelegate {
|
16
26
|
|
17
|
-
|
18
|
-
|
19
|
-
|
27
|
+
|
20
|
-
|
21
|
-
super.init(nibName: nil, bundle: nil)
|
22
|
-
|
23
|
-
object_setClass(self.tabBar, WeiTabBar.self)
|
24
|
-
|
25
|
-
}
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
required init?(coder: NSCoder) {
|
30
|
-
|
31
|
-
fatalError("init(coder:) has not been implemented")
|
32
|
-
|
33
|
-
}
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
class WeiTabBar: UITabBar {
|
38
|
-
|
39
|
-
override func sizeThatFits(_ size: CGSize) -> CGSize {
|
40
|
-
|
41
|
-
var sizeThatFits = super.sizeThatFits(size)
|
42
|
-
|
43
|
-
sizeThatFits.height = 120
|
44
|
-
|
45
|
-
return sizeThatFits
|
46
|
-
|
47
|
-
}
|
48
|
-
|
49
|
-
}
|
50
|
-
|
51
|
-
|
52
28
|
|
53
29
|
override func viewDidLoad() {
|
54
30
|
|
@@ -56,6 +32,108 @@
|
|
56
32
|
|
57
33
|
}
|
58
34
|
|
35
|
+
|
36
|
+
|
37
|
+
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
|
38
|
+
|
39
|
+
guard let fromView = selectedViewController?.view, let toView = viewController.view else {
|
40
|
+
|
41
|
+
return false
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
if fromView != toView {
|
46
|
+
|
47
|
+
UIView.transition(from: fromView, to: toView, duration: 1, options: [.transitionCrossDissolve], completion: nil)
|
48
|
+
|
49
|
+
}
|
50
|
+
|
51
|
+
return true
|
52
|
+
|
53
|
+
}
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
func barItemImage(index: Int) -> UIImageView? {
|
58
|
+
|
59
|
+
let view = subviews[index + 1]
|
60
|
+
|
61
|
+
return view.recursiveSubviews.compactMap { $0 as? UIImageView }
|
62
|
+
|
63
|
+
.first
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
|
70
|
+
|
71
|
+
guard let index = tabBar.items?.firstIndex(of: item),
|
72
|
+
|
73
|
+
let customTabBar = tabBar as? RAMAnimatedTabBarController,
|
74
|
+
|
75
|
+
let imageView = customTabBar.barItemImage(index: index) else {
|
76
|
+
|
77
|
+
return
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
iconBounceAnimation(view: imageView)
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
func iconBounceAnimation(view: UIView) {
|
88
|
+
|
89
|
+
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
|
90
|
+
|
91
|
+
view.transform = CGAffineTransform(scaleX: 1.25, y: 1.25)
|
92
|
+
|
93
|
+
UIView.animate(withDuration: 0.5, delay: 0.2, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
|
94
|
+
|
95
|
+
view.transform = CGAffineTransform(scaleX: 1, y: 1)
|
96
|
+
|
97
|
+
}, completion: nil)
|
98
|
+
|
99
|
+
}, completion: nil)
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
extension UITabBar {
|
108
|
+
|
109
|
+
override open func sizeThatFits(_ size: CGSize) -> CGSize {
|
110
|
+
|
111
|
+
super.sizeThatFits(size)
|
112
|
+
|
113
|
+
var sizeThatFits = super.sizeThatFits(size)
|
114
|
+
|
115
|
+
sizeThatFits.height = 100
|
116
|
+
|
117
|
+
sizeThatFits.height += safeAreaInsets.bottom
|
118
|
+
|
119
|
+
return sizeThatFits
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
extension UIView {
|
128
|
+
|
129
|
+
// 再起的にsubviewsを取得
|
130
|
+
|
131
|
+
var recursiveSubviews: [UIView] {
|
132
|
+
|
133
|
+
return subviews + subviews.flatMap { $0.recursiveSubviews }
|
134
|
+
|
135
|
+
}
|
136
|
+
|
59
137
|
}
|
60
138
|
|
61
139
|
```
|