关于启动动画设计思路

app启动时加载的是LaunchImage。这个是一张静态图。现在好多应用启动时是动态的。这里介绍一下自己在做这种动画时的一种方案。
启动图依然是加载的,只不过是一闪而过,这时候我想到的是拿到当前的LaunchImage图片,然后进行处理,造成一种改变了LaunchImage动画的假象。
这个思路面临的第一个问题就是如何拿到当前的LaunchImage图片。众所周知,LaunchImage也是一套图,在不同设备上启动用得真实图片大小是不同的。可以通过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;
}

拿到之后就可以进行处理了

// 启动完成执行该方法
- (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) {
            // 动画执行完毕后一定要记得移除
            [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

可能会有疑问。为什么要取当前设备的size,而不是直接使用图片的尺寸。是因为通过上面方法取得的是实际用的图片由于@2x和@3x的关系,取得图片尺寸和实际有差别.这样就要取得实际的尺寸,进行切割.否则图片会变形.

你可能感兴趣的:(关于启动动画设计思路)