再谈UIView.bounds

引言

相信不少初学者都会对这个属性比较困惑吧。其实我也是.:)

所以,写着篇文章有两个初衷:一是为新人释义答惑;二是,看了好些文章,发现理解的有些问题,想纠正下。

什么是bounds

看了官方文档和这篇博客Understanding UIScrollView才恍然大悟:原来bound就是定义了一个视窗(viewport)啊。仅此而已,并不涉及到坐标变换之类的(我看了些文章,要么就是照抄斯坦福大学的视频,要么就是拿坐标变换、相对位置说事)。好吧,说的好简单。。。请继续往下看。

bounds就是一个视窗,不涉及坐标变换

为了验证bound不涉及坐标变换,我们首先自定义一个UIView GSCoordinateView:标出UIView的原点位置。然后添加两个view:parentViewchildViewchildView添加到parentView之中,parentView添加到根视图中。最后,改变parentView视图的bounds属性,观察原点的位置以确定是否发生了坐标变换。

  1. 标原点
    很简单,就是用(0,0)标出原点的位置。
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    // Drawing code
    NSDictionary *attributes=@{NSFontAttributeName:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]};
    NSAttributedString *originString=[[NSAttributedString alloc] initWithString:@"(0,0)" attributes:attributes];
   
    CGSize size=[originString size];
    CGRect originRect=CGRectMake(0, 0, size.width, size.height);
    [originString drawInRect:originRect];
}
  1. 设置视图
 // setup view
    self.view.backgroundColor=[UIColor whiteColor];
    // set up parent view
    self.parentView=[[GSCoordinateView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    self.parentView.contentMode=UIViewContentModeRedraw;
    self.parentView.backgroundColor=[UIColor cyanColor];
    [self.view addSubview:self.parentView];
    // set up child view
    self.childView=[[GSCoordinateView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
    self.childView.contentMode=UIViewContentModeRedraw;
    self.childView.backgroundColor=[UIColor purpleColor];
    [self.parentView addSubview:self.childView];
    // set up restore button
    UIButton *restoreButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [restoreButton setTitle:@"Restore" forState:UIControlStateNormal];
    restoreButton.frame=CGRectMake(8, 300, 100, 30);
    [restoreButton addTarget:self action:@selector(restore) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:restoreButton];
    // set up change bound button
    UIButton *changeBoundButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [changeBoundButton setTitle:@"Change Bound" forState:UIControlStateNormal];
    changeBoundButton.frame=CGRectMake(76, 300, 150, 30);
    [changeBoundButton addTarget:self action:@selector(changeBound) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeBoundButton];

注意: view的contentMode要设置成UIViewContentModeRedraw。否则,更改bounds后不会重绘。
官方的解释:

**The size of the bounds rectangle is coupled to the size of the frame rectangle, so that changes to one affect the other. ** Changing the bounds size grows or shrinks the view relative to its center point. The coordinates of the bounds rectangle are always specified in points.
Changing the frame rectangle automatically redisplays the receiver without invoking the drawRect: method. If you want the drawRect: method invoked when the frame rectangle changes, set the contentMode property to UIViewContentModeRedraw.

  1. 更改及还原parentViewbounds
- (void)restore {
    self.parentView.bounds=CGRectMake(0, 0, 200, 200);
}
- (void)changeBound {
    self.parentView.bounds=CGRectMake(-30, -30, 200, 200);
}
  1. 结果确认
  2. 默认


    默认位置
  3. 更改bounds后
    更改`bounds`后显示位置

    可以看出,parentView的原点并不是左上角位置,没有涉及到坐标系的变换。bounds只是定义了视图显示的窗口而已。 The bounds rectangle represents the visible portion of the view. That's it.

总结

遇到问题还是要弄清楚,多查资料,不要人云亦云。技术债迟早要还的。如果有疑惑,自己可以spike一下,验证自己的理解是否正确。另外Understanding UIScrollView值得读下,博主教你如何自己实现UIScrollView。代码已上传Github

参考资料

  1. 官方文档UIView.bounds
  2. Understanding UIScrollView
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,108评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,699评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,812评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,236评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,583评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,739评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,957评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,704评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,447评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,643评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,133评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,486评论 3 256
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,151评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,889评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,782评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,681评论 2 272

推荐阅读更多精彩内容