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

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);
        }];

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值