美文网首页
iOS 启动页带动画

iOS 启动页带动画

作者: 一个没有记忆的梦 | 来源:发表于2018-03-06 17:56 被阅读0次

APP在启动的时候有的是静态图有的是动图,那怎么设置动图呢?本文主要介绍从网络上获取动图,这样可以只更改服务器上的图片而不需要更改代码。


cc.gif

1、Objective—C
首先需要从网络上获取图片并展示,此处用到SDWebImage三方库;再添加一个点击图片的手势,以便于点击图片跳到对应的页面。

- (void)loadImage {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [self.view addSubview:imageView];
    self.gifVC = imageView;
    //点击imageview的事件 --- 点击动画的事件
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(adImageViewClick)];
    imageView.userInteractionEnabled = YES;//打开imageview的用户交互,否则点击无效
    [imageView addGestureRecognizer:tap];
    self.gifVC = imageView;
    //从网上下载gif动画
    __weak typeof(self) weakSelf = self;
    NSString *urlStr = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1520337949419&di=368c52f1e0d4fce2d53d7f5ee58869e3&imgtype=0&src=http%3A%2F%2Fimg.mp.sohu.com%2Fupload%2F20170721%2Fbcb112a39e0b4596968ad39ce88a6ef9.png";
    NSURL *url = [NSURL URLWithString:urlStr];
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:url options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
        self.gifImage = image;
        [weakSelf performSelectorOnMainThread:@selector(updateImageViewImage) withObject:nil waitUntilDone:NO];
        //        [[SDImageCache sharedImageCache] storeImage:image forKey:urlStr toDisk:YES];
    }];
}

下面两个方法看注释

/*
 * 单击手势事件 --- 点击广告的事件
 */
- (void)adImageViewClick {
    
}
/*
 * 下载完成之后更新图片
 */
- (void)updateImageViewImage {
    self.gifVC.image = self.gifImage;
    [self addClickedInfo];//此方法添加按钮,用于点击是跳过,倒计时
}
//添加按钮
- (void)addClickedInfo {
    UIButton *skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
    skipButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 15 - 70, 20, 70, 35);
    [skipButton setTitle:[NSString stringWithFormat:@"%zd 跳过", self.number] forState:UIControlStateNormal];
    [skipButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    skipButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [skipButton addTarget:self action:@selector(skipButtonClick) forControlEvents:UIControlEventTouchUpInside];
    skipButton.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    skipButton.layer.cornerRadius = 35 / 2;
    [self.view addSubview:skipButton];
    self.skipButton = skipButton;
}
//按钮的点击事件
- (void)skipButtonClick {
    [self removeTimer];// 移除定时器
    ViewController *homeVC = [[ViewController alloc] init];
    //    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:homeVC];
    [UIApplication sharedApplication].keyWindow.rootViewController = homeVC;
    CATransition *anim = [CATransition animation];
    anim.duration = 0.5;
    anim.type = @"fade";
    [[UIApplication sharedApplication].keyWindow.layer addAnimation:anim forKey:nil];
}

然后创建一个定时器用于倒计时

- (void)controlTimer {
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeOut) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer;
}

/**
 * 设置超时,然后退出当前控制器回到首页
 */
- (void)timeOut {
    self.number--;
    if (self.number > 0) {
        [self.skipButton setTitle:[NSString stringWithFormat:@"%zd 跳过", self.number] forState:UIControlStateNormal];
    } else {
        [self skipButtonClick];
    }
}

/**
 *  移除定时器
 */
- (void)removeTimer {
    // 停止定时器
    [self.timer invalidate];
    self.timer = nil;
}

最后是调用

 self.number = 5;//设置时间
 [self controlTimer];//倒计时
 [self loadImage];//加载图片

2、Swift
先声明几个全局变量,以便于在全局中使用方便控制。

var time = 5;
var imageView:UIImageView! = nil;
var downImage:UIImage!;
var skipButton: UIButton!;
var timer:Timer! = nil;

在viewDidLoad()中调用方法

loadTimer();//加载定时器的方法
 loadImage();//加载图片的方法
