2015-09-21 2 views
0

Я хотел бы добавить GIF фона в моей посадки экран с быстрым, так что я нашел этот код в Интернете, но когда я пытаюсь использовать его на Xcode 7 с Swift 2.0Добавление GIF фона в ViewController с Swift

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 получить следующее сообщение об ошибке:

I have tried the suggested Edit from XCODE which is add "!" after the "gif" but this didn't work also

+0

TRIE d, что уже, не работает –

ответ

2

Try следующий код:

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 
} 


} 
+0

Спасибо, отлично поработал :) –

+0

@bpolat Это хорошо, есть ли способ сделать эту работу в заставке? – Manolo

+0

Это замечательно. Любой способ сделать это повторением/черепицей через дисплей? – theflarenet

Смежные вопросы