swift 随机背景色的实现

//

// ViewController.swift

//网络获取图片

// Created by悦兑科技on 15/1/27.

// 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)

button.frame = frame

//设置字体

button.setTitle("随即色",forState: UIControlState.normal)

button.setTitleColor(UIColor.blackColor(),253)"> 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

}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...