UIView
UIView是所有视图的父类,UIView的属性和方法,就是所有视图的属性和方法
一.关于坐标系的属性和方法
@property(nonatomic) CGRect frame;
@property(nonatomic) CGRect bounds;
@property(nonatomic) CGPoint center;
@property(nonatomic) CGAffineTransform transform;
属性修饰符 atomic表示原子操作,即set方法get方法不可被其他线程中断 atomic是缺省属性;
iOS的UIKit框架下的视图属性默认都是 nonatomic,非原子操作 。
1、创建一个UIView视图
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
2、设置view的背景色
view.backgroundColor=[UIColor redColor];
3、设置center属性,中心点坐标(设置view至正中心)
view.center=self.view.center;
4、设置view的bounds,相对于本视图坐标系,默认的为左上角为(0,0)
view.bounds=CGRectMake(0, 0, 50, 50);
5、让view的尺寸变形
两个参数是浮点型,它是变形后和变形前的横纵的百分比
view.transform=CGAffineTransformMakeScale(0.5, 2);
6、让角度变形
view.transform=CGAffineTransformMakeRotation(M_PI_2/4);
7、把view放到controller
[self.view addSubview:view];
[view release];
二.关于父子视图关系的属性和方法
【注】任何视图,都可以添加到另一个视图上面,但是每个视图只能有一个父视图。也就是说一个子视图被添加到另一个视图上,会从原父视图上脱离。
【注】子视图的坐标是相对于父视图的。以父视图左上角一点为原点,缺省原点为(0,0)点。移动父视图,因为子视图的位置是相对的,所以会一起移动。
@property(nonatomic,readonly) UIView *superview;
@property(nonatomic,readonly,copy) NSArray *subviews;
@property(nonatomic,readonly) UIWindow *window;
1、默认hidden为隐藏属性,默认为NO
父视图隐藏,那么子视图也会跟着一起隐藏(父子隐藏)
superView.hidden=YES;
2、边界裁剪,会裁掉超出父视图那部分
superView.clipsToBounds=YES;
3、alpha 透明度,默认为1
如果改变了父视图的透明度,那么子视图的也会改变
superView.alpha=0.6;
三.关于同父视图的子视图的层次关系
传入子视图地址,将子视图拿到最上层
1、把某个子视图放到最前面
[self.view bringSubviewToFront:redLabel];
2、把某个子视图放到最后
[self.view sendSubviewToBack:blueLabel];/
3、交换两个子视图的位置(挡没挡住),第一个参数如果还不是父视图的子视图,会被添加上去,如果是子视图,那么只改变层次关系(上)
[self.view insertSubview:yellowLabel aboveSubview:redLabel];
4、交换两个子视图的位置,第一个参数如果还不是父视图的子视图,会被添加上去,如果是子视图,那么只改变层次关系(下)
[self.view insertSubview:yellowLabel belowSubview:redLabel];
5、为两个子视图换一个位置,两个参数是视图的序号
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
四.层次与事件接收
1.父视图不能接收事件,则子视图无法接受事件。
2.子视图超出父视图的部分,不能接收事件。
3.同一个父视图下,最上面的视图,首先遭遇事件,如果能够响应,就不向下传递事件。如果不能响应,事件向下传递。
4、这个是控制是否可以接收事件,或者可以响应,(UIView的属性,默认是打开的YES);
label也有这个属性,但是他的这个属性默认是关闭的;
UIImageView也是UIView,他同样有这个属性,他的属性默认是关闭的
button.userInteractionEnabled=YES;
5、裁剪,边界裁剪,如果打开这个属性,父视图会裁剪子视图超出父视图那部分,默认为NO
superView.clipsToBounds=YES;
五.UIView动画
【注】UIView动画只能修改关于坐标系的属性,以及色彩和透明度。
1、启动动画
[UIView beginAnimations:nil context:nil];
2、设置动画启动的延迟时间
[UIView setAnimationDelay:3];
3、设置动画持续的时间,也就是完成的时间
[UIView setAnimationDuration:10];
4、设置动画的委托
[UIView setAnimationDelegate:self];
5、设置回调方法
[UIView setAnimationDidStopSelector:@selector(stopAnimation)];
6、执行动画
[UIView commitAnimations];
7、通过block方式实现动画
void(^animationBlock)(void)=^(void)
{
view.frame=self.view.bounds;
view.backgroundColor=[UIColor blueColor];
};
void(^completionBlock)(BOOL finished)=^(BOOL finished){
[UIView animateWithDuration:5 delay:0 options:0 animations:^{
view.frame=CGRectMake(150, 230, 20, 20);
view.backgroundColor=[UIColor redColor];
}completion:nil];
};
第一个参数是持续时间,第二个参数是动画启动的延迟时间,第三个默认0,第四个参数是block,在这里我们来实现我们的动画,第五个block是完成的block
[UIView animateWithDuration:10 delay:2 options:0 animations:animationBlock completion:completionBlock];
六.停靠模式
【注】停靠模式,是控制父视图改变大小时,子视图的变化方式。
【注】停靠模式并非服务于视图缩放,使用transform属性scale视图,根本是等比缩放,子视图也会等比缩放。停靠模式是服务于父视图边界修改后,子视图的重新布局。
1、设置停靠模式
上边距自由调整,下边距自由调整,高度不变
subView.autoresizingMask=UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
UIViewAutoresizingNone = 0, 没有变化
UIViewAutoresizingFlexibleLeftMargin = 1 << 0, 自由调整左边距,保持右边距不变
UIViewAutoresizingFlexibleWidth = 1 << 1, 自由调整宽距,保持左右边局部不变
UIViewAutoresizingFlexibleRightMargin = 1 << 2, 自由调整右边距,保持左边距不变
UIViewAutoresizingFlexibleTopMargin = 1 << 3, 自由调整上边距,保持下边距不变
UIViewAutoresizingFlexibleHeight = 1 << 4, 自由调整高度,保持上下边距不变
UIViewAutoresizingFlexibleBottomMargin = 1 << 5, 自由调整下边距,保持上边距不变
2、创建滑块视图
UISlider *slider=[[UISlider alloc] initWithFrame:CGRectMake(10, 400, 280, 20)];
3、设置滑块视图最大值
slider.maximumValue=1;
4、设置滑块视图最小值
slider.minimumValue=0;
5、滑块初始位置
slider.value=1;
6、为滑块视图添加事件
[slider addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
UIImageView
图片视图 UIImage UIImageView
【注】UIImage和UIImageView的关系,近似于NSString和UILabel的关系。
1、创建UIImageView,也是继承于UIView
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(200, 200, 50, 50)];
imageView.backgroundColor=[UIColor blueColor];
imageView.center=self.view.center;
[self.view addSubview:imageView];
[imageView release];
2、类方法创建image
(1)UIImage *image=[UIImage imageNamed:str];
(2)NSString *path=[NSString stringWithFormat:@“照片名”];
NSString *filePath=[[NSBundle mainBundle] pathForResource:path ofType:@"jpg"];
UIImage *image=[UIImage imageWithContentsOfFile:filePath];
3、(1)设置图片数据(这个数据一定是UIImage类型)
NSMutableArray *imageArray=[[NSMutableArray alloc] init];
把图片放到了我们图片数组里
for (int i=1; i<=12; i++) {
NSString *str=[NSString stringWithFormat:@"player%d.png",i];
UIImage *image=[UIImage imageNamed:str];
[imageArray addObject:image];
}
imageView.animationImages=imageArray;
(2)imageView.image=image;
4、设置我们动画的持续时间,我在多少秒内完成我的图片切换
imageView.animationDuration=3;
5、重复执行的次数,如果为0,无限次循环
imageView.animationRepeatCount=0;
6、启动动画
[imageView startAnimating];
7、停止动画
[imageView stopAnimating];
例题:会飞的鸽子