小學(xué)期 2

今天的學(xué)習(xí)的是Oc中控件的學(xué)習(xí),先是對(duì)控件方法的了解,然后是在代碼板上寫(xiě)代碼,實(shí)現(xiàn)在界面上圖片的轉(zhuǎn)換,還有是對(duì)按鈕的應(yīng)用

#import "ViewController.h"

?

@interface?ViewController?()

//視圖加載好的時(shí)候調(diào)用

@end

@implementation?ViewController

?- (void)viewDidLoad {

????[super?viewDidLoad];

????// Do any additional setup after loading the view, typically from a nib.

????[self?func1];

????//調(diào)用方法是空格,方法結(jié)束用中括號(hào)表示

????NSInteger?num=[self?func2];

????NSLog(@"num=%ld",num);

?????NSInteger?length=[self?lengOfString:@"12345"];

????NSLog(@"length=%ld",length);

}

//oc方法的格式:

// +/- ?+表示類方法,只能用類來(lái)調(diào)用,-表示實(shí)列方法,用對(duì)象調(diào)用

// ?無(wú)參輸入的方法格式:???+/-(方法的返回值)方法名

-(void)func1

{

????NSLog(@"%s",__func__);

}

-(NSInteger)func2{

????NSLog(@"%s",__func__);

????return?20;

}

?// ??有參輸入的方法格式????+/-(方法返回值)?方法名:(參數(shù)1類型)參數(shù)1名??方法名:(參數(shù)2類型)參數(shù)2名

?//輸入字符串,返回字符串的長(zhǎng)度

-(NSInteger)lengOfString:(NSString?*)string

{??return?string.length;

}

//內(nèi)存溢出的時(shí)候調(diào)用

- (void)didReceiveMemoryWarning {

????[super?didReceiveMemoryWarning];

????// Dispose of any resources that can be recreated.

}

?

@end

上面是對(duì)相關(guān)方法的用法的陳述,

#import "ViewController.h"

?

@interface?ViewController?()

有@property的表示的全局變量

