1、uiimageview 是用來顯示圖檔的控件,相當于相框,用來顯示uiimage對象
//初始化uiimage對象及為其加載圖檔
//第一種方式
// uiimage *image = [uiimage imagenamed:@"1.jpg"];
第二種方式
通過圖檔的路徑加載圖檔
通過應用程式包找出圖檔 nsbundle (應用程式包類)
擷取目前應用包對象
//pathforresource: 資源名稱 oftype: 資源類型
// nsstring *filepath = [[nsbundle mainbundle]pathforresource:@"2" oftype:@"jpg"];
// uiimage *image2 = [uiimage imagewithcontentsoffile:filepath];
兩種優缺點和使用場景:
第一種方式:如果這個圖檔資源被多次使用,使用第一種方式,此種方式會把圖檔添加到應用程式的緩存中,多次使用比較友善 ,缺點:占用記憶體,優點:第二次使用速度很快
第二種方式:如果這個圖檔資源隻被使用一次,使用第二種方式,此種方式不會把圖檔對象添加到緩存中,缺點:耗時
——————————————————————————————
//建立uiimageview 對象
uiimageview *imageview = [[uiimageview alloc]initwithimage:image];
//配置屬性
//設定frame
imageview.frame = [uiscreen mainscreen].bounds;
//重新設定圖檔
imageview.image = image2;
[self.view addsubview:imageview];
[imageview release];
//uiimageview 加載動态圖檔
//1.準備一組圖檔 使用數組存放
nsmutablearray *imagearray = [nsmutablearray arraywithcapacity:7];
//使用for循環添加圖檔
for (int i =1; i < 8; i ++) {
//先确定圖檔的名字
nsstring *name = [nsstring stringwithformat:@"huoju_%d.tiff",i ];
//初始化image對象
uiimage *image = [uiimage imagenamed:name];
//将圖檔添加到數組中
[imagearray addobject:image];
}
// nslog(@"%@",imagearray);//驗證數組中是否添加完成
//建立動畫視圖
uiimageview *fireimageview = [[uiimageview alloc]initwithframe:cgrectmake(120, 100, 79, 106)];
fireimageview.backgroundcolor = [uicolor greencolor];
//設定uiimageview 播放動态圖檔需要的數組
fireimageview.animationimages = imagearray;
//設定播放速度
fireimageview.animationduration = 0.1;
//設定播放圖檔重複次數
fireimageview.animationrepeatcount = 1000;
//啟動動态圖檔播放 對動畫的配置寫在動畫開始之前 vip
[fireimageview startanimating];
//添加到父視圖
[self.view addsubview:fireimageview];
[fireimageview release];
//等比縮放圖檔
nsstring *fielepath2 = [[nsbundle mainbundle]pathforresource:@"8" oftype:@"jpg"];
uiimage *image3 = [uiimage imagewithcontentsoffile:fielepath2];
//image3.size 中存放的是圖檔的寬和高
nslog(@"%@",nsstringfromcgsize(image3.size));
//将uiimageview 設定為寬200,高為知,用來顯示不是真的image3
uiimageview *cfimageview = [[uiimageview alloc]initwithimage:image3];
cgfloat h = image3.size.height * 200 / image3.size.width ;
cfimageview.frame = cgrectmake(60, 200, 200, h);
[self.view addsubview:cfimageview];
[cfimageview release];
————————————————————————————
練習1:
//制作僵屍動态圖檔
cgsize zsize = cgsizezero;
nsmutablearray *zombil = [nsmutablearray arraywithcapacity:22];
for (int i = 1; i < 23; i ++) {
nsstring *name1 = [nsstring stringwithformat:@"zombie%d.tiff",i];
//建立image對象
uiimage *image4 = [uiimage imagenamed:name1];
//定義size變量存儲圖檔大小
zsize = image4.size;
[zombil addobject:image4];
nslog(@"%@",zombil);//驗證
//建一個imageview 視圖
uiimageview *zombie1 = [[uiimageview alloc]initwithframe:cgrectmake(10, 100, 300, zsize.height * 300 / zsize.height)];
zombie1.backgroundcolor = [uicolor redcolor];
//設定播放動畫需要的數組
zombie1.animationimages = zombil;
zombie1.animationduration = 3;
//設定播放重複次數:給0是無限重複
zombie1.animationrepeatcount = 100;
//啟動動畫
[zombie1 startanimating];
//加載到父視圖
[self.view addsubview:zombie1];
[zombie1 release];
======================================================
2、uiswitch 開關控件 繼承自 uicontrol
//建立swictch對象
uiswitch *aswitch1 = [[uiswitch alloc]initwithframe:cgrectmake(30, 50, 0, 0)];
//配置switch邊框的渲染顔色
aswitch1.tintcolor = [uicolor redcolor];
//配置控件内部的顔色
aswitch1.ontintcolor = [uicolor bluecolor];
//設定按鈕的顔色
aswitch1.thumbtintcolor = [uicolor cyancolor];
//給switch 添加事件
//self 指視圖控制器對象
[aswitch1 addtarget:self action:@selector(handleswitch:) forcontrolevents:uicontroleventvaluechanged];//當狀态代表的數值改變的時候事件觸發
[self.view addsubview:aswitch1];
[aswitch1 release];
}
#pragma mark switch 的關聯事件
- (void)handleswitch: (uiswitch *)aswitch{
//首先應該或許開關控件目前的狀态
switch ((int)aswitch.on) {
case yes:
nslog(@"開了");
break;
case no:
nslog(@"關了");
default:
3、uistepper 加減控件 繼承自 uicontrol
uistepper *astep = [[uistepper alloc]initwithframe:cgrectmake(100, 100, 0, 0)];
//設定邊框顔色
astep.tintcolor = [uicolor redcolor];
//設定背景顔色
astep.backgroundcolor = [uicolor cyancolor];
//設定step的最小值 預設最小值是0.0
astep.minimumvalue = 10.0;
//設定step的最大值 預設最大值是100.0
astep.maximumvalue = 30.0;
//長按按鈕數值是否自動增加或減小,預設為yes
astep.autorepeat = no;
//設定控件所代表的數值當大于最大值或者小于最小值的時候,是否可以從另一頭開始
// astep.wraps = yes;
//設定點選按鈕時數值的變化值
astep.stepvalue = 10.0;
//給astep添加事件
[astep addtarget:self action:@selector(handlestepper : ) forcontrolevents:(uicontroleventvaluechanged)];
[self.view addsubview:astep];
[astep release];
//實作事件方法
- (void)handlestepper : (uistepper *)stepper{
//stepper.value 代表這個控件目前的數值
nslog(@"%f",stepper.value);
=========================================================
4、uisegmentedcontrol 分段控制器,此控件有多個分段組成,每一個分段相當于一個button
nsarray *titles = @[@"紅色",@"綠色背景",@"藍色",@"橙色",@"紫色"];
//建立segmentcontrol 對象,并為每個分段添加title
uisegmentedcontrol *asegment = [[uisegmentedcontrol alloc]initwithitems:titles];
//segmentcontrol 每個标題的寬度預設是等分總寬度的
asegment.frame = cgrectmake(20, 40, 280, 40);
//設定segmentcontrol的邊框顔色
asegment.tintcolor = [uicolor magentacolor];
//設定預設選中的分段
asegment.selectedsegmentindex = 0;
//修改分段的寬度
[asegment setwidth:60 forsegmentatindex:1];
//給segmentcontrol 關聯事件
[asegment addtarget:self action:@selector(handlsegment : ) forcontrolevents:(uicontroleventvaluechanged)];
//添加父視圖
[self.view addsubview:asegment];
[asegment release];
————————————————————
#pragma mark segmentcontrol 的關聯事件
- (void)handlsegment : (uisegmentedcontrol *)segment{
// segment.selectedsegmentindex 傳回目前分段控制器被選中的下标
nslog(@"%ld",segment.selectedsegmentindex);//驗證
self.view.backgroundcolor = [uicolor bluecolor];
switch (segment.selectedsegmentindex) {
case 0:
self.view.backgroundcolor = [uicolor redcolor];
case 1:
self.view.backgroundcolor = [uicolor greencolor];
case 2:
self.view.backgroundcolor = [uicolor bluecolor];
case 3:
self.view.backgroundcolor = [uicolor orangecolor];
case 4:
self.view.backgroundcolor = [uicolor purplecolor];
================================================
5、uislider 滑塊控件,繼承自uicontrol ,主要用來顯示目前播放進度,控制音量或進度
(注意:必須記憶的方法)
uislider *aslider = [[uislider alloc]initwithframe:cgrectmake(20, 100, 280, 30)];
//設定屬性
//設定滑塊最小值
aslider.minimumvalue = 0.2;
//設定滑塊最大值
aslider.maximumvalue = 1.0;
//設定滑塊目前的數值(或位置)
aslider.value = 0.5;
//設定滑過區域的顔色
aslider.minimumtracktintcolor = [uicolor redcolor];
//設定未滑過區域的顔色
aslider.maximumtracktintcolor = [uicolor whitecolor];
//設定滑塊上的圖檔
[aslider setthumbimage:[uiimage imagenamed:@"slider"] forstate:uicontrolstatenormal];
//關聯時間
[aslider addtarget:self action:@selector(handleslinder : ) forcontrolevents:(uicontroleventvaluechanged)];
[self.view addsubview:aslider];
[aslider release];
效果圖:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5ycuFmc091Zz9CXu9Wbt92Yvw1cldWYtl2LcVGb5R3c3c2bsJ2Lc52YuMnah5Waz5yZtl2cvw1LcpDc0RHaiojIsJye.gif)
#pragma mark slider的關聯時間
- (void)handleslinder : (uislider *)aslider{
//通過滑塊控制視圖的透明度
self.view.alpha = aslider.value;
//value 傳回的是目前滑塊所在位置代表的數值
//nslog(@"%.2f",aslider.value);//驗證使用,用過要注掉
總結:uicontroll 是控制控件的基類,凡是繼承自該類的子類都可以通過addtarget: action: forcontrolevents : 這個方法添加響應時間,uiontrol類的實作類似我們學過的target ... action 設計模式,是以如果你想自定義一個視圖,而且還是能夠響應事件的,那就讓它繼承自uicontrol;
歡迎學習本文,未經部落客許可,禁止轉載!
轉自:http://blog.csdn.net/qq_31810357