app啟動時加載的是LaunchImage漏健。這個是一張靜態(tài)圖。現(xiàn)在好多應(yīng)用啟動時是動態(tài)的秸应。這里介紹一下自己在做這種動畫時的一種方案虑凛。
啟動圖依然是加載的,只不過是一閃而過软啼,這時候我想到的是拿到當(dāng)前的LaunchImage圖片桑谍,然后進(jìn)行處理,造成一種改變了LaunchImage動畫的假象祸挪。
這個思路面臨的第一個問題就是如何拿到當(dāng)前的LaunchImage圖片锣披。眾所周知,LaunchImage也是一套圖贿条,在不同設(shè)備上啟動用得真實圖片大小是不同的盈罐。可以通過info的信息拿到真實啟動圖的Name闪唆。
// 獲取實際使用的LaunchImage圖片
- (NSString *)getLaunchImageName {
CGSize viewSize = self.window.bounds.size;
// 豎屏
NSString *viewOrientation = @"Portrait";
NSString *launchImageName = nil;
NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
for (NSDictionary* dict in imagesDict) {
CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]) {
launchImageName = dict[@"UILaunchImageName"];
}
}
return launchImageName;
}
拿到之后就可以進(jìn)行處理了
// 啟動完成執(zhí)行該方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIView *backView = [[UIView alloc] initWithFrame:self.window.frame];
NSString *launchImageName = [self getLaunchImageName];
UIImage *launchImage = [UIImage imageNamed:launchImageName];
UIImageView *launchView = [[UIImageView alloc] initWithImage:launchImage];
launchView.frame = self.window.bounds;
[backView addSubview:launchView];
[self.window addSubview:backView];
UIImageView *topImageView = [[UIImageView alloc] initWithImage:[UIImage clipImage:launchImage withRect:CGRectMake(0, 0, mainScreenSize.width, mainScreenSize.height * 0.5)]];
topImageView.contentMode = UIViewContentModeScaleToFill;
topImageView.width = self.window.width;
topImageView.height = self.window.height * 0.5;
UIImageView *bottomImageView = [[UIImageView alloc] initWithImage:[UIImage clipImage:launchImage withRect:CGRectMake(0, mainScreenSize.height * 0.5, mainScreenSize.width, mainScreenSize.height * 0.5)]];
bottomImageView.y = self.window.height * 0.5;
bottomImageView.width = self.window.width;
bottomImageView.height = self.window.height * 0.5;
[backView addSubview:topImageView];
[backView addSubview:bottomImageView];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
launchView.image = nil;
[launchView removeFromSuperview];
[UIView animateWithDuration:1 animations:^{
topImageView.y = -topImageView.height;
bottomImageView.y = bottomImageView.height * 2;
} completion:^(BOOL finished) {
// 動畫執(zhí)行完畢后一定要記得移除
[backView removeFromSuperview];
}];
});
}
剪切圖片的方法很簡單
+ (UIImage *)clipImage:(UIImage *)image withRect:(CGRect)rect {
CGImageRef refImage = CGImageCreateWithImageInRect(image.CGImage, rect);
UIImage *newImage = [UIImage imageWithCGImage:refImage];
CGImageRelease(refImage);
return newImage;
}
在上面的mainScreenSize是我定義的一個宏盅粪。
#define mainScreenSize [[UIScreen mainScreen] currentMode].size
可能會有疑問。為什么要取當(dāng)前設(shè)備的size悄蕾,而不是直接使用圖片的尺寸票顾。是因為通過上面方法取得的是實際用的圖片由于@2x和@3x的關(guān)系,取得圖片尺寸和實際有差別.這樣就要取得實際的尺寸,進(jìn)行切割.否則圖片會變形.