效果图如下:
实现代码
- (void)viewDidLoad
{
[superviewDidLoad];
self.view.backgroundColor= [UIColorwhiteColor];
// 初始化UIWebView对象,添加GIF文件
UIWebView *webView = [[UIWebViewalloc]initWithFrame:CGRectMake(0,250,self.view.frame.size.width,200)];
[self.viewaddSubview:webView];
NSString *filePath = [[NSBundlemainBundle]pathForResource:@"duck"ofType:@"gif"];
NSData *gif = [NSDatadataWithContentsOfFile:filePath];
[webView loadData:gifMIMEType:@"image/gif"textEncodingName:@""baseURL:[NSURLURLWithString:filePath]];
webView.userInteractionEnabled=NO;
// 创建一个灰色的蒙版,提升效果
UIView *filter = [[UIViewalloc]initWithFrame:self.view.bounds];
filter.backgroundColor= [UIColorblackColor];
filter.alpha=0.3;
[self.viewaddSubview:filter];
self.view.backgroundColor= [UIColorwhiteColor];
// 初始化UIWebView对象,添加GIF文件
UIWebView *webView = [[UIWebViewalloc]initWithFrame:CGRectMake(0,250,self.view.frame.size.width,200)];
[self.viewaddSubview:webView];
NSString *filePath = [[NSBundlemainBundle]pathForResource:@"duck"ofType:@"gif"];
NSData *gif = [NSDatadataWithContentsOfFile:filePath];
[webView loadData:gifMIMEType:@"image/gif"textEncodingName:@""baseURL:[NSURLURLWithString:filePath]];
webView.userInteractionEnabled=NO;
// 创建一个灰色的蒙版,提升效果
UIView *filter = [[UIViewalloc]initWithFrame:self.view.bounds];
filter.backgroundColor= [UIColorblackColor];
filter.alpha=0.3;
[self.viewaddSubview:filter];
}
注意:将gif图片放在跟controller平级的文件夹中,不要放在Assets.xcassets中
在AppDelegate中切换控制器:ViewController有webView的控制器,XZMainViewController首页控制器
self.window = [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];
[self.windowmakeKeyAndVisible];
self.window.rootViewController = [[ViewControlleralloc]init];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
self.window.rootViewController = [[UINavigationControlleralloc]initWithRootViewController:[[XZMainViewControlleralloc]init]];
});
本文介绍如何在iOS应用中使用UIWebView加载本地GIF文件,并通过添加灰色蒙版增强视觉效果。此外,还提供了在AppDelegate中切换包含UIWebView的控制器到其他控制器的方法。
6408

被折叠的 条评论
为什么被折叠?



