天天看点

UIView、UILabel、UIImage基本控件的使用

1.UIView

//UIView常见的属性
@property(nonatomic,readonly) UIView *superview;
@property(nonatomic,readonly,copy) NSArray *subviews;
//尽量少使用tag
//1.效率低
//2.容易乱
@property(nonatomic) NSInteger tag;
@property(nonatomic) CGAffineTraansform transform;


//UIView常见的方法
-(void)addSubview:(UIView *)view;
-(void)removeFromSuperView;
-(UIView *)viewWithTag:(NSInteger)tag;
           

改变位置和尺寸

@property(nonatomic) CGRect frame;   //位置和尺寸
label.frame = CGRectMake(0,0,100,60);   //左上角相对于父控件的距离
@property(nonatomic) CGRect bounds;   //矩形框的位置和尺寸,以自己左上角为坐标原点,所以bounds的x,y一般为0。iOS9以后中心点不变,向四周延伸

@property(nonatomic) CGPoint center;  //改变位置
label.center = CGPointMake(100,200);   //中心点相对于父控件的左上角的距离
//子控件居中
label.center = CGPointMake(self.superview.frame.size.width * 0.5, self.superview.frame.size.height * 0.5);

改变位置和尺寸的方式

//方式1:
self.label.frame = CGRectMake(200,100,100,60);   //只改变位置
//方式2:
CGRect frame = self.label.frame;
frame.origin.x += 100;
frame.size.width += 50;
self.label.frame = frame;   //结构体是值传递,需要重新赋值

//方式3

           

2.UILabel

UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(100,200,300,75);
label.backgroundColor = [UIColor yellowColor];
[self.view addsubview:label];

label.text = @"hello word!";
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:25.f];
label.font = [UIFont boldSystemFontOfSize:25.f];
label.font = [UIFont italicSystemFontOfSize:25.f];
label.textColor = [UIColor redColor];
label.shadowColor = [UIColor whiteColor];   //设置阴影的颜色
label.shadowOffset = CGSizeMake(-2,8);   //设置阴影的偏移量
label.numberOfLines = 0;  //自动换行
label.lineBreakMode = NSLineBreakByWordWrapping;


           

3.UIImage

UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(100,200,200,200);
imageView.backgroundColor = [UIColor redColor];
imageView.image = [UIImage imageNamed:@"img1"];  //png不需要加后缀,jpg需要加后缀
//默认模式会拉伸
imageView.contentMode = UIViewContentModeCenter;  //不拉伸,但是图片有可能会超出imageView的大小,因此需要对图片进行裁

/*
UIViewContentModeReDraw

UIViewContentModeScaleToFill  拉伸

UIViewContentModeScaleAspectFit  宽高比不变  适应
UIViewContentModeScaleAspectFill   宽高比不变  填充满
 

UIViewContentModeCenter
UIViewContentModeTop
...
*/
imageView.clipsToBounds = YES; //将图片沿着边框裁剪,,保留中间部分,让图片填充满边框

[self.view addSubview:imageView];



           

毛玻璃效果

UIToolBar *tooBar = [[UIToolBar alloc] init];
toolBar.frame = imageView.bounds;
toolBar.barStyle = UIBarStyleBlack;
toolBar.alpha = 0.9;
[imageView addSubView:tooBar];
           

UIImageView的frame的设置

//方式1:
imageView.frame = CGRectMake(100,100,100,100);
imageView.frame = (CGRect){{100,100}{100,100}};
//方式2   获取图片的尺寸并根据图片的尺寸设置UIImageView的大小
imageView.frame = CGRectMake(0,0,image.size.width,image.width.height);
//方式3
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100,100,200,200)];
//方式4
UIImageView *imageView = [UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"];
imageView.center = CGPointMake(self.view.frame.width*0.5,self.view.frame.height*0.5);
//注意:通过这种方式不需要手动设置尺寸,尺寸就是图片的尺寸
           

iOS中的资源存放问题

图片的两种加载方式:

1.

NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"];
self.imageView.image = [UIImage imageWithContentOfFile:path ];

           

注意:

1.加载assest.xcassets里面的资源打包后变成Assets.car,拿不到路径

只能通过imageNamed:方式来加载图片,不能通过imageWithContentOfFile来加载图片

2.放到项目中的图片,可以拿到资源路径,能通过imageNamed:方式来加载图片,也能通过imageWithContentOfFile来加载图片

3.项目中只有蓝色的文件夹才是真实存在的,黄色的文件夹并不存在,只是为了方便我们对项目进行整理。

帧动画

NSMutableArray<UIImage> *imageArray = [NSMutableArray array];
for( int i = 0; i < 10; i++ ){
   NSString *imageNamed = [NSString stringWithFormat:@"stand_%d",i+1];
   UIImage *image = [UIImage imageNamed:imageNamed];
   [imageArray addObject:image];
}
//设置动画图片
self.imageView.animationImages = imageArray;
//设置播放次数
self.imageView.animationRepeatCount = 0;    //无限次
//设置播放的时常
self.imageView.animationDuration = 0.5;
//播放
[self.imageView startAnimating];

[self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];
           

imageNamed和imageWithContentOfFile的区别

1.imageNamed

a.就算指向它的指针被销毁,该资源也不会被从内存中回收

b.放到Assets.xcassets的图片,默认就有缓存

c.图片经常被使用的地方

2.imageWithContentOfFile

a.指向它的指针被销毁,会从内存中回收

b.放到项目中的图片不带有缓存

c.不经常用的图片,大批量的图片