效果图如下:
实现代码
- (
void
)viewDidLoad {
[
super
viewDidLoad
];
self . view . backgroundColor = [ UIColor whiteColor ];
// 初始化 UIWebView 对象,添加 GIF 文件
UIWebView *webView = [[ UIWebView alloc ] initWithFrame : CGRectMake ( 0 , 250 , self . view . frame . size . width , 200 )];
[ self . view addSubview :webView];
NSString *filePath = [[ NSBundle mainBundle ] pathForResource : @"duck" ofType : @"gif" ];
NSData *gif = [ NSData dataWithContentsOfFile :filePath];
[webView loadData :gif MIMEType : @"image/gif" textEncodingName : @"" baseURL :[ NSURL URLWithString :filePath]];
webView. userInteractionEnabled = NO ;
// 创建一个灰色的蒙版,提升效果
UIView *filter = [[ UIView alloc ] initWithFrame : self . view . bounds ];
filter. backgroundColor = [ UIColor blackColor ];
filter. alpha = 0.3 ;
[ self . view addSubview :filter];
self . view . backgroundColor = [ UIColor whiteColor ];
// 初始化 UIWebView 对象,添加 GIF 文件
UIWebView *webView = [[ UIWebView alloc ] initWithFrame : CGRectMake ( 0 , 250 , self . view . frame . size . width , 200 )];
[ self . view addSubview :webView];
NSString *filePath = [[ NSBundle mainBundle ] pathForResource : @"duck" ofType : @"gif" ];
NSData *gif = [ NSData dataWithContentsOfFile :filePath];
[webView loadData :gif MIMEType : @"image/gif" textEncodingName : @"" baseURL :[ NSURL URLWithString :filePath]];
webView. userInteractionEnabled = NO ;
// 创建一个灰色的蒙版,提升效果
UIView *filter = [[ UIView alloc ] initWithFrame : self . view . bounds ];
filter. backgroundColor = [ UIColor blackColor ];
filter. alpha = 0.3 ;
[ self . view addSubview :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]];
});