i wish pick change background color of swift application calling random color predetermined string array such (red, green, pink).
so have array , want background color randomly selected 1 of thoses colors.
using self.bg.background color resulted in xcode saying string not converted uicolor
here's example of 1 way this:
import uikit extension array { func randomelement() -> element { return self[int(arc4random_uniform(uint32(self.count)))] } } extension uicolor { enum colorenum: string { case red // = "red" case green // = "green" case blue // = "blue" case pink // = "pink" func tocolor() -> uicolor { switch self { case .red: return .redcolor() case .green: return .greencolor() case .blue: return .bluecolor() case .pink: return uicolor(hue: 1.0, saturation: 0.25, brightness: 1.0, alpha: 1.0) } } } static func fromstring(name: string) -> uicolor? { return colorenum(rawvalue: name)?.tocolor() } } let colors = ["red", "green", "pink"] // stored string uicolor.fromstring(colors.randomelement()) let enumcolors: [uicolor.colorenum] = [.red, .green, .pink] // stored enum enumcolors.randomelement().tocolor()
Comments
Post a Comment