ios - Trying to change backgroundColor every x seconds -
what trying change backgroundcolor of screen new color every 5 seconds keep getting 2 errors. 1 in extension of uicolor, says "declaration valid @ file scope" , in self.view.backgroundcolor says "type 'uicolor' has no member of 'randomcolor'". thank or , time , ope helps other people out there. below code:
import uikit class viewcontroller: uiviewcontroller { @iboutlet weak var person: uiview! extension uicolor { func randomcolor() -> uicolor{ let red = cgfloat(drand48()) let green = cgfloat(drand48()) let blue = cgfloat(drand48()) return uicolor(red: red, green: green, blue: blue, alpha: 1.0) } } func changebackround() -> uicolor { self.view.backgroundcolor = uicolor.randomcolor() } override func viewdidload() { self.viewdidload() nstimer.scheduledtimerwithtimeinterval(5, target: self, selector: selector("changebackground"), userinfo: nil, repeats: true) } }
you cannot declare class extension within declaration scope of class. should move block
extension uicolor { func randomcolor() -> uicolor { let red = cgfloat(drand48()) let green = cgfloat(drand48()) let blue = cgfloat(drand48()) return uicolor(red: red, green: green, blue: blue, alpha: 1.0) } } outside of scope of class viewcontroller: uiviewcontroller.
plus, here func randomcolor() -> uicolor declares method on instances of uicolor. want declare func static have class method. resulting file should be
import uikit class viewcontroller: uiviewcontroller { @iboutlet weak var person: uiview! func changebackround() -> uicolor { self.view.backgroundcolor = uicolor.randomcolor() } override func viewdidload() { self.viewdidload() nstimer.scheduledtimerwithtimeinterval(5, target: self, selector: selector("changebackground"), userinfo: nil, repeats: true) } } extension uicolor { static func randomcolor() -> uicolor { let red = cgfloat(drand48()) let green = cgfloat(drand48()) let blue = cgfloat(drand48()) return uicolor(red: red, green: green, blue: blue, alpha: 1.0) } }
Comments
Post a Comment