UIView

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //创建一个新的窗口对象,并且和屏幕大小一样
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    //设置窗口的背景色为白色
    self.window.backgroundColor = [UIColor colorWithRed: arc4random()% 256 / 255.0 green:arc4random()% 256 / 255.0 blue:arc4random()% 256 / 255.0 alpha:1.0];
    //初始化与窗口等大的视图
    //将window比作画板的话,此视图就是画纸
    //创建一个UIView对象的过程
    //1、初始化,并且指定大小
    _containView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //
    [self.window addSubview:_containView];
    //2、设置背景颜色
    //视图如果不给定背景色,默认透明
    
    //3、将视图添加到window上面进行显示
    _containView.backgroundColor = [UIColor whiteColor];
    
     
    //创建一个红色视图,添加到containView上面进行显示
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    NSLog(@"%@",NSStringFromCGPoint(view.center));//此函数可以将结构体CGPoint转换为字符串,进行打印
    //center视图中心点
    CGPoint newCenter = CGPointMake(0, 0);
    //一个视图的中心点与视图的frame是息息相关的。center与frame一样,都是相对于父视图来说的
    //视图中心点center.x (数学)
    newCenter.x = view.frame.origin.x +view.frame.size.width / 2;
    newCenter.y = view.frame.origin.y +view.frame.size.height / 2;
    view.center = newCenter;
    NSLog(@"%@",NSStringFromCGPoint(view.center));

    view.backgroundColor = [UIColor redColor];
    [_containView addSubview:view];
    [view release];
   /*
    1、frame,center是相对于父视图而言的,改变视图本身的frame,center会直接影响自身在父视图上的显示位置
    2、bounds是相对于自身而言的,改变bounds的值会影响自身坐标系原点的位置。进而影响子视图在其上的显示位置
    3、一个视图bounds的默认值为(0,0,宽,高),因为bounds前面的两个值x,y代表的含义是视图本身左上角点重合,所有是0
    4、改变一个视图的bounds(x,y)不会造成自身位置的变化,因为父视图的bounds没有改变,自身的frame以及center没有任何变化,所以不会动
    
    */

posted on 2016-02-21 08:09  哥依然帅气  阅读(105)  评论(0编辑  收藏  举报

导航