iOS 延迟启动页以及启动页动画

2019-03-28  本文已影响0人  叩首问路梦码为生

1、单纯的延长启动页


UIImageView *lanuchImgV;
  UIImageView *loadImgV;

-(void)delayAnimotion{
    // 注意:一定要在[self.window makeKeyAndVisible]之后添加imageView
    lanuchImgV = [[UIImageView alloc] initWithFrame:self.window.bounds];
    lanuchImgV.image = [self launchImage];
    [self.window addSubview:lanuchImgV];
    [self.window bringSubviewToFront:lanuchImgV];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.7 animations:^{
            lanuchImgV.alpha = 0.05;
            lanuchImgV.transform = CGAffineTransformMakeScale(1.5, 1.5);
        } completion:^(BOOL finished) {
            [lanuchImgV removeFromSuperview];
        }];
        
    });

// 获取启动页图片
- (UIImage *)launchImage
{
    UIImage *lauchImage = nil;
    NSString *viewOrientation = nil;
    CGSize viewSize = [UIScreen mainScreen].bounds.size;
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
        viewOrientation = @"Landscape";
    } else {
        viewOrientation = @"Portrait";
    }
    NSArray *imagesDictionary = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
    for (NSDictionary *dict in imagesDictionary) {
        
        CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
        if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]) {
            lauchImage = [UIImage imageNamed:dict[@"UILaunchImageName"]];
        }
    }
    return lauchImage;
}


}


2、启动页动画


-(void)animotion{
    // 注意:一定要在[self.window makeKeyAndVisible]之后添加imageView
    lanuchImgV = [[UIImageView alloc] initWithFrame:self.window.bounds];
    lanuchImgV.image = [self launchImage];
    [self.window addSubview:lanuchImgV];
    [self.window bringSubviewToFront:lanuchImgV];
    // 这里做了0.8秒的延迟执行,因为我发现如果不做延迟,动画执行会有卡顿现象,具体原因不明,有知道的烦请告知下
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self showLoadImg];
        //        [UIView animateWithDuration:1.2 animations:^{
        //            launchImageView.alpha = 0.0;
        //            launchImageView.transform = CGAffineTransformMakeScale(1.2, 1.2);
        //        } completion:^(BOOL finished) {
        //            [launchImageView removeFromSuperview];
        //        }];
    });
}

- (UIImage *)launchImage
{
    UIImage *lauchImage = nil;
    NSString *viewOrientation = nil;
    CGSize viewSize = [UIScreen mainScreen].bounds.size;
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
        viewOrientation = @"Landscape";
    } else {
        viewOrientation = @"Portrait";
    }
    NSArray *imagesDictionary = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
    for (NSDictionary *dict in imagesDictionary) {
        
        CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
        if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]) {
            lauchImage = [UIImage imageNamed:dict[@"UILaunchImageName"]];
        }
    }
    return lauchImage;
}


- (void)showLoadImg {
    loadImgV = [[UIImageView alloc] init];
    loadImgV.backgroundColor = [UIColor clearColor];
    if (Is_Iphone_X) {
        loadImgV.frame = CGRectMake(ScreenWidth/2-78, 433, 155, 12);
    }else {
        if (ScreenHeight == 480) {
            loadImgV.frame = CGRectMake(ScreenWidth/2-78, 243, 155, 12);
        }else if (ScreenHeight == 568) {
            loadImgV.frame = CGRectMake(ScreenWidth/2-78, 290, 155, 12);
        }else if (ScreenHeight == 1334/2) {
            loadImgV.frame = CGRectMake(ScreenWidth/2-78, 346, 155, 12);
        }else if (ScreenHeight == 2208/3) {
            loadImgV.frame = CGRectMake(ScreenWidth/2-78, 390, 155, 12);
        }
    }
    loadImgV.animationImages = [self animationImages]; //获取Gif图片列表
    loadImgV.animationDuration = 0.9;     //执行一次完整动画所需的时长
    loadImgV.animationRepeatCount = 1;  //动画重复次数
    [loadImgV startAnimating];
    [self.window addSubview:loadImgV];
    [self performSelector:@selector(hideLoadImg) withObject:self afterDelay:1.5];
}


#pragma mark -- 动画
- (NSMutableArray *)animationImages {
    NSMutableArray *imagesArr = [NSMutableArray array];
    for (int i = 3; i < 31; i++) {
        NSString *strImg = [NSString stringWithFormat:@"new_loading_%02d.png",i];
        NSString *path = [[NSBundle mainBundle] pathForResource:strImg ofType:nil];
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        [imagesArr addObject:image];
    }
    return imagesArr;
}


- (void)hideLoadImg {
    loadImgV.hidden = YES;

    [UIView animateWithDuration:0.7 animations:^{
        lanuchImgV.alpha = 0.05;
        lanuchImgV.transform = CGAffineTransformMakeScale(1.5, 1.5);
    } completion:^(BOOL finished) {
        [loadImgV removeFromSuperview];
        [lanuchImgV removeFromSuperview];
    }];
    
}


上一篇下一篇

猜你喜欢

热点阅读