swift 随机背景色的实现

前端之家收集整理的这篇文章主要介绍了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

}

原文链接:https://www.f2er.com/swift/325342.html

猜你在找的Swift相关文章