uikit——UIView——bounds frame

来源:互联网 发布:linux中etc是什么意思 编辑:程序博客网 时间:2024/06/13 22:07

coordinate system

// animatable. do not use frame if view is transformed since it will not correctly reflect the actual location of the view. use bounds + center instead.@property(nonatomic) CGRect            frame;// use bounds/center and not frame if non-identity transform. if bounds dimension is odd, center may be have fractional part@property(nonatomic) CGRect            bounds;      // default bounds is zero origin, frame size. animatable@property(nonatomic) CGPoint           center;      // center is center of frame. animatable@property(nonatomic) CGAffineTransform transform;   // default is CGAffineTransformIdentity. animatable
解释:
  • frame:参考父view坐标系
  • bounds:参考view自身坐标系
  • center:参考父view坐标系
注1:view尺寸与坐标系无关,即frame.size和bound.size恒一致
注2:独立改变frame.size不改变center,frame.size改变以center为中心四周均匀变化

应用

- (void)showView{    self.view.backgroundColor = [UIColor purpleColor];        {        UIView *parent = [[UIView alloc] initWithFrame:CGRectMake(60, 30, 200, 160)];        UIView *sub = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];                parent.backgroundColor = [UIColor blueColor];        sub.backgroundColor = [UIColor brownColor];                [self.view addSubview:parent];        [parent addSubview:sub];    }        {        UIView *parent = [[UIView alloc] initWithFrame:CGRectMake(60, 210, 200, 160)];        UIView *sub = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];                parent.backgroundColor = [UIColor blueColor];        sub.backgroundColor = [UIColor brownColor];                [self.view addSubview:parent];        [parent addSubview:sub];                CGRect bounds = parent.bounds;        bounds.origin = CGPointMake(20, 20);        parent.bounds = bounds;    }        {        UIView *parent = [[UIView alloc] initWithFrame:CGRectMake(60, 390, 200, 160)];        UIView *sub = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];                parent.backgroundColor = [UIColor blueColor];        sub.backgroundColor = [UIColor brownColor];                [self.view addSubview:parent];        [parent addSubview:sub];                CGRect bounds = parent.bounds;        bounds.origin = CGPointMake(-20, -20);        parent.bounds = bounds;    }}
总结:
  • 子view左上角pos相对于父view左上角pos的offset为子view.frame.origin减去父view.bounds.origin,即子view在父坐标系中pos减去父view在自身坐标系中pos
注:bounds.origin改变本质是改变view自身坐标系原点(子view参考坐标系),因此会影响子view显示
0 0
原创粉丝点击