iOS 启动页动画OC+Swift

原理是在keywindow上加一个imgeview:demo
OC 代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = [[ViewController alloc] init];
    [self.window makeKeyAndVisible];
    [self customLaunchImageView];
    return YES;
}

- (void)customLaunchImageView
    {
        UIImageView *launchImageView = [[UIImageView alloc] initWithFrame:self.window.bounds];
        launchImageView.image = [UIImage imageNamed:@"launchScreen_bg"];
        [self.window addSubview:launchImageView];
        [self.window bringSubviewToFront:launchImageView];
        
        
        UIImageView *img1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width * 0.7, [UIScreen mainScreen].bounds.size.width * 0.7)];
        img1.center = CGPointMake(self.window.center.x,[UIScreen mainScreen].bounds.size.height *0.36);
        img1.image = [UIImage imageNamed:@"launchScreen_b"];
        [launchImageView addSubview:img1];
        [self addAnimation:img1 cc:NO];
        
        UIImageView *img2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width * 0.7, [UIScreen mainScreen].bounds.size.width * 0.7)];
        img2.center = CGPointMake(self.window.center.x,[UIScreen mainScreen].bounds.size.height *0.36);
        img2.image = [UIImage imageNamed:@"launchScreen_c"];
        [launchImageView addSubview:img2];
        [self addAnimation:img2 cc:YES];
        
 

        
        
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:1.2 animations:^{

            } completion:^(BOOL finished) {
                [launchImageView removeFromSuperview];
                //do something
            }];
        });
    }
- (void)addAnimation:(UIView *)view cc:(BOOL)cc
    {
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        if (cc)
        {
            animation.fromValue = [NSNumber numberWithFloat:0.f];
            animation.toValue = [NSNumber numberWithFloat: M_PI *2];
        }
        else
        {
            animation.fromValue = [NSNumber numberWithFloat: M_PI *2];
            animation.toValue = [NSNumber numberWithFloat:0.f];
        }
        
        animation.duration = 3;
        animation.autoreverses = NO;
        animation.fillMode = kCAFillModeForwards;
        animation.repeatCount = MAXFLOAT;
        [view.layer addAnimation:animation forKey:nil];
    }

SWift代码

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        self.window?.makeKeyAndVisible()
        
        let launchImageView = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
        launchImageView.image = UIImage.init(named: "launchScreen_bg")
        launchImageView.backgroundColor = UIColor.red
        self.window?.addSubview(launchImageView)
        self.window?.bringSubview(toFront: launchImageView)
        
        
        let img_b = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width * 0.7, height: UIScreen.main.bounds.size.width * 0.7))
        img_b.center = CGPoint.init(x: (self.window?.center.x)!, y: UIScreen.main.bounds.size.height * 0.36)
        img_b.image = UIImage.init(named: "launchScreen_b")
        launchImageView.addSubview(img_b)
        self.addAnimation(view: img_b, isOrder: false)
        
        let img_c = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width * 0.7, height: UIScreen.main.bounds.size.width * 0.7))
        img_c.center = CGPoint.init(x: (self.window?.center.x)!, y: UIScreen.main.bounds.size.height * 0.36)
        img_c.image = UIImage.init(named: "launchScreen_c")
        launchImageView.addSubview(img_c)
        self.addAnimation(view: img_c, isOrder: true)
        
        
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            
            UIView.animate(withDuration: 0.5, animations: {
                
            }, completion: { (finished:Bool) in
                
                launchImageView.removeFromSuperview()
                // do something
  
                
            })
            
        }
        
        return true
    }

    func addAnimation(view:UIView,isOrder:Bool)
    {
        let animation = CABasicAnimation.init(keyPath: "transform.rotation.z")
        if isOrder {
            animation.fromValue = 0.0
            animation.toValue = Double.pi * 2
        }
        else
        {
            animation.fromValue = Double.pi * 2
            animation.toValue = 0.0
        }
        animation.duration = 4
        animation.autoreverses = false
        animation.fillMode = kCAFillModeForwards
        animation.repeatCount = 2
        view.layer.add(animation, forKey: nil)
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容