如何在Swift中添加GIF背景的UIButtons?

问题描述:

我发现这个代码,登陆屏幕上添加GIF背景和伟大的工作,如何在Swift中添加GIF背景的UIButtons?

import UIKit 

class ViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let filePath = NSBundle.mainBundle().pathForResource("MovingClouds", ofType: "gif") 
    let gif = NSData(contentsOfFile: filePath!) 

    let webViewBG = UIWebView(frame: self.view.frame) 
    webViewBG.loadData(gif!, MIMEType: "image/gif", textEncodingName: "utf-8", baseURL: NSURL()) 

    webViewBG.userInteractionEnabled = false; 
    self.view.addSubview(webViewBG) 

    let filter = UIView() 
    filter.frame = self.view.frame 
    filter.backgroundColor = UIColor.blackColor() 
    filter.alpha = 0.05 
    self.view.addSubview(filter) 

    let welcomeLabel = UILabel(frame: CGRectMake(0, 100, self.view.bounds.size.width, 100)) 
    welcomeLabel.text = "KeepON" 
    welcomeLabel.textColor = UIColor.whiteColor() 
    welcomeLabel.font = UIFont(name: "Zapfino", size: 35) 
    welcomeLabel.textAlignment = NSTextAlignment.Center 
    self.view.addSubview(welcomeLabel) 
} 

这说明我的GIF的背景和我的标题,但是当我尝试添加在故事板的按钮在视图控制器他们不当我运行我的应用程序时不会出现,只有带标题的GIF背景,并且我不想使用代码添加按钮。

那么有什么想法? 谢谢

这是因为您在已经存在的视图顶部添加子视图。而不是addSubView,使用insertSubView(webViewBG,atIndex:0);

+0

谢谢,有关如何调整GiF的任何想法?因为它在自动布局ON时显得更小,谢谢 –

+0

这是一个网页视图。所以我不知道,是否有任何特定的原因,你想它作为一个Web视图?因为uiimageview支持动画,并且比使用网页浏览器更好。顺便说一句,如果这回答你的问题,请将其标记为答案,让人们知道。 – Knight0fDragon

+0

不,没有特别的原因,这是我发现的唯一工作解决方案,所以我必须选择我的GIF,因为我选择了一个正常的图像?并感谢我不知道的信息xD –