iOS_视图

1.层次结构

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [btn setTag:1000+1];
    btn.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:btn];
    
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 100)];
    label.text = @"This is a label lllll";
    //设置标记
    [label setTag:1000+2];
    [self.view addSubview:label];
    
    //查看某视图的子视图元素
    NSArray * array =[self.view subviews];
    NSLog(@"%@",array);
    
    //对子视图重新排序
    [self.view exchangeSubviewAtIndex:1001 withSubviewAtIndex:1002];
    
    //将子视图向前或向后移动
    [self.view bringSubviewToFront:btn];
    //从父视图中删除
    [label removeFromSuperview];

}

向子视图方向重设支持

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return YES;
}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

    CGRect directionRect;
    directionRect.origin = CGPointMake(0.0f, 0.0f);
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft||toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) {
        directionRect.size =  CGSizeMake(480.0f, 320.0f);
    }else{
        directionRect.size = CGSizeMake(320.0f, 480.0f);
    }
    
    float offset = 32.0f;
    for (UIView *subView in [contentView subviews]) {
        CGRect frame = CGRectInset(directionRect, offset, offset);
        [subView setFrame:frame];
        offset += 32.0f;
    }
}



2.几何特征

视图绘制使用Core Graphics中的drawRect.

注意:调用Core Graphics函数时,Quartz是从左下方建立坐标系。UIViews的原点在左上角。

    float offset = 10.0f;
    CGRect r1 = CGRectMake(100, 50, 100, 50);
    CGRect r2 = CGRectMake(200, 100, 100, 50);
    //创建大小相同位置不同的矩形
    CGRect r3 = CGRectOffset(r1, offset, offset);
    //创建中心点相同2个矩形的大小
    CGRect r5 = CGRectMake(200, 200, 100, 100);
    CGRect r = CGRectInset(r5, offset, offset);
    
    CGPoint p = CGPointMake(50, 60);
    CGSize  s = CGSizeMake(60, 70);
    CGAffineTransform transform = CGAffineTransformMake(10, 20, 30, 40, 2, 4);
    //NSStringFromUIEdgeInsets
    
    
    
    //打印CGRect等结构体
    NSLog(@"r1=%@",NSStringFromCGRect(r1));
    NSLog(@"r3=%@",NSStringFromCGRect(r3));
    NSLog(@"r=%@",NSStringFromCGRect(r));
    NSLog(@"r5=%@",NSStringFromCGRect(r5));
    NSLog(@"p=%@",NSStringFromCGPoint(p));
    NSLog(@"s=%@",NSStringFromCGSize(s));
    NSLog(@"t=%@",NSStringFromCGAffineTransform(transform));
    
    //string 恢复成矩形
    NSString *str = @"{0,0},{100,100}";
    CGRect r4 = CGRectFromString(str);
    
    
    
    //判断2个矩形是否相交
    BOOL b = CGRectIntersectsRect(r1, r2);
    if (b) {
        NSLog(@"相交");
    }else{
        NSLog(@"不相交=%d",b);
    }

添加递进式子视图:

//添加递进式子视图
-(void)addForwardSubviews{
    
    CGRect appRect = [UIScreen mainScreen].applicationFrame;
    UIView * myView = [[UIView alloc]initWithFrame:appRect];
    myView.backgroundColor = [UIColor whiteColor];
    self.view = myView;
    NSLog(@"%@",NSStringFromCGRect(appRect));
    CGPoint p = CGPointMake(appRect.origin.x, appRect.origin.y);
    NSLog(@"%@",NSStringFromCGPoint(p));
    appRect.origin = CGPointMake(0.0f, 20.0f);
    int count = 30;
    for (int i=0; i<count; i++) {
        float step = 10.0f;
        UIView * subview = [[UIView alloc]initWithFrame:CGRectInset(appRect, step*i, step*i)];
        
        int number = 256;
        float newColor1 = arc4random()%number/256.0;
        float newColor2 = arc4random()%number/256.0;
        float newColor3 = arc4random()%number/256.0;
        subview.backgroundColor = [UIColor colorWithRed:newColor1   green: newColor2  blue: newColor3 alpha:1];
        [myView addSubview:subview];
    }
}

视图的层次关系:

视图是现实可见元素的图形用户界面(GUI);


父类:

NSObject:


   UIView

     为NSObject子类


    UIControl

    UILabel

    UIWindow

    UIPorgressView

    UIActivityIndicatorView

为UIView的子控件


UIButton

    UITextField

    UISlider

    UISwitch

    UIPageControl

    UISegmentedControl

为UIControl子控件


视图的层次结构:



视图与坐标系:

每一个view的坐标系以他的 superview 的左上角为原点



Core Graphic数据结构:

CGRect:
CGPoint:
CGSize:



添加子视图

addSubview:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值