Adding GIF Background to viewController with Swift

守給你的承諾、 提交于 2021-02-07 04:36:17

问题


I wanted to add GIF Background to my landing screen with swift, so I found this code online but when I try to use it on XCODE 7 with Swift 2.0

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {

    super.viewDidLoad()

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

    var webViewBG = UIWebView(frame: self.view.frame)
    webViewBG.loadData(gif, MIMEType: "image/gif", textEncodingName: nil, baseURL: nil)
    webViewBG.userInteractionEnabled = false;
    self.view.addSubview(webViewBG)

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

    var welcomeLabel = UILabel(frame: CGRectMake(0, 100, self.view.bounds.size.width, 100))
    welcomeLabel.text = "WELCOME"
    welcomeLabel.textColor = UIColor.whiteColor()
    welcomeLabel.font = UIFont.systemFontOfSize(50)
    welcomeLabel.textAlignment = NSTextAlignment.Center
    self.view.addSubview(welcomeLabel)

    var loginBtn = UIButton(frame: CGRectMake(40, 360, 240, 40))
    loginBtn.layer.borderColor = UIColor.whiteColor().CGColor
    loginBtn.layer.borderWidth = 2
    loginBtn.titleLabel!.font = UIFont.systemFontOfSize(24)
    loginBtn.tintColor = UIColor.whiteColor()
    loginBtn.setTitle("Login", forState: UIControlState.Normal)
    self.view.addSubview(loginBtn)

    var signUpBtn = UIButton(frame: CGRectMake(40, 420, 240, 40))
    signUpBtn.layer.borderColor = UIColor.whiteColor().CGColor
    signUpBtn.layer.borderWidth = 2
    signUpBtn.titleLabel!.font = UIFont.systemFontOfSize(24)
    signUpBtn.tintColor = UIColor.whiteColor()
    signUpBtn.setTitle("Sign Up", forState: UIControlState.Normal)
    self.view.addSubview(signUpBtn)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return UIStatusBarStyle.LightContent
}


}

I get the following error :


回答1:


Try following code:

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {

    super.viewDidLoad()

    let filePath = NSBundle.mainBundle().pathForResource("videoName", 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 = "WELCOME"
    welcomeLabel.textColor = UIColor.whiteColor()
    welcomeLabel.font = UIFont.systemFontOfSize(50)
    welcomeLabel.textAlignment = NSTextAlignment.Center
    self.view.addSubview(welcomeLabel)

    let loginBtn = UIButton(frame: CGRectMake(40, 360, 240, 40))
    loginBtn.layer.borderColor = UIColor.whiteColor().CGColor
    loginBtn.layer.borderWidth = 2
    loginBtn.titleLabel!.font = UIFont.systemFontOfSize(24)
    loginBtn.tintColor = UIColor.whiteColor()
    loginBtn.setTitle("Login", forState: UIControlState.Normal)
    self.view.addSubview(loginBtn)

    let signUpBtn = UIButton(frame: CGRectMake(40, 420, 240, 40))
    signUpBtn.layer.borderColor = UIColor.whiteColor().CGColor
    signUpBtn.layer.borderWidth = 2
    signUpBtn.titleLabel!.font = UIFont.systemFontOfSize(24)
    signUpBtn.tintColor = UIColor.whiteColor()
    signUpBtn.setTitle("Sign Up", forState: UIControlState.Normal)
    self.view.addSubview(signUpBtn)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return UIStatusBarStyle.LightContent
}


}


来源:https://stackoverflow.com/questions/32704764/adding-gif-background-to-viewcontroller-with-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!