NSTimer使用簡單例子

2021-07-02 13:02:34 字數 2179 閱讀 2973

新建乙個工程,往window上新增赤橙黃綠青藍紫七個檢視 做成乙個巢狀的方形,使用nstimer每一秒鐘換一下顏色,要求迴圈換顏色並發生旋轉,待十秒後移除window上所有試圖(剛學,只會用最簡單的方法)

首先是建立檢視,我是通過手寫uiview來建立檢視的

**:

self.window = [[uiwindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];

self.window.backgroundcolor = [uicolor whitecolor];

[self.window makekeyandvisible];

view7=[[uiview alloc]initwithframe:cgrectmake(120, 100, 80, 80)];

view7.backgroundcolor=[uicolor purplecolor];

[self.window addsubview:view7];

view6=[[uiview alloc]initwithframe:cgrectmake(120, 100, 80, 80)];

view6.backgroundcolor=[uicolor cyancolor];

[self.window addsubview:view6];

view5=[[uiview alloc]initwithframe:cgrectmake(120, 100, 80, 80)];

view5.backgroundcolor=[uicolor bluecolor];

[self.window addsubview:view5];

view4=[[uiview alloc]initwithframe:cgrectmake(120, 100, 80, 80)];

view4.backgroundcolor=[uicolor greencolor];

[self.window addsubview:view4];

view3=[[uiview alloc] initwithframe:cgrectmake(120, 100, 80, 80)];

view3.backgroundcolor=[uicolor yellowcolor];

[self.window addsubview:view3];

view2=[[uiview alloc]initwithframe:cgrectmake(120, 100, 80, 80)];

view2.backgroundcolor=[uicolor orangecolor];

[self.window addsubview:view2];

view1=[[uiview alloc]initwithframe:cgrectmake(120, 100, 80, 80)];

view1.backgroundcolor=[uicolor redcolor];

[self.window addsubview:view1];

if(1)

return yes;
然後是判斷什麼時候改變檢視顏色,即切換檢視

**:

-(void) changecolor:(nstimer *)timer

if(i==2||i==9)

if (i==3||i==10)

if (i==4)

if (i==5)

if (i==6)

if (i==7)

if(i==10)

}

所建立的view時全域性變數,i是靜態變數。

nstimer是乙個類,首先建立乙個例項物件。

例如

nstimer *timer1=[nstimer scheduledtimerwithtimeinterval:(nstimeinterval) target:(id) selector:(sel) userinfo:(id) repeats:(bool)];
//執行
[timer fire];
其他很簡單,看一下都懂了,nstimer這個類說簡單簡單,說難又難,更加詳細的可以上網查一下。

最後結尾,第一次寫的東西雖然很簡單,還是作為ios開發的第一次程式設計吧,人生總有第一次,不管怎樣,先開個頭吧

NSTimer的簡單使用和停止

nstimer timer 1.執行一次自動停止 timer nstimer scheduledtimerwithtimeinterval 3.0 target self selector selector function userinfo nilrepeats no 這樣timer執行一次就會自...

NSTimer 使用高階

nstimer 是 ios 上的一種計時器,通過 nstimer 物件,可以指定時間間隔,向乙個物件傳送訊息。nstimer 是比較常用的工具,比如用來定時更新介面,定時傳送請求等等。但是在使用過程中,有很多需要注意的地方,稍微不注意就會產生 bug,crash,記憶體洩漏。本文講解了使用 nsti...

GDI 簡單使用例子

gdi 簡單使用例子 1 目錄 2 修訂歷史紀錄 3 1 介紹 4 2 約定 4 3 示例 4 本文件將以乙個具體的例子,講解gdi 簡單的使用例子。斜體字是真正的程式 gdi 主要使用的是system.drawing system.drawing.drawing2d system.drawing....