OC語法

//標題標簽

@property(nonatomic,strong)UILabel*titleLabel;

//左邊按鈕

@property(nonatomic,strong)UIButton*leftBtn;

//右邊按鈕

@property(nonatomic,strong)UIButton*rightBtn;

//顯示圖片

@property(nonatomic,strong)UIImageView*myImageView;

@property(nonatomic,strong)NSArray*imageNames;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

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

//定義標簽位置與名稱

self.titleLabel=[[UILabelalloc]initWithFrame:CGRectMake(50,50,150,30)];

self.titleLabel.text=@"biaoqingdi";

[self.viewaddSubview:self.titleLabel];

//定義做按鈕的位置

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

//關(guān)閉交互

self.leftBtn.userInteractionEnabled=NO;

//定義按鈕的圖片

UIImage*leftImage=[UIImageimageNamed:@"left_disable"];

//設(shè)置左按鈕的背景圖片

[self.leftBtnsetBackgroundImage:leftImageforState:(UIControlStateNormal)];

[self.viewaddSubview:self.leftBtn];

//顯示相框名稱

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

UIImage*image1= [UIImageimageNamed:@"biaoqingdi"];

self.myImageView.image=image1;

//顯示相框圖片

[self.viewaddSubview:self.myImageView];

//設(shè)置右按鈕位置

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

//設(shè)置右按鈕圖片

UIImage*rightImage=[UIImageimageNamed:@"right_normal"];

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

[self.rightBtnsetBackgroundImage:rightImageforState:(UIControlStateNormal)];

[self.viewaddSubview:self.rightBtn];

[self.rightBtnaddTarget:selfaction:@selector(rightBtnAction)forControlEvents:(UIControlEventTouchUpInside)];

[self.leftBtnaddTarget:selfaction:@selector(leftBtnAction)forControlEvents:(UIControlEventTouchUpInside)];

}

-(void)leftBtnAction

{

NSUIntegerindex = [self.imageNamesindexOfObject:self.titleLabel.text];

if(index>0){

if(index==1){

self.leftBtn.userInteractionEnabled=NO;

UIImage*image = [UIImageimageNamed:@"left_disable"];

[self.leftBtnsetBackgroundImage:imageforState:(UIControlStateNormal)];

}else{

self.leftBtn.userInteractionEnabled=YES;

self.rightBtn.userInteractionEnabled=YES;

UIImage*leftNormal= [UIImageimageNamed:@"left_normal"];

UIImage*rightNormal=[UIImageimageNamed:@"right_normal"];

[self.leftBtnsetBackgroundImage:leftNormalforState:(UIControlStateNormal)];

[self.rightBtnsetBackgroundImage:rightNormalforState:(UIControlStateNormal)];

}

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

self.titleLabel.text= preTitle;

self.myImageView.image=[UIImageimageNamed:preTitle];

}

}

-(void)rightBtnAction

{

//切換到下一張圖片

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

NSUIntegerindex = [self.imageNamesindexOfObject:self.titleLabel.text];

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

if(index<4){

if(index==3){

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

self.rightBtn.userInteractionEnabled=NO;

UIImage*image = [UIImageimageNamed:@"right_disable"];

[self.rightBtnsetBackgroundImage:imageforState:(UIControlStateNormal)];

}else{

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

self.leftBtn.userInteractionEnabled=YES;

self.rightBtn.userInteractionEnabled=YES;

UIImage*leftNormal= [UIImageimageNamed:@"left_normal"];

UIImage*rightNormal=[UIImageimageNamed:@"right_normal"];

[self.leftBtnsetBackgroundImage:leftNormalforState:(UIControlStateNormal)];

[self.rightBtnsetBackgroundImage:rightNormalforState:(UIControlStateNormal)];

}

//通過下標訪問下一張圖片

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

self.titleLabel.text= nextTitle;

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

self.myImageView.image= [UIImageimageNamed:nextTitle];

}

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容