今天的学习的是Oc中控件的学习,先是对控件方法的了解,然后是在代码板上写代码,实现在界面上图片的转换,还有是对按钮的应用
#import "ViewController.h"
@interface ViewController ()
//视图加载好的时候调用
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self func1];
//调用方法是空格,方法结束用中括号表示
NSInteger num=[self func2];
NSLog(@"num=%ld",num);
NSInteger length=[self lengOfString:@"12345"];
NSLog(@"length=%ld",length);
}
//oc方法的格式:
// +/- +表示类方法,只能用类来调用,-表示实列方法,用对象调用
// 无参输入的方法格式: +/-(方法的返回值)方法名
-(void)func1
{
NSLog(@"%s",__func__);
}
-(NSInteger)func2{
NSLog(@"%s",__func__);
return 20;
}
// 有参输入的方法格式 +/-(方法返回值) 方法名:(参数1类型)参数1名 方法名:(参数2类型)参数2名
//输入字符串,返回字符串的长度
-(NSInteger)lengOfString:(NSString *)string
{ return string.length;
}
//内存溢出的时候调用
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
上面是对相关方法的用法的陈述,
#import "ViewController.h"
@interface ViewController ()
有@property的表示的全局变量
@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(160, 30, 150, 30)];
self.titleLabel.textAlignment=NSTextAlignmentCenter;
//添加标签文本
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];
//设置相框UIImageView的位置
self.myImageView=[[UIImageView alloc]initWithFrame:CGRectMake(85, 100, 200, 200)];
//根据文字加载图片
UIImage *image=[UIImage imageNamed:@"biaoqingdi"];
//设置imageview显示图片
self.myImageView.image=image;
//添加到视图中
[self.view addSubview:self.myImageView];
//you按钮的位置
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];
//左按钮监听
[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{
//左边按钮和右边按钮都是在一个正常状态
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{
//左右两边按钮都是正常状态
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];
//添加到标签中
self.titleLabel.text=beforeTitle;
//调加到相框
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表明了控件的坐标和宽高(CGRect类型)
// button.frame=CGRectMake(50, 50, 80, 80);
[button setTitle:@"眼睛哥" forState:UIControlStateNormal ];
//根据名字加载图片
UIImage *image=[UIImage imageNamed:@"right_normal"];
//给按钮设置背景图片
[button setBackgroundImage:image forState:UIControlStateNormal];
//forState按钮状态
button.backgroundColor=[UIColor greenColor];
//按钮监听
[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"];
//设置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
这是用控件制作在界面上实现图片的交换