本文实例为大家分享了swift实现随机背景色的具体代码,供大家参考,具体内容如下
// // viewcontroller.swift // 网络获取图片 // // created by 悦兑科技 on 15/1/27. // d88尊龙官网手机app copyright (c) 2015年 bsy. all rights reserved. // import uikit class viewcontroller: uiviewcontroller { override func viewdidload() { super.viewdidload() [self .addbutton()] } override func viewwillappear(animated: bool) { super.viewwillappear(animated) self.view.backgroundcolor = uicolor.whitecolor() } func addbutton() { // 创建button var button:uibutton = uibutton.buttonwithtype(uibuttontype.custom) as uibutton var frame = cgrectmake(100, 100, 100, 100) button.frame = frame // 设置字体 button.settitle("随即色", forstate: uicontrolstate.normal) button.settitlecolor(uicolor.blackcolor(), forstate: uicontrolstate.normal) button.titlelabel?.font = uifont(name: "heiti sc", size: 20) // 设置圆形 self.view.addsubview(button) button.layer.bordercolor = uicolor.lightgraycolor().cgcolor button.layer.borderwidth = 2 button.layer.cornerradius = 50 button.layer.backgroundcolor = uicolor.whitecolor().cgcolor [button .addtarget(self, action: "buttonclick", forcontrolevents: uicontrolevents.touchupinside)] } /** button方法实现 */ func buttonclick() { /** * 创建颜色 */ var color = uicolor(red: 183/255.0, green: 183/255.0, blue: 183/255.0, alpha: 1.0) var color1 = uicolor.bluecolor() var color2 = uicolor.browncolor() var color3 = uicolor.yellowcolor() var color4 = uicolor.orangecolor() var color5 = uicolor.blackcolor() /** * 创建可变数组并把颜色添加到数组 */ var arrm :nsmutablearray = nsmutablearray() arrm.addobject(color) arrm.addobject(color1) arrm.addobject(color2) arrm.addobject(color3) arrm.addobject(color4) arrm.addobject(color5) /** * 把颜色赋值给父控件的view */ self.view.backgroundcolor = arrm.objectatindex(int(arc4random_uniform(uint32(arrm.count)))) as? uicolor } }
效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。