【问题标题】:Adding GIF Background to viewController with Swift使用 Swift 将 GIF 背景添加到 viewController
【发布时间】:2015-09-21 21:21:54
【问题描述】:

我想用 swift 将 GIF 背景添加到我的登陆屏幕,所以我在网上找到了这段代码,但是当我尝试在带有 Swift 2.0 的 XCODE 7 上使用它时

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
}


}

我收到以下错误:

【问题讨论】:

  • 已经试过了,还是不行

标签: ios xcode swift swift2 xcode7


【解决方案1】:

试试下面的代码:

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
}


}

【讨论】:

  • @bpolat 太好了有没有办法在启动画面中完成这项工作?
  • 这很棒。有什么方法可以让它在显示屏上重复/平铺?
猜你喜欢
  • 2016-01-09
  • 1970-01-01
  • 2020-12-01
  • 1970-01-01
  • 2017-11-18
  • 2015-05-29
  • 1970-01-01
  • 2019-08-07
  • 1970-01-01
相关资源
最近更新 更多