從體驗(yàn)方面來看忠荞,一個(gè)好的App的啟動(dòng)圖會(huì)給軟件增色不少蒋歌,給App加啟動(dòng)圖,成本也不大委煤,這篇文章堂油,就來實(shí)現(xiàn)一下啟動(dòng)圖動(dòng)畫效果,東西不多碧绞,僅App啟動(dòng)圖+動(dòng)畫府框,下面馬上開始
啟動(dòng)圖
分兩步
第一步,新建一個(gè)工程讥邻,里面會(huì)有LaunchScreen.storyboard這個(gè)文件迫靖,他就是為了方便我們?cè)O(shè)置啟動(dòng)圖的院峡。選擇一張合適的尺寸的圖片,拖入到項(xiàng)目中系宜,建議最好用PNG格式的照激。
在LaunchScreen.storyboard中拖入一個(gè)UIImageView控件,做好約束盹牧,然后設(shè)置剛剛拖入的圖片
第二步俩垃,設(shè)置設(shè)置啟動(dòng)圖見下圖
這樣啟動(dòng)圖就設(shè)置好了
啟動(dòng)動(dòng)畫
LaunchScreen.storyboard也就是啟動(dòng)圖是最先展示給用戶的,然后才能是啟動(dòng)動(dòng)畫汰寓。我們知道軟件啟動(dòng)完成就會(huì)走Appdelegate的代理方法
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
所以只要在這個(gè)方法中實(shí)現(xiàn)動(dòng)畫效果就可以了口柳。
思路:展示啟動(dòng)圖后,馬上展示另外一張圖片有滑,并且這張圖片在5秒內(nèi)放大1.5倍跃闹,然后在2秒內(nèi)消失。右上角放一個(gè)跳過按鈕毛好,顯示倒計(jì)時(shí)
寫代碼的地方知道了辣卒,思路也知道了,直接上代碼
先定義一個(gè)宏
#define timeIntence 5.0//圖片放大的時(shí)間
添加按鈕和圖片
//使主窗口顯示到屏幕的最前端
[self.window makeKeyAndVisible];
//獲取中心點(diǎn)的xy坐標(biāo)
float y = [UIScreen mainScreen].bounds.size.height/2;
float x = [UIScreen mainScreen].bounds.size.width/2;
//創(chuàng)建展示動(dòng)畫的imageView
self.imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.imageView.center = CGPointMake(x, y);
self.imageView.userInteractionEnabled = YES;
self.imageView.image = [UIImage imageNamed:@"啟動(dòng)圖.jpg"];
[self.window addSubview:self.imageView];
[self.window bringSubviewToFront:self.imageView];
//添加一個(gè)跳過按鈕
self.skipButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
self.skipButton.frame = CGRectMake(2*x - 80, 40, 70, 25);
self.skipButton.layer.cornerRadius = 12.5;
self.skipButton.titleLabel.font = [UIFont systemFontOfSize:13];
[self.skipButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
self.skipButton.backgroundColor = [UIColor grayColor];
[self.skipButton addTarget:self action:@selector(skipAction:) forControlEvents:(UIControlEventTouchUpInside)];
[self.window addSubview:self.skipButton];
按鈕倒計(jì)時(shí)用到了一個(gè)庫睛榄,需要引入頭文件
#import<libkern/OSAtomic.h>
實(shí)現(xiàn)按鈕的倒計(jì)時(shí)
//跳過按鈕的倒計(jì)時(shí)
__block int32_t timeOutCount=timeIntence;
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1ull * NSEC_PER_SEC, 0);
dispatch_source_set_event_handler(timer, ^{
OSAtomicDecrement32(&timeOutCount);
if (timeOutCount == 0) {
NSLog(@"timersource cancel");
dispatch_source_cancel(timer);
}
[self.skipButton setTitle:[NSString stringWithFormat:@"%d 跳過",timeOutCount+1] forState:(UIControlStateNormal)];
});
dispatch_source_set_cancel_handler(timer, ^{
NSLog(@"timersource cancel handle block");
});
dispatch_resume(timer);
圖片放大和消失動(dòng)畫
//圖片放大動(dòng)畫
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:timeIntence];
self.imageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.5f, 1.5f);
[UIView commitAnimations];
//圖片消失動(dòng)畫
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeIntence * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:2 animations:^{
self.imageView.alpha = 0;
self.skipButton.alpha = 0;
}];
});
//所有動(dòng)畫完成荣茫,刪除圖片和按鈕
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((timeIntence + 2.0) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.imageView removeFromSuperview];
[self.skipButton removeFromSuperview];
});
//點(diǎn)擊跳過按鈕,刪除圖片和按鈕
-(void)skipAction:(UIButton*)sender{
[self.imageView removeFromSuperview];
[self.skipButton removeFromSuperview];
}
到此所有代碼都寫完了场靴,是不是很簡(jiǎn)單,再附上demo下載地址
點(diǎn)擊下載DEMO