您的位置:首页 > 产品设计 > UI/UE

IOS开发UI-------UIView(视图)

2016-01-18 16:03 465 查看
UIView

特别注意:UIView是所有控件的父类

以下是view得各种属性设置(背景颜色,tag值,设置圆角,位于此view之下的view是否遮盖,边框大小,边框颜色)

UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(10, 50, 100, 100)];

//给视图添加背景颜色
aView.backgroundColor = [UIColor redColor];
//每一个View都有一个tag的属性
aView.tag = 1000;
//设置圆角
aView.layer.cornerRadius = 15;
//告诉layer将位于它之下的layer都遮盖住
aView.layer.masksToBounds = YES;
//设置边框大小
aView.layer.borderWidth = 3;
//设置边框颜色
aView.layer.borderColor = [UIColor greenColor].CGColor;

[self.view addSubview:aView];

/*
理解子视图和父视图得概念
UIView *superview = aView.superview;
NSArray *subViewARR = aView.subviews;

*/
//每个视图都有添加子视图的方法:addSubview:
//    [aView addSubview:<#(nonnull UIView *)#>]


设置View的大小位置颜色

UIView *Bview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
Bview.backgroundColor = [UIColor cyanColor];
/*
关于视图的位置:UIView类给了三个属性:frame;bounds;center;
frame和bounds的区别
frame 使有参照物的,参考父视图的左上角
bounds使参考它自己

*/

NSLog(@"aview.frame = %@",[NSValue valueWithCGRect:aView.frame]);

NSLog(@"aview.bounds = %@",[NSValue valueWithCGRect:aView.bounds]);
NSLog(@"aView.center = %@",[NSValue valueWithCGPoint:aView.center]);


这里需要提及一个View的属性userInteractionEnabled

/*
//一个BOOL值,决定是否用户触发的事件被该视图对象忽略和把该视图对象从事件相应队列中移除
//UIImageView 会默认userInteractionEnabled为NO,即不能点击; */

aView.userInteractionEnabled = YES;
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"7`TY]FD{UB3TXDMCR)BAS(P.jpg"]];
image.frame = CGRectMake(150, 200, 200, 300);
[self.view addSubview:image];


以下是用上面的View做得一个小UI

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"点击wo" forState:UIControlStateNormal];
//    [button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor orangeColor];

button.frame = CGRectMake(10, 200, 100, 100);
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

_flag = YES;//这是一个全局变量


Button实现的方法在这里

-(void)buttonAction:(UIButton *)sender{
NSLog(@"点击了一下");
UIView *newView = [self.view viewWithTag:1000];

_flag =!_flag;
newView.backgroundColor  = _flag?[UIColor redColor]:[UIColor yellowColor];

if (!_flag) {

//此方法是将VIEW移动(持续动画)
[UIView animateWithDuration:2.0 animations:^{
//(移动到指定的位置)
newView.frame = CGRectMake(200, 50, 120, 120);
}];
}else{
[UIView animateWithDuration:2.0 animations:^{
newView.frame = CGRectMake(10, 50, 120, 120);
}];

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: