ViewController.m
// ?UI常用控件
//
// ?Created by lanou on 16/7/10.
// ?Copyright ? 2016年 lanou. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
//標題標簽
@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
@implementation ViewController
- (void)viewDidLoad {
? ?[super viewDidLoad];
? ?self.imageNames =@[@"biaoqingdi",@"bingli",@"chiniupa",@"danteng",@"wangba"];
// ? 定義標簽位置與名稱
? ?self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(150,50, 150, 30)];
? ?self.titleLabel.text = @"biaoqingdi";
? ?[self.view addSubview:self.titleLabel];
// ? 定義做按鈕的位置
? ?self.leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(20,150, 45, 45)];
// ? ?關閉交互
? self.leftBtn.userInteractionEnabled = NO;
// ? 定義按鈕的圖片
? ?UIImage *leftImage = [UIImage imageNamed:@"left_disable"];
// ? ? 設置左按鈕的背景圖片
? [self.leftBtn setBackgroundImage:leftImage forState:(UIControlStateNormal)];
? ?[self.view addSubview:self.leftBtn];
// ? 顯示相框名稱
? ?self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(85, 100, 200, 200)];
? ?UIImage *image = [UIImage imageNamed:@"biaoqingdi"];
?self.myImageView.image = image;
// ? ?顯示相框圖片
? ?[self.view addSubview:self.myImageView];
// ? 設置右按鈕的位置
? ?self.rightBtn ?=[[UIButton alloc]initWithFrame:CGRectMake(305, 150, 45, 45)];
? ?\
// ? 設置右按鈕的圖片
? ?UIImage *rightImage = [UIImage imageNamed:@"right_normal"];
// ?設置右按鈕的背景圖片
? ?[self.rightBtn setBackgroundImage:rightImage forState:(UIControlStateNormal)];
? ?[self.view addSubview:self.rightBtn];
? ?// ? 按鈕的監(jiān)聽
? ?[self.rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
? ?[self.leftBtn addTarget:self action:@selector(leftBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
}
-(void)rightBtnAction
{
// ? ?切換到下一張圖片
// ? ?獲取當前是第幾張圖片
? ?NSInteger index = [self.imageNames indexOfObject:self.titleLabel.text];
// ? ?不是為最后一張才切換到下一張
? ?if (index < 4){
? ? ? ?if (index == 3){
// ? ? ? ? ? ?改變右邊按鈕的圖片和關閉交互
? ? ? ? ? ?self.rightBtn.userInteractionEnabled = NO;
? ? ? ? ? ?UIImage *image = [UIImage imageNamed:@"right_disable"];
? ? ? ? ? ?[self.rightBtn 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 *nextTitle = self.imageNames[index+1];
? ? ? ?self.titleLabel.text = nextTitle;
? ? ? ?self.myImageView.image = [UIImage imageNamed:nextTitle];
? ?}
}
-(void)leftBtnAction
? ?{
? ? ? ? ?NSInteger index = [self.imageNames indexOfObject:self.titleLabel.text];
? ? ? ?if (index > 0){
? ? ? ? ? ?if (index == 1){
? ? ? ? ? ?// ? ? ? ? ? ?改變左邊按鈕的圖片和關閉交互
? ? ? ? ? ?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 *preTitle = self.imageNames[index-1];
? ? ? ? ? ?self.titleLabel.text = preTitle;
? ? ? ? ? ?self.myImageView.image = [UIImage imageNamed:preTitle];
? ? ? ?}
? ?}
-(void)btnClickLister
{
? ?NSLog(@"click btn");
}
-(void)demo {
? ?// ? ?UIView *view =[u]
? ?// ? ?按鈕UIButton
? ?// ? ?UIButton *button =[UIButton buttonWithType:UIButtonTypeInfoDark];
? ?UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(20, 50,80, 80)];
? ?// ? ?frame表明了控件的坐標和寬高(CGRect類型)
? ?// ? ?[button setTitle:@"阿陶" forState:UIControlStateNormal];
? ?UIImage *image = [UIImage imageNamed:@"left_normal"];
? ?// ? ?根據名字加載圖片
? ?[button setBackgroundImage:image forState:UIControlStateNormal];
? ?// ? ?給按鈕設置背景圖片
? ?// ? button.backgroundColor = [UIColor redColor];
? ?// ? ?按鈕的監(jiān)聽
? ?[button addTarget:self action:@selector(btnClickLister) forControlEvents:UIControlEventTouchUpInside];
? ?// ? ?添加到視圖上面
? ?[self.view addSubview:button];
? ?//相框UIImageView
? ?UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(150, 50, 200, 200)];
? ?UIImage *image1 = [UIImage imageNamed:@"biaoqingdi"];
? ?// ? ?設置imageView顯示的圖片
? ?imageview.image = image1;
? ?[self.view addSubview:imageview];
? ?// ? ?標簽UILabel
? ?UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(150, 270, 150, 30)];
? ?// ? ?設置標簽文本
? ?label.text = @"濤哥哥";
? ?// ? ?設置居中方式
? ?label.textAlignment = NSTextAlignmentCenter;
? ?label.textColor = [UIColor redColor];
? ?[self.view addSubview:label];}
@end