2017-03-24 24 views
0

我每次運行這個迅速遊樂場(在Xcode中),我得到的最後一行說一個錯誤:無法建造,因爲它沒有可訪問initialisers如何使這個UIButton的動作正確運行?

「PlaygroundView」。

我也有第4行的說法另一個錯誤:

類的PlaygroundView'沒有初始化。

import UIKit 
import PlaygroundSupport 

class PlaygroundView:UIViewController { 

var introImage:UIImageView 
var introButton:UIButton 

override func loadView() { 

    print("workspace started running.") 

    //Main Constant and Variable Declarations/General Setup 

    let mainView = UIView() 

    //Main View Modifications & styling 

    mainView.backgroundColor = UIColor.init(colorLiteralRed: 250, green: 250, blue: 250, alpha: 1) 

    func addItem(item: UIView) { 

     mainView.addSubview(item) 

    } 

    //Sub-Element Constant and Variable Declarations 

    introImage = UIImageView(frame: CGRect(x: 87.5, y: -300, width: 200, height: 200)) 
    introImage.layer.borderColor = UIColor.black.cgColor 
    introImage.layer.borderWidth = 4 
    introImage.alpha = 0 
    introImage.transform = CGAffineTransform(rotationAngle: -90.0 * 3.14/180.0) 
    introImage.image = UIImage(named: "profile_pic.png") 
    introImage.image?.accessibilityFrame = introImage.frame 
    introImage.layer.cornerRadius = 100 
    addItem(item: introImage) 

    introButton = UIButton(frame: CGRect(x: 87.5, y: 900, width: 200, height: 30)) 
    introButton.alpha = 0 
    introButton.setTitle("Tap to meet me", for: .normal) 
    introButton.titleLabel?.font = UIFont(name: "Avenir Book", size: 20) 
    introButton.backgroundColor = UIColor.black 
    introButton.layer.cornerRadius = 15 
    introButton.layer.borderWidth = 5 

    addItem(item: introButton) 

    introButton.addTarget(self, action: #selector(self.introButtonAction), for: .touchDown) 

    UIView.animate(withDuration: 1.5, delay: 1, options: .curveEaseInOut, animations: { 

     self.introImage.frame = CGRect(x: 87.5, y: 175, width: 200, height: 200) 
     self.introButton.frame = CGRect(x: 87.5, y: 400, width: 200, height: 30) 

     self.introImage.transform = CGAffineTransform(rotationAngle: 0.0 * 3.14/180.0) 

     self.introImage.alpha = 1 
     self.introButton.alpha = 1 

    }) { (mainAnimationComped) in 

     if mainAnimationComped == true { 

      print("introduction animation comped.") 

     } 

    } 


} 

//User Interface Actions 

func introButtonAction() { 

    print("user interacted with user interface") 

} 

} 

PlaygroundPage.current.liveView = PlaygroundView() 

我將如何解決這個問題?

回答

0

這是因爲在雨燕,你需要之前初始化的變量在代碼中使用它們了。在另一方面,因爲在這種情況下,你在你的函數明確設置它們,你還不如隱式聲明它們展開自選

var introImage:UIImageView! 
var introButton:UIButton! 

,這不應該在你的代碼的其他任何變動工作

0

添加? introImage和introButton

var introImage:UIImageView? 
var introButton:UIButton? 
+0

謝謝!我完全按照你所說的做了,而且它很有效。還有這個網站:https://www.ralfebert.de/snippets/ios/uikit-playground/UIButton/,它幫助我完成了我的項目。 –