【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,非原子操作
//创建一个UIView视图
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
//设置view的背景色
view.backgroundColor=[UIColor redColor];
//设置center属性,中心点坐标
view.center=self.view.center;
//设置view的bounds,相对于本视图坐标系,默认的为左上角为(0,0)
view.bounds=CGRectMake(0, 0, 50, 50);
//下面我们让view的尺寸变形
view.transform=CGAffineTransformMakeScale(0.5, 2);
//两个参数是浮点数,它是变形后和变形前的横纵的百分比
//我们让角度变形
view.transform=CGAffineTransformMakeRotation(M_PI_2/3);
//如果下面设置相同的属性,那么下面设置会覆盖上面的
//把view放到我们的controller
[self.view addSubview:view];
[view release];
//改变一个superView中心点
superView.center=CGPointMake(100, 100);
//我们改变了父视图center坐标,那么子视图也跟着移动
//改变一下superView的bounds
superView.bounds=CGRectMake(-10, -10, superView.bounds.size.width, superView.bounds.size.height);
//hidden为隐藏属性,默认为NO
//父视图隐藏,那么子视图也会跟着一起隐藏
//父子隐藏
superView.hidden=NO;
//设置redVieW中心点,超时父视图部分也是可以显示的
redView.center = CGPointMake(40, 0);
//我们进行边界裁剪,会裁掉超出父视图的那部分
superView.clipsToBounds=YES;
//alpha 透明度,默认为1
//如果改变了,父视图的透明度,那么子视图的也跟着透明
superView.alpha=0.6;
二.关于父子视图关系的属性和方法
【注】任何视图,都可以添加到另一个视图上面,但是每个视图只能有一个父视图。也就是说一个子视图被添加到另一个视图上,会从原父视图上脱离。
【注】子视图的坐标是相对于父视图的。以父视图左上角一点为原点,缺省原点为(0,0)点。移动父视图,因为子视图的位置是相对的,所以会一起移动。
三.关于同父视图的子视图的层次关系
//传入子视图地址,将子视图拿到最上层
//创建三个lable
UILabel *Redlable=[[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 50)];
Redlable.backgroundColor=[UIColor redColor];
UILabel *yellowlable=[[UILabel alloc]initWithFrame:CGRectMake(50, 80, 100, 50)];
yellowlable.backgroundColor=[UIColor yellowColor];
UILabel *bluelable=[[UILabel alloc]initWithFrame:CGRectMake(50, 110, 100, 50)];
bluelable.backgroundColor=[UIColor blueColor];
[self.view addSubview:Redlable];
[self.view addSubview:yellowlable];
[self.view addSubview:bluelable];
[Redlable release];
[yellowlable release];
[bluelable release];
// 获取subViews(所有的controller的视图)
NSArray *subViews=[self.view subviews];
for(int i=0;i<[subViews count];i++)
{
UILabel *lable=(UILabel*)[subViews objectAtIndex:i];
lable.text=[NSString stringWithFormat:@"hello %d",i];
lable.adjustsFontSizeToFitWidth=YES;
}
添加的视图是有序号的,先添加的为0,一次加1,我们看到了红、黄 、蓝
改变视图的层次关系的几种方法
1⃣️. 将某一个子视图放到最前边,我们看到下面的结果红色视图放到了最前边,序号为2
[self.view bringSubviewToFront:Redlable];
2⃣️. //把某个子视图放到最后边,我们看到红色的跑到了最后边序号为0
[self.view sendSubviewToBack:Redlable];
3⃣️. //交换两个子视图的位置,第一个参数如果还不是视图的子视图,会添加上去,如果是那么只改变层次关系,我们看到下边红色和黄色的序号已经交换
[self.view insertSubview:Redlable aboveSubview:yellowlable];
4⃣️.//如果第一个参数还不是父视图的子视图,同样也会被添加上去,如果是,只改变层次关系
[self.view insertSubview:bluelable belowSubview:yellowlable];
�️. //为两个子视图换位置 两个参数是视图的序号,先添加的为0
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:2];
Δ四.层次与事件接收 superView.userInteractionEnabled=NO;
1.父视图不能接收事件,则子视图无法接受事件。
2.子视图超出父视图的部分,不能接收事件
3.同一个父视图下,最上面的视图,首先遭遇事件,如果能够响应,就不向下传递事件。如果不能响应,事件向下传递。
//创建UView
UIView *superView=[[UIView alloc] initWithFrame:self.view.bounds];
superView.backgroundColor=[UIColor orangeColor];
[self.view addSubview:superView];
[superView release];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(100, 100, 100, 50)];
btn.backgroundColor=[UIColor blackColor];
//为btn添加事件
[btn addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
//设置btn文字
[btn setTitle:@"按钮" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//这个控制是否可以接收事件,或者可以响应事件,(UIView的属性,默认是打开的YES)
//label也有这个属性,但是他的这个属性默认是关闭的
//UIImageView也是UIView,他同样有这个属性,他的空上属性默认是打关闭,、所有的view的子类都有这个方法
superView.userInteractionEnabled=NO;
//如果superView.userInteractionEnabled=NO;那么子视图btn就不能接受点击事件,当superView.userInteractionEnabled=YES;的时候子视图btn就可以相应点击事件
//裁剪,边界裁剪,如果打开这个属性,父视图会裁剪子视图超出父视图那部分,默认为NO
superView.clipsToBounds=YES;
[superView addSubview:btn];
五.UIView动画
【注】UIView动画只能修改关于坐标系的属性,以及色彩和透明度。
-(void)createAnimation
{
//UIView动画
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(160, 240, 0, 0)];
view.tag=11;
view.backgroundColor=[UIColor orangeColor];
[self.view addSubview:view];
[view release];
#if 1
//第一种方式
//启动动画
[UIView beginAnimations:nil context:nil];
//设置动画的启动的延迟时间
[UIView setAnimationDelay:3];
//设置动画持续时间,完成时间
[UIView setAnimationDuration:10];
[UIView setAnimationDelegate:self];
//设置回调方法
[UIView setAnimationDidStopSelector:@selector(stopAnimating)];
view.frame=self.view.bounds;
view.backgroundColor=[UIColor blueColor];
//执行动画
[UIView commitAnimations];
#else//第二种方式
void(^animationBlock)(void)=^(void)
{
view.frame=self.view.bounds;
view.backgroundColor=[UIColor redColor];
};
void(^completionBlock)(BOOL finished)=^(BOOL finished)
{
[UIView animateWithDuration:5 delay:0 options:0 animations:^{
view.frame=CGRectMake(150, 230, 20, 20);
view.backgroundColor=[UIColor grayColor];
} completion:nil];
};
//通过block方式实现动画
//第一个参籹是动画持续时间,第二个参数是动画启动的延迟时间,第四个参数是block,在这里我们来实现我们的动画,第五个block是完成的block
[UIView animateWithDuration:10 delay:2 options:0 animations:animationBlock completion:completionBlock];
#endif
}
-(void)stopAnimating
{
UIView *view=[self.view viewWithTag:11];
//启动动画
[UIView beginAnimations:nil context:nil];
//设置动画持续时间
[UIView setAnimationDuration:5];
view.frame=CGRectMake(150, 230, 20, 20);
view.backgroundColor=[UIColor redColor];
//执行动画
[UIView commitAnimations];
}
六.停靠模式
【注】停靠模式,是控制父视图改变大小时,子视图的变化方式。
【注】停靠模式并非服务于视图缩放,使用transform属性scale视图,根本是等比缩放,子视图也会等比缩放。停靠模式是服务于父视图边界修改后,子视图的重新布局。
//停靠模式
//设置停靠模式
subView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
//上边距自由调整,下边距自由调整,高度不变
// UIViewAutoresizingNone
//没有变化
// UIViewAutoresizingFlexibleLeftMargin
//自由调整左边距,保持右边距不变
// UIViewAutoresizingFlexibleWidth
//自由调整宽度,保持左右边距不变
// UIViewAutoresizingFlexibleRightMargin
//自由调整右边距,保持左边距不变
// UIViewAutoresizingFlexibleTopMargin
// UIViewAutoresizingFlexibleHeight
// UIViewAutoresizingFlexibleBottomMargin
滑块视图
//创建滑块视图
UISlider *slider=[[UISlider alloc] initWithFrame:CGRectMake(10, 400, 280, 20)];
//设置滑块视图最大值
slider.maximumValue=1;
//设置滑块视图最小值
slider.minimumValue=0;
slider.value=1;
//为我们的滑块视图添加事件
[slider addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
//设置slider停靠模式
// slider.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:slider];
[slider release];
【UIImageView】
//图片视图 UIImage UIImageView
【注】UIImage和UIImageView的关系,近似于NSString和UILabel的关系。
一.图片视图的基本使用
二.图片视图的动画效果
//创建UIImageView,也是继承于UIView
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(200, 200, 50, 50)];
//设置背景色
imageView.backgroundColor=[UIColor blueColor];
imageView.tag=11;
imageView.center=self.view.center;
[self.view addSubview:imageView];
[imageView release];
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];
}
//设置图片数据(这个数组的数据一定是UIImage类型)
imageView.animationImages=imageArray;
//设置我们的动画持续时间,我在多少秒内完成的图片切换
imageView.animationDuration=3;
//重复执行的次数,如果为0,无限次循环
imageView.animationRepeatCount=0;
//下面就开始启动我们的动画
[imageView startAnimating];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];
[btn setBackgroundColor:[UIColor orangeColor]];
[btn setFrame:CGRectMake(50, 20, 100, 40)];
[btn addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)onClick
{
UIImageView *imageView=(UIImageView*)[self.view viewWithTag:11];
//停止动画的
[imageView stopAnimating];
}