iOS 较好的文章

iOS-UIView

2016-08-24  本文已影响6人  亦晴工作室

//演示UIView的使用

//1.UIView创建和使用

//  重要性:

//  (1) 常见控件父类或间接父类都是UIView

//UILabel->UIView

//UIImageView->UIView

//UIButton->UIControl->UIView

//  (2)自定义控件

//  继承与UIView, view加上其他控件

//  (3)使用UIView作为界面布局, UIView作为其他控件的容器

//理解: 显示一块矩形区域
    UIView *view1 = [[UIView alloc] init];

    view1.frame = CGRectMake(100, 100, 100, 100);

    view1.backgroundColor = [UIColor redColor];

    [self.view addSubview:view1];
//2.常用属性

//位置

//view1.frame

//只改大小, 不改位置
    view1.bounds = CGRectMake(0, 0, 50, 50);

    view1.backgroundColor = [UIColor yellowColor];
//使用tag区分不同控件
    //view1.tag = 100;
//设置中心点位置
    view1.center = CGPointMake(50, 50);
//是否打开用户交互
    //UILabel和UIImageView控件这个值是NO
//  按钮加入到UIImageView没有反应
    view1.userInteractionEnabled = YES;
//view设置圆角
    view1.layer.cornerRadius = 10;

    view1.clipsToBounds = YES;
//是否隐藏
    view1.hidden = YES;

    view1.hidden = NO;
//透明度
    view1.alpha = 0.7;
//3.常用方法

//view1 addSubview:<#(UIView *)#>

//界面上移除控件
    //[view1 removeFromSuperview];

//视图层级关系方法
    view1.frame = CGRectMake(100, 100, 100, 100);

    

    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(150, 150, 100, 100)];

    view2.backgroundColor = [UIColor blueColor];

    [self.view addSubview:view2];
//放到最前面
    [self.view bringSubviewToFront:view1];

//最后面
    [self.view sendSubviewToBack:view1];
//插入视图
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(125, 125, 100, 100)];

    view3.backgroundColor = [UIColor greenColor];

    //[self.view addSubview:view3];

    [self.view insertSubview:view3 belowSubview:view2];
//属性
    //所有子视图

    //self.view.subviews

    

    //父视图

    //self.view.superview
//4.使用UIView进行控件自定义和界面布局
    //  view+2个label+一个图片

    //原理: 只是原理展示, 控件定制需要继承

    UIView *photoView = [[UIView alloc] initWithFrame:CGRectMake(0, 300, 320, 100)];

    photoView.backgroundColor = [UIColor lightGrayColor];

    [self.view addSubview:photoView];

    ```

    //头像
UIImageView *headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 60, 60)];

headImageView.image = [UIImage imageNamed:@"defaultHead.png"];

[photoView addSubview:headImageView];



UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 20, 100, 30)];

label.text = @"风景";

[photoView addSubview:label];





UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 60, 100, 30)];

timeLabel.text = @"2014-08-26";

[photoView addSubview:timeLabel];



UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dealTap)];

[photoView addGestureRecognizer:tap];
    

    

    //5.UIView的动画(扩展: 仿射变换)
UIImageView *bullet = [[UIImageView alloc] initWithFrame:CGRectMake(10, 450, 20, 20)];

bullet.image = [UIImage imageNamed:@"bullet.png"];

[self.view addSubview:bullet];

// [UIView animateWithDuration:2 animations:^{

// bullet.frame = CGRectMake(300, 450, 50, 50);

// bullet.alpha = 0;

// }];

//

[UIView animateWithDuration:4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{

    bullet.frame = CGRectMake(300, 450, 50, 50);

    //bullet.alpha = 0;

} completion:^(BOOL finished) {

    [bullet removeFromSuperview];

}];


// 调整 UIView 的位置关系

  将一个UIView显示在最前面只需要调用其父视图的 bringSubviewToFront()方法。

将一个UIView层推送到背后只需要调用其父视图的 sendSubviewToBack()方法。

用第二层子视图 换 第一层子视图的位置  self.view是第0层

[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:2];

}

-(void)dealTap

{

NSLog(@"点击相册");

}

上一篇下一篇

猜你喜欢

热点阅读