swift 視圖控件的應(yīng)用:霓虹燈

import UIKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

// Override point for customization after application launch.

//設(shè)置window大小澄成,UIScreen.main.bounds獲取屏幕大小

self.window = UIWindow(frame: UIScreen.main.bounds)

//設(shè)置背景顏色

self.window?.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)

//設(shè)置window成為主屏幕,并顯示

self.window?.makeKeyAndVisible()

//創(chuàng)建根控制器

self.window?.rootViewController = UIViewController()

//設(shè)置子視圖(紅色)

//創(chuàng)建子視圖(視圖位置,視圖大行逑酢)

let redView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))

//視圖背景顏色

redView.backgroundColor = UIColor.red

//視圖tag值(視圖唯一標(biāo)識)

redView.tag = 201

//設(shè)置視圖中心點(diǎn)

redView.center = (window?.center)!

//將視圖切成圓形(半徑)

redView.layer.cornerRadius = 100

//將視圖添加到主屏幕

self.window?.addSubview(redView)

//黃色(下同)

let yellowView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))

yellowView.backgroundColor = UIColor.yellow

yellowView.tag = 202

yellowView.center = (window?.center)!

yellowView.layer.cornerRadius = 75

yellowView.center = (window?.center)!

self.window?.addSubview(yellowView)

//藍(lán)色(下同)

let blueView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

blueView.backgroundColor = UIColor.blue

blueView.tag = 203

blueView.center = (window?.center)!

blueView.layer.cornerRadius = 50

self.window?.addSubview(blueView)

//定時(shí)器:參數(shù)1:定時(shí)執(zhí)行的間隔此疹,參數(shù)2:執(zhí)行目標(biāo)逸嘀,參數(shù)3:目標(biāo)對象選擇執(zhí)行的方法仿畸,參數(shù)4:用戶信息(nil)惶岭,參數(shù)5:定時(shí)器是否重復(fù)執(zhí)行

Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(chengeColor), userInfo: nil, repeats: true)

return true

}

//定時(shí)器方法

func chengeColor(){

//

let redView = self.window?.viewWithTag(201)

//存儲顏色

let color = redView?.backgroundColor

//交換顏色

self.window?.viewWithTag(201)?.backgroundColor = self.window?.viewWithTag(202)?.backgroundColor

self.window?.viewWithTag(202)?.backgroundColor = self.window?.viewWithTag(203)?.backgroundColor

self.window?.viewWithTag(203)?.backgroundColor = color

}

func applicationWillResignActive(_ application: UIApplication) {

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.

}

func applicationDidEnterBackground(_ application: UIApplication) {

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

func applicationWillEnterForeground(_ application: UIApplication) {

// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

}

func applicationDidBecomeActive(_ application: UIApplication) {

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

func applicationWillTerminate(_ application: UIApplication) {

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末示损,一起剝皮案震驚了整個(gè)濱河市渗磅,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌检访,老刑警劉巖始鱼,帶你破解...
    沈念sama閱讀 216,544評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異脆贵,居然都是意外死亡医清,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評論 3 392
  • 文/潘曉璐 我一進(jìn)店門丹禀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來状勤,“玉大人,你說我怎么就攤上這事双泪〕炙眩” “怎么了?”我有些...
    開封第一講書人閱讀 162,764評論 0 353
  • 文/不壞的土叔 我叫張陵焙矛,是天一觀的道長葫盼。 經(jīng)常有香客問我,道長村斟,這世上最難降的妖魔是什么贫导? 我笑而不...
    開封第一講書人閱讀 58,193評論 1 292
  • 正文 為了忘掉前任抛猫,我火速辦了婚禮,結(jié)果婚禮上孩灯,老公的妹妹穿的比我還像新娘闺金。我一直安慰自己,他們只是感情好峰档,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評論 6 388
  • 文/花漫 我一把揭開白布败匹。 她就那樣靜靜地躺著,像睡著了一般讥巡。 火紅的嫁衣襯著肌膚如雪掀亩。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,182評論 1 299
  • 那天欢顷,我揣著相機(jī)與錄音槽棍,去河邊找鬼。 笑死抬驴,一個(gè)胖子當(dāng)著我的面吹牛炼七,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播怎爵,決...
    沈念sama閱讀 40,063評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼特石,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了鳖链?” 一聲冷哼從身側(cè)響起姆蘸,我...
    開封第一講書人閱讀 38,917評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎芙委,沒想到半個(gè)月后逞敷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,329評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡灌侣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評論 2 332
  • 正文 我和宋清朗相戀三年推捐,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片侧啼。...
    茶點(diǎn)故事閱讀 39,722評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡牛柒,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出痊乾,到底是詐尸還是另有隱情皮壁,我是刑警寧澤,帶...
    沈念sama閱讀 35,425評論 5 343
  • 正文 年R本政府宣布哪审,位于F島的核電站蛾魄,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜滴须,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評論 3 326
  • 文/蒙蒙 一舌狗、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧扔水,春花似錦痛侍、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至嘹狞,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間誓竿,已是汗流浹背磅网。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評論 1 269
  • 我被黑心中介騙來泰國打工迫吐, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留何暇,地道東北人拳喻。 一個(gè)月前我還...
    沈念sama閱讀 47,729評論 2 368
  • 正文 我出身青樓姜骡,卻偏偏與公主長得像唉俗,于是被迫代替她去往敵國和親吃靠。 傳聞我的和親對象是個(gè)殘疾皇子蔑滓,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評論 2 353

推薦閱讀更多精彩內(nèi)容