UIImageView 的contentMode这个属性是用来设置图片的显示方式,如居中、居右,是否缩放等,有以下几个常量可供设定:
UIViewContentModeScaleToFill
UIViewContentModeScaleAspectFit
UIViewContentModeScaleAspectFill
UIViewContentModeRedraw
UIViewContentModeCenter
UIViewContentModeTop
UIViewContentModeBottom
UIViewContentModeLeft
UIViewContentModeRight
UIViewContentModeTopLeft
UIViewContentModeTopRight
UIViewContentModeBottomLeft
UIViewContentModeBottomRight
注意以上几个常量,凡是没有带Scale的,当图片尺寸超过 ImageView尺寸时,只有部分显示在ImageView中。UIViewContentModeScaleToFill属性会导致图片变形。UIViewContentModeScaleAspectFit会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白。UIViewContentModeScaleAspectFill也会证图片比例不变,但是是填充整个ImageView的,可能只有部分图片显示出来。
比较字符串用isEqualToString
计时器销毁invalidate
progressView和activityIndictor控制图片的加载显示图的加载进度,加载过程中
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//初始化imageview并分配空间
self.imageview =[[UIImageView alloc]initWithFrame:CGRectMake(30, 160, 260, 180)];
//设置imageview的背景色
self.imageview.backgroundColor=[UIColor groupTableViewBackgroundColor];
//在imageview上贴上图片
[self.imageview setImage:[UIImage imageNamed:@"222.jpg"]];
//设置imagevie的默认显示模式
self.imageview.contentMode=UIViewContentModeScaleAspectFit;
//将view贴到viewcontrol上
[self.view addSubview:self.imageview];
//定义在segment上显示的数组
NSArray *arrar=[[NSArray alloc]initWithObjects:@"111.png",@"222.jpg", nil];
//初始化
self.segment=[[UISegmentedControl alloc]initWithItems:arrar];
//设置segmentcontrol的frame
self.segment.frame=CGRectMake(30, 460, 260, 30);
//添加回调
[self.segment addTarget:self action:@selector(tapB:) forControlEvents:UIControlEventValueChanged];
//默认按键
self.segment.selectedSegmentIndex=0;
[self.view addSubview:self.segment];
/*
self.label=[[UILabel alloc]initWithFrame:CGRectMake(100, 150, 100, 50)];
self.label.backgroundColor=[UIColor groupTableViewBackgroundColor];
self.label.text=@"显示一段文字";
[self.view addSubview:self.label];
self.button=[[UIButton alloc]initWithFrame:CGRectMake(100, 250,100 ,50 )];;
self.button.backgroundColor=[UIColor groupTableViewBackgroundColor];
[self.button addTarget:self action:@selector(tapA) forControlEvents:UIControlEventTouchUpInside];
_isButton=YES;
[self.button setTitle:@"按钮" forState: UIControlStateNormal];
[self.view addSubview:self.button];*/
}
/*
-(void)tapA
{
if (_isButton==YES) {
self.label.text=@"你好";
[self.view addSubview:self.label];
} else {
self.label.text=@"欢迎";
[self.view addSubview:self.label];
}
}*/
-(void)tapB:(UISegmentedControl *)sender
{
switch (sender.selectedSegmentIndex) {
case 1:
[self.imageview setImage:[UIImage imageNamed:@"111.png"]];
break;
case 0:
[self.imageview setImage:[UIImage imageNamed:@"222.jpg"]];
break;
default:
break;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/* //初始化imageview并分配空间
self.imageview =[[UIImageView alloc]initWithFrame:CGRectMake(30, 160, 260, 180)];
//设置imageview的背景色
self.imageview.backgroundColor=[UIColor groupTableViewBackgroundColor];
//在imageview上贴上图片
[self.imageview setImage:[UIImage imageNamed:@"222.jpg"]];
//设置imagevie的默认显示模式
self.imageview.contentMode=UIViewContentModeScaleAspectFit;
//将view贴到viewcontrol上
[self.view addSubview:self.imageview];
//定义在segment上显示的数组
NSArray *arrar=[[NSArray alloc]initWithObjects:@"111.png",@"222.jpg", nil];
//初始化
self.segment=[[UISegmentedControl alloc]initWithItems:arrar];
//设置segmentcontrol的frame
self.segment.frame=CGRectMake(30, 460, 260, 30);
//添加回调
[self.segment addTarget:self action:@selector(tapB:) forControlEvents:UIControlEventValueChanged];
//默认按键
self.segment.selectedSegmentIndex=0;
[self.view addSubview:self.segment];
*/
self.label=[[UILabel alloc]initWithFrame:CGRectMake(100, 150, 100, 50)];
self.label.backgroundColor=[UIColor groupTableViewBackgroundColor];
self.label.text=@"显示一段文字";
[self.view addSubview:self.label];
self.button=[[UIButton alloc]initWithFrame:CGRectMake(100, 250,100 ,50 )];;
self.button.backgroundColor=[UIColor groupTableViewBackgroundColor];
[self.button addTarget:self action:@selector(tapA) forControlEvents:UIControlEventTouchUpInside];
_isButton=YES;
[self.button setTitle:@"按钮" forState: UIControlStateNormal];
[self.view addSubview:self.button];
}
-(void)tapA
{
if (_isButton==YES) {
self.label.text=@"你好";
[self.view addSubview:self.label];
} else {
self.label.text=@"欢迎";
[self.view addSubview:self.label];
}
}