//加载图片
    func loadImage() {
        self.imageView = UIImageView();//初始化UIImageView
        self.imageView.frame = CGRect(x:0, y:0, width:UIScreen.main.bounds.size.width, height:UIScreen.main.bounds.size.height);
        self.view.addSubview(self.imageView);
        let urlStr = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1520337949419&di=368c52f1e0d4fce2d53d7f5ee58869e3&imgtype=0&src=http%3A%2F%2Fimg.mp.sohu.com%2Fupload%2F20170721%2Fbcb112a39e0b4596968ad39ce88a6ef9.png";
        let url = URL(string:urlStr);
        weak var weakSelf = self;
        //  使用SDWebImage预先下载该图片,需要时从本地读取
        SDWebImageDownloader.shared().downloadImage(with: url, options: SDWebImageDownloaderOptions(rawValue: 1), progress: { (_, _) in}) { (image: UIImage?, data: Data?, error: Error?, finished: Bool?) in
            if image != nil {
                self.downImage = image;
                weakSelf?.performSelector(onMainThread: #selector(self.updataImageViewImage), with: nil, waitUntilDone: false);
            }
        }
    }
//更新图片 @objc需要添加
    @objc func updataImageViewImage() {
        self.imageView.image = self.downImage;
        addClickedButton();//添加跳过按钮
    }
    //创建按钮并添加到视图上
    func addClickedButton() {
        self.skipButton = UIButton(type: .custom);
        self.skipButton.frame = CGRect(x: UIScreen.main.bounds.size.width-15-70, y: 20, width: 60, height: 30);
        self.skipButton.setTitle(String(format: "%2d跳过", self.time), for: UIControlState.normal);
        self.skipButton.setTitleColor(UIColor.red, for: UIControlState.normal);
        self.skipButton.titleLabel?.font = UIFont.systemFont(ofSize: 13);
        self.skipButton.addTarget(self, action: #selector(skipButtonAction), for: UIControlEvents.touchUpInside);
        self.skipButton.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.4);
        self.skipButton.layer.cornerRadius = 10;
        self.view.addSubview(self.skipButton);
    }

    //点击跳过按钮事件------按钮的点击事件
    @objc func skipButtonAction() {
        removeTimer();
        let vc = ViewController();
        UIApplication.shared.keyWindow?.rootViewController = vc;
        let animation = CATransition();
        animation.duration = 0.5;
        animation.type = "fade";
        UIApplication.shared.keyWindow?.layer.add(animation, forKey: nil);
    }

创建定时器的方法

//加载定时器
    func loadTimer() {
        self.timer = Timer.scheduledTimer(timeInterval: TimeInterval(1.0), target: self, selector: #selector(timeOut), userInfo: nil, repeats: true);
    }
    
    @objc func timeOut() {
        self.time = self.time-1;
        if self.time > 0 {
            self.skipButton.setTitle(String(format: "%2d跳过", self.time), for: UIControlState.normal);
        } else {
            skipButtonAction();
        }
    }
    //删除定时器
    func removeTimer() {
        self.timer.invalidate();
        self.timer = nil;
    }

以上就是设置启动页带动画的所有代码!!!

相关文章

  • iOS适配启动页

    iOS适配启动页 iOS适配启动页

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

    1、单纯的延长启动页 2、启动页动画

  • iOS代码添加视图约束

    项目要做这样一个效果的启动页。 考虑到版本号是会不断变更的,因此采用动画效果启动页,让版本号动态加载iOS启动页动...

  • iOS开发_启动页动画

    【作者前言】:13年入圈,分享些本人工作中遇到的点点滴滴那些事儿,17年刚开始写博客,高手勿喷!以分享交流为主,欢...

  • iOS 启动页带动画

    APP在启动的时候有的是静态图有的是动图,那怎么设置动图呢?本文主要介绍从网络上获取动图,这样可以只更改服务器上的...

  • iOS启动页动画效果

    最近项目中要在启动页增加版本号,因为版本号是不断的改变,所以要动态实现把它加到启动页上;在XCode上面配置的La...

  • Flutter 启动页

    启动页 IOS启动页,在ios/Runner/Assets.xcassets/LaunchImage.images...

  • [iOS]Swift-启动页动画

    从开发者角度讲app启动当然越快越好,但是有时候总有些奇葩需求。比如,让启动页故意延时、在启动页上加个动画等等。下...

  • iOS LaunchImage启动页 标准尺寸

    iOS LaunchImage启动页 标准尺寸

  • iOS 启动页动画OC+Swift

    原理是在keywindow上加一个imgeview:demoOC 代码 SWift代码

网友评论

      本文标题:iOS 启动页带动画

      本文链接:https://www.haomeiwen.com/subject/clerfftx.html