forked from ChenYilong/CYLTabBarController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppDelegate.m
159 lines (128 loc) · 6.79 KB
/
AppDelegate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// AppDelegate.m
// CYLTabBarController
//
// v1.12.0 Created by 微博@iOS程序犭袁 ( http://weibo.com/luohanchenyilong/ ) on 10/20/15.
// Copyright © 2015 https://github.com/ChenYilong . All rights reserved.
//
#import "AppDelegate.h"
#import "CYLTabBarControllerConfig.h"
#import "CYLPlusButtonSubclass.h"
@interface AppDelegate ()<UITabBarControllerDelegate, CYLTabBarControllerDelegate>
@end
@implementation AppDelegate
#define RANDOM_COLOR [UIColor colorWithHue: (arc4random() % 256 / 256.0) saturation:((arc4random()% 128 / 256.0 ) + 0.5) brightness:(( arc4random() % 128 / 256.0 ) + 0.5) alpha:1]
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 设置主窗口,并设置根控制器
self.window = [[UIWindow alloc]init];
self.window.frame = [UIScreen mainScreen].bounds;
[CYLPlusButtonSubclass registerPlusButton];
CYLTabBarControllerConfig *tabBarControllerConfig = [[CYLTabBarControllerConfig alloc] init];
CYLTabBarController *tabBarController = tabBarControllerConfig.tabBarController;
[self.window setRootViewController:tabBarController];
[tabBarController setViewDidLayoutSubViewsBlock:^(CYLTabBarController *aTabBarController) {
UIViewController *viewController = aTabBarController.viewControllers[0];
UIView *tabBadgePointView0 = [UIView cyl_tabBadgePointViewWithClolor:RANDOM_COLOR radius:4.5];
[viewController.tabBarItem.cyl_tabButton cyl_setTabBadgePointView:tabBadgePointView0];
[viewController cyl_showTabBadgePoint];
UIView *tabBadgePointView1 = [UIView cyl_tabBadgePointViewWithClolor:RANDOM_COLOR radius:4.5];
[aTabBarController.viewControllers[1] cyl_setTabBadgePointView:tabBadgePointView1];
[aTabBarController.viewControllers[1] cyl_showTabBadgePoint];
UIView *tabBadgePointView2 = [UIView cyl_tabBadgePointViewWithClolor:RANDOM_COLOR radius:4.5];
[aTabBarController.viewControllers[2] cyl_setTabBadgePointView:tabBadgePointView2];
[aTabBarController.viewControllers[2] cyl_showTabBadgePoint];
[aTabBarController.viewControllers[3] cyl_showTabBadgePoint];
//添加提示动画,引导用户点击
[self addScaleAnimationOnView:aTabBarController.viewControllers[3].cyl_tabButton.cyl_tabImageView repeatCount:20];
}];
tabBarController.delegate = self;
[self.window makeKeyAndVisible];
[self customizeInterface];
return YES;
}
- (void)customizeInterface {
[self setUpNavigationBarAppearance];
}
/**
* 设置navigationBar样式
*/
- (void)setUpNavigationBarAppearance {
UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
UIImage *backgroundImage = nil;
NSDictionary *textAttributes = nil;
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
backgroundImage = [UIImage imageNamed:@"navigationbar_background_tall"];
textAttributes = @{
NSFontAttributeName : [UIFont boldSystemFontOfSize:18],
NSForegroundColorAttributeName : [UIColor blackColor],
};
} else {
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
backgroundImage = [UIImage imageNamed:@"navigationbar_background"];
textAttributes = @{
UITextAttributeFont : [UIFont boldSystemFontOfSize:18],
UITextAttributeTextColor : [UIColor blackColor],
UITextAttributeTextShadowColor : [UIColor clearColor],
UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetZero],
};
#endif
}
[navigationBarAppearance setBackgroundImage:backgroundImage
forBarMetrics:UIBarMetricsDefault];
[navigationBarAppearance setTitleTextAttributes:textAttributes];
}
#pragma mark - delegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
[[self cyl_tabBarController] updateSelectionStatusIfNeededForTabBarController:tabBarController shouldSelectViewController:viewController];
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectControl:(UIControl *)control {
UIView *animationView;
if ([control cyl_isTabButton]) {
//更改红标状态
if ([[self cyl_tabBarController].selectedViewController cyl_isShowTabBadgePoint]) {
[[self cyl_tabBarController].selectedViewController cyl_removeTabBadgePoint];
} else {
[[self cyl_tabBarController].selectedViewController cyl_showTabBadgePoint];
}
animationView = [control cyl_tabImageView];
}
// 即使 PlusButton 也添加了点击事件,点击 PlusButton 后也会触发该代理方法。
if ([control cyl_isPlusButton]) {
UIButton *button = CYLExternPlusButton;
animationView = button.imageView;
}
if ([self cyl_tabBarController].selectedIndex % 2 == 0) {
[self addScaleAnimationOnView:animationView repeatCount:1];
} else {
[self addRotateAnimationOnView:animationView];
}
}
//缩放动画
- (void)addScaleAnimationOnView:(UIView *)animationView repeatCount:(float)repeatCount {
//需要实现的帧动画,这里根据需求自定义
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"transform.scale";
animation.values = @[@1.0,@1.3,@0.9,@1.15,@0.95,@1.02,@1.0];
animation.duration = 1;
animation.repeatCount = repeatCount;
animation.calculationMode = kCAAnimationCubic;
[animationView.layer addAnimation:animation forKey:nil];
}
//旋转动画
- (void)addRotateAnimationOnView:(UIView *)animationView {
// 针对旋转动画,需要将旋转轴向屏幕外侧平移,最大图片宽度的一半
// 否则背景与按钮图片处于同一层次,当按钮图片旋转时,转轴就在背景图上,动画时会有一部分在背景图之下。
// 动画结束后复位
CGFloat oldZPosition = animationView.layer.zPosition;//0
animationView.layer.zPosition = 65.f / 2;
[UIView animateWithDuration:0.32 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
animationView.layer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
} completion:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.70 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveEaseOut animations:^{
animationView.layer.transform = CATransform3DMakeRotation(2 * M_PI, 0, 1, 0);
} completion:nil];
});
}
@end