Chapter4: View - The Big Nerd Ranch Guide 读书笔记

View的基本概念:

  • View是一个UIView的实例,或者UIView子类的实例
  • View知道怎么画它自己
  • View 可以handle Event,比如:触摸
  • View存在在多个View的hierarchy中,在这个hierarchy的root是application的window

View的hierarchy:
每一个application都有一个UIWindow,UIWindow是一个app里面所有View的容器。当app运行的时候,window被创建,在window创建后,你可以添加其他view上去。
当一个view添加到window上,它被叫做window的subview。view也可以被添加subview。一旦view hierarchy被创建,它会画到屏幕上。这个过程可以分为2步:

  1. 在hierarchy上的每个view,包含window,自己画自己。它把自己画到它的layer上,layer是一个CALayer。你可以想象一个view的layer是一个位图
  2. 所有view的layer,在屏幕上编辑在一起
    一个例子:
    这里写图片描述

像UIButton这些类已经知道如何render他们自己到layer上。比如: 你创建了一个实例UILabel,你告诉他要显示什么text,但是你不需要告诉iOS怎么去画,apple会去负责画出来。
你也可以自定义override func drawRect(rect: CGRect),去告诉iOS怎么去画。

frame定义了View的size和相对于它的superView的位置。由一个CGPoint(x, y)和size(宽和长)决定。而bounds,继承于UIView,是一个方形,定义了view画自己的区域。每一个view都有一个坐标系统,bounds是一个view的方型在view自己的坐标系统里面。而frame的坐标系统时相对于他的superview的。view总是方形的。
为什么有了frame还要有bounds?
因为他们有他们各自清楚的目的,frame用在编辑阶段,所有的view会被编辑在一起。
frame用在render阶段,把view画出来。
frame和bounds对比图如下:
这里写图片描述

在window上显示一个view:

        let firstFrame = CGRectMake(160, 240, 100, 150)
        var firstView:BNRView = BNRView(frame:firstFrame)
        firstView.backgroundColor = UIColor.redColor()
        self.window!.addSubview(firstView)
        self.window?.backgroundColor = UIColor.whiteColor()
        self.window?.makeKeyAndVisible()

在iOS编程里面,都是用point,而不是pixel。这样Retina和non-retina的屏可以用同样的布局。point和pixel的换算:

  • Retina: 一个pixel = 半个point
  • non-Retina: 一个pixel = 一个point
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Title: Android Programming: The Big Nerd Ranch Guide, 3rd Edition Author: Bill Phillips, Chris Stewart Length: 624 pages Edition: 3 Language: English Publisher: Big Nerd Ranch Guides Publication Date: 2017-02-09 ISBN-10: 0134706056 ISBN-13: 9780134706054 Table of Contents Chapter 1. Your First Android Application Chapter 2. Android and Model-View-Controller Chapter 3. The Activity Lifecycle Chapter 4. Debugging Android Apps Chapter 5. Your Second Activity Chapter 6. Android SDK Versions and Compatibility Chapter 7. UI Fragments and the Fragment Manager Chapter 8. Displaying Lists with RecyclerView Chapter 9. Creating User Interfaces with Layouts and Widgets Chapter 10. Using Fragment Arguments Chapter 11. Using ViewPager Chapter 12. Dialogs Chapter 13. The Toolbar Chapter 14. SQLite Databases Chapter 15. Implicit Intents Chapter 16. Taking Pictures with Intents Chapter 17. Two-Pane Master-Detail Interfaces Chapter 18. Localization Chapter 19. Accessibility Chapter 20. Data Binding and MVVM Chapter 21. Unit Testing and Audio Playback Chapter 22. Styles and Themes Chapter 23. XML Drawables Chapter 24. More About Intents and Tasks Chapter 25. HTTP and Background Tasks Chapter 26. Loopers, Handlers, and HandlerThread Chapter 27. Search Chapter 28. Background Services Chapter 29. Broadcast Intents Chapter 30. Browsing the Web and WebView Chapter 31. Custom Views and Touch Events Chapter 32. Property Animation Chapter 33. Locations and Play Services Chapter 34. Maps Chapter 35. Material Design Chapter 36. Afterword

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值