@property(nonatomic,strong)UILabel?*titleLabel;(定義的標(biāo)簽

//左邊按鈕

@property(nonatomic,strong)UIButton?*leftBtn;

//右邊按鈕

@property(nonatomic,strong)UIButton?*rightBtn;

//顯示圖片

@property(nonatomic,strong)UIImageView?*myImageView;

//定義了一個(gè)字符串?dāng)?shù)組來(lái)存放圖片名字

@property(nonatomic,strong)NSArray?*imageNames;

@end

@implementation?ViewController

- (void)viewDidLoad {

????[super?viewDidLoad];

????self.imageNames=@[@"biaoqingdi",@"bingli",@"chiniupa",@"danteng",@"wangba"];

??//創(chuàng)建并設(shè)置標(biāo)題標(biāo)簽

????//標(biāo)簽的位置

????self.titleLabel=[[UILabel?alloc]initWithFrame:CGRectMake(160, 30, 150, 30)];

????self.titleLabel.textAlignment=NSTextAlignmentCenter;

????//添加標(biāo)簽文本

????self.titleLabel.text=@"biaoqingdi";

????//添加到視圖中

????[self.view?addSubview:self.titleLabel];

????//左按鈕的位置

????self.leftBtn=[[UIButton?alloc]initWithFrame:CGRectMake(20, 150, 45, 45)];

????//關(guān)閉交互

????self.leftBtn.userInteractionEnabled=NO;

????//根據(jù)文字加載圖片

????UIImage??*leftImage=[UIImage?imageNamed:@"left_disable"];

????//給按鈕設(shè)置背景圖片

????[self.leftBtn?setBackgroundImage:leftImage forState:(UIControlStateNormal)];

????//添加到視圖中

????[self.view?addSubview:self.leftBtn];

????//設(shè)置相框UIImageView的位置

????self.myImageView=[[UIImageView?alloc]initWithFrame:CGRectMake(85, 100, 200, 200)];

????//根據(jù)文字加載圖片

????UIImage?*image=[UIImage?imageNamed:@"biaoqingdi"];

????//設(shè)置imageview顯示圖片

????self.myImageView.image=image;

????//添加到視圖中

????[self.view?addSubview:self.myImageView];

????//you按鈕的位置

????self.rightBtn=[[UIButton?alloc]initWithFrame:CGRectMake(305, 150, 45, 45)];

????//根據(jù)文字加載圖片

????UIImage??*rightImage=[UIImage?imageNamed:@"right_normal"];

????//給按鈕設(shè)置背景圖片

????[self.rightBtn?setBackgroundImage:rightImage forState:(UIControlStateNormal)];

?????//添加到視圖中

????[self.view?addSubview:self.rightBtn];

????//左按鈕監(jiān)聽(tīng)

????[self.rightBtn?addTarget:self?action:@selector(rightBtnAction) forControlEvents:(UIControlEventTouchUpInside)];

????//右按鈕監(jiān)聽(tīng)

????[self.leftBtn?addTarget:self?action:@selector(leftBtnAction) forControlEvents:(UIControlEventTouchUpInside)];

}

-(void)rightBtnAction

{

????//切換到下一張圖片

???//獲取當(dāng)前是第幾張圖片

????NSInteger?index = [self.imageNames?indexOfObject:self.titleLabel.text];

????//不是為最后一張才切換到下一張

????//從第一張開(kāi)始變化

????if(index<4){

????????if(index==3){

????????????//改變右邊按鈕的顏色和關(guān)閉交互

????????????self.rightBtn.userInteractionEnabled=NO;

????????????UIImage?*image=[UIImage?imageNamed:@"right_disable"];

????????????[self.rightBtn?setBackgroundImage:image forState:(UIControlStateNormal)];

????????}else{

????????????//左邊按鈕和右邊按鈕都是在一個(gè)正常狀態(tài)

?????????self.leftBtn.userInteractionEnabled=YES;

????????self.rightBtn.userInteractionEnabled=YES;

????????????UIImage?*leftNormal=[UIImage?imageNamed:@"left_normal"];

????????????UIImage?*rightNormal=[UIImage?imageNamed:@"right_normal"];

????????????[self.leftBtn?setBackgroundImage:leftNormal forState:(UIControlStateNormal)];

????????????[self.rightBtn?setBackgroundImage:rightNormal forState:(UIControlStateNormal)];

????????}

????????//切換到下一張

????????NSString?*nextTitle=self.imageNames[index+1];

????????//添加到標(biāo)簽中

????????self.titleLabel.text=nextTitle;

????????//調(diào)加到相框

????????self.myImageView.image=[UIImage?imageNamed:nextTitle];

????}

}

-(void)leftBtnAction

{

???NSInteger?index= [self.imageNames?indexOfObject:self.titleLabel.text];

????//從最后一張開(kāi)始變化的

????if(index>0)

????{

????????if(index==1){

????????????//左邊按鈕交互關(guān)閉

????????????self.leftBtn.userInteractionEnabled=NO;

????????????UIImage?*image=[UIImage?imageNamed:@"left_disable"];

????????????[self.leftBtn?setBackgroundImage:image forState:(UIControlStateNormal)];

????????}else{

????????????//左右兩邊按鈕都是正常狀態(tài)

????????????self.leftBtn.userInteractionEnabled=YES;

????????????self.rightBtn.userInteractionEnabled=YES;

????????????UIImage?*leftNormal=[UIImage?imageNamed:@"left_normal"];

????????????UIImage?*rightNormal=[UIImage?imageNamed:@"right_normal"];

????????????[self.leftBtn?setBackgroundImage:leftNormal forState:(UIControlStateNormal)];

????????????[self.rightBtn?setBackgroundImage:rightNormal forState:(UIControlStateNormal)];

????????}

????????NSString?*beforeTitle=self.imageNames[index-1];

????????//添加到標(biāo)簽中

????????self.titleLabel.text=beforeTitle;

????????//調(diào)加到相框

????????self.myImageView.image=[UIImage?imageNamed:beforeTitle];

????}

}

-(void)btnClickLister

{

????NSLog(@"click btn");

}

-(void)demo{

????//按鈕UIButton

????// ??UIButton *button=[UIButton buttonWithType:UIButtonTypeInfoDark];

????UIButton?*button=[[UIButton?alloc]initWithFrame:CGRectMake(50, 50, 80, 80)];

????//frame表明了控件的坐標(biāo)和寬高(CGRect類型)

????// button.frame=CGRectMake(50, 50, 80, 80);

????[button setTitle:@"眼睛哥"?forState:UIControlStateNormal?];

????//根據(jù)名字加載圖片

????UIImage?*image=[UIImage?imageNamed:@"right_normal"];

????//給按鈕設(shè)置背景圖片

????[button setBackgroundImage:image forState:UIControlStateNormal];

????//forState按鈕狀態(tài)

????button.backgroundColor=[UIColor?greenColor];

????//按鈕監(jiān)聽(tīng)

????[button addTarget:self?action:@selector(btnClickLister) forControlEvents:UIControlEventTouchUpOutside];

????//添加到視圖上面

????[self.view?addSubview:button];

????//相框UIImageView

????UIImageView?*imageView =[[UIImageView?alloc]initWithFrame:CGRectMake(150, 50, 200, 200)];

????UIImage?*image1=[UIImage?imageNamed:@"biaoqingdi"];

????//設(shè)置imageview顯示圖片

????imageView.image=image1;

????//添加到視圖上面

????[self.view?addSubview:imageView];

????//標(biāo)簽UILabel

????UILabel?*label=[[UILabel?alloc]initWithFrame:CGRectMake(150, 270, 150, 30)];

????label.text=@"表情帝";

????//設(shè)置居中方式

????label.textAlignment=NSTextAlignmentCenter;

????label.textColor=[UIColor?redColor];

????[self.view?addSubview:label];

}

@end

這是用控件制作在界面上實(shí)現(xiàn)圖片的交換

?



?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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