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

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;
}
以上就是设置启动页带动画的所有代码!!!
网友评论