2017-05-24 163 views
-1

我想知道如果我可以使用for代碼循環。請原諒我,我知道這是一個蹩腳的問題,但我是新來的迅速。希望你能幫我這裏,夥計們!我可以使用'for'循環嗎?

非常感謝大家!

代碼:

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Background color 

    let kolorTla = UIColor(red: 0/255.0, green: 66/255.0, blue: 132/255.0, alpha: 1.0) 
    view.backgroundColor = kolorTla 

    // Icons border 

    ramka.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor 
    ramka.layer.cornerRadius = 5.0 
    ramka.layer.borderWidth = 3 


    // Image 

    skill1.image = UIImage(named: "english") 

    // Image border 

    skill1.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor 
    skill1.layer.cornerRadius = 5.0 
    skill1.layer.borderWidth = 3 
    skill1.contentMode = .scaleAspectFit 

    // Image 

    skill2.image = UIImage(named: "literature") 

    // Image border 

    skill2.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor 
    skill2.layer.cornerRadius = 5.0 
    skill2.layer.borderWidth = 3 
    skill2.contentMode = .scaleAspectFit 

    // Image 

    skill3.image = UIImage(named: "idea1") 

    // Image border 

    skill3.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor 
    skill3.layer.cornerRadius = 5.0 
    skill3.layer.borderWidth = 3 
    skill3.contentMode = .scaleAspectFit 
+0

這個方法我是一個總的斯威夫特無知,但讓我給您一個建議。忘記它是Swift;假裝它是你熟悉的其他語言。如果你使用其他語言的for循環(或者任何類型的循環),你可以在Swift中使用for循環。 –

回答

1

您可以在某種程度上。你只需要定義一個你的項目的數組並循環它們。不知道它是否可以爲您節省大量代碼,但確實使它更容易理解。

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Background color 
    let kolorTla = UIColor(red: 0/255.0, green: 66/255.0, blue: 132/255.0, alpha: 1.0) 
    view.backgroundColor = kolorTla 

    // Icons border 
    ramka.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor 
    ramka.layer.cornerRadius = 5.0 
    ramka.layer.borderWidth = 3 

    // Set Image 
    skill1.image = UIImage(named: "english") 
    skill2.image = UIImage(named: "literature") 
    skill3.image = UIImage(named: "idea1") 

    // Set Image border 
    for skill in [skill1, skill2, skill3] { 
     skill.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor 
     skill.layer.cornerRadius = 5.0 
     skill.layer.borderWidth = 3 
     skill.contentMode = .scaleAspectFit 
    } 
} 

至少在我看來,更好的方法是創建一個簡單的函數來處理這個問題。

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Background color 
    let kolorTla = UIColor(red: 0/255.0, green: 66/255.0, blue: 132/255.0, alpha: 1.0) 
    view.backgroundColor = kolorTla 

    // Icons border 
    ramka.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor 
    ramka.layer.cornerRadius = 5.0 
    ramka.layer.borderWidth = 3 
    // Set Images 
    setupImageView(imageView: skill1, imageName: "english") 
    setupImageView(imageView: skill2, imageName: "literature") 
    setupImageView(imageView: skill3, imageName: "idea1") 
} 

func setupImageView(imageView: UIImageView, imageName: String) { 
    // Set Image 
    imageView.image = UIImage(named: imageName) 
    // Set Image border 
    imageView.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor 
    imageView.layer.cornerRadius = 5.0 
    imageView.layer.borderWidth = 3 
    imageView.contentMode = .scaleAspectFit 
} 
+0

太好了,非常感謝! :) –

+0

我喜歡做出功能的決定。 –

1

是的,可以。將skill1,skill2和skill3放置在一個數組中,並像這樣迭代它:

var objectArray = [skill1,skill2,skill3] 


for object in objectArray 
{ 
object.layer = .... 


} 
+0

太好了,非常感謝! :) –

1

當你不能夠使用for遍歷他們修改變量名和循環,你可以把你的skill情況下,在一個數組和循環通過他們的方式。

... 

skill1.image = UIImage(named: "english") 
skill2.image = UIImage(named: "literature") 
skill3.image = UIImage(named: "idea1") 

let skills = [skill1, skill2, skill3] 

for skill in skills { 
    skill.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor 
    skill.layer.cornerRadius = 5.0 
    skill.layer.borderWidth = 3 
    skill.contentMode = .scaleAspectFit 
} 
+0

太好了,非常感謝! :) –

0

你可以只擴展你的ImageView

extension UIImageView { 
    func addCustomLayer() { // add arguments to function if you wish to change the value assigned 
     self.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor 
     self.layer.cornerRadius = 5.0 
     self.layer.borderWidth = 3 
     self.contentMode = .scaleAspectFit 
    } 
    } 
} 

然後調用每個UIImageView的

for each in [skill1, skill2, skill3] { 
    each.addCustomLayer() 
} 
相關問題