iOS Programming: The Big Nerd Ranch Guide (4th Edition) 阅读笔记-关于View和View Hierarchy

关于 View

  • ViewUIView(或其子类)的一个实例
  • View负责对自己的绘制
  • View处理如触摸等事件
  • View存在于某个 View Hierarchy,且 View Hierarchy的根为applicationwindow
  • View既可以在Interface Builder,也可以在代码中创建

 

关于 ViewHierarchy

  • application启动时,会创建一个UIWindow的实例作为整个 application中所有 view的容器,然后便可以向其中添加其他 View。(在 AppDelegate 中对应为 application:didFinishLaunchingWithOptions:方法)
  • view被添加到window中后,它就成为了这个 window subview,同时该 view也可以拥有自己的 subview,于是最终便会形成一个由 view组成、window为根的view hierarchy
  • view hierarchy创建完毕后,它将会分为两步被绘制到屏幕上:
    • 每个 hierarchy中的 view(包括 window)绘制自己并 return给自己的 layer layer CALayer的实例)。
    • 所有的 layer被叠加在一起呈现在屏幕上。

 

关于 Frame,CGRect, CGPoint

  • UIView designated initializer initWithFrame:,该方法有一个参数类型为 CGRect,赋给该 view frame属性,然后生成一个 view的实例。
  • View frame指明 view的尺寸和 view与其 superview的相对位置。
  • CGRect是一个C的结构体,它包含另外两个结构体 CGPoint,分别用为 origin size,每个结构体中有 float类型的 x , y两个成员变量。

 

通过代码产生 viewview hierarchy的一般步骤:

  • 通过 CGRectMake方法传入尺寸和相对于 superview的位置坐标,生成一个 CGRect作为 viewframe
  • 通过 View initWithFrame:方法生成初始化 view实例
  • 对该 view实例进行需要的设置(如背景颜色等)
  • 将该 view通过其 superviewaddSubview方法添加到 hierarchy

同时会自动将该 view superview属性指向该 superviewsuperview属性为 weak以避免循环引用)

 

关于 point pixels

  • 为了兼顾多种分辨率和尺寸,iOS中采用 point描述尺寸和位置,而不是 pixels
  • non-Retina设备(iPhone 2G, 3G, 3GS

Render @1x 1 point对应 1 pixel

  • Retina设备(iPhone 4, 4s, 5, 5s ,6

Render @2x 1 point对应 2 pixel

  • Retina设备(iPhone 6 Plus

Render @3x 1 point对应 3 pixel

实际在渲染时为  @2.46x,但苹果为了方便开发者故使用 @3x素材,再缩放到@2.46x

 

自定义 view的绘制方法drawRect:

  • drawRect:方法是 view将其自身绘制到 layer上的过程,现有的 UIView均已实现了该方法,例如 UIButton中该方法在 view中间绘制亮蓝色文本,而自定义 view则需要自己覆写该方法。
  • 覆写 drawRect:的一般步骤:
    • 获取 view boundsboundsUIView的属性,是描述需要绘制的东西相对于view的尺寸和位置。( frame是描述 view相对于其superview的尺寸和位置)因此若需要一个全屏的 frame可以取 window bounds
    • 使用 UIBezierPath 类绘制图形
      • 创建 UIBezierPath类的实例
      • 为该实例指明绘制的路径(可参见开发者文档)
      • 配置该 path实例的属性(如 lineWidth
      • 对画笔进行设置(如stroke的颜色)
      • 向该实例发送消息(如 stroke)进行绘制
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
About the Book iPhone Programming: The Big Nerd Ranch Guide is based on Big Nerd Ranch's Beginning iPhone Bootcamp and takes the same approach as our tried and true classes: learning the necessary programming concepts by actually building appllications. About the Authors Joe Conway is the senior iPhone instructor at the Big Nerd Ranch and has been consulting on the iPhone platform since its creation. A University of Wisconsin graduate, he has been writing Objective-C and Cocoa code since the dawn of OS X. Joe wrote the materials for the exceptionally popular Big Nerd Ranch iPhone Bootcamp, which eventually evolved into this book. Aaron Hillegass is the founder of Big Nerd Ranch. A former employee at NeXT and Apple, he has twenty years experience with Objective-C and Cocoa. Aaron wrote "Cocoa Programming for Mac OS X," the standard text on developing Mac applications. Updated and expanded to cover iOS 7 and Xcode 5, iOS Programming: The Big Nerd Ranch Guide leads you through the essential concepts, tools, and techniques for developing iOS applications. After completing this book, you will have the know-how and the confidence you need to tackle iOS projects of your own. Based on Big Nerd Ranch's popular iOS Bootcamp course and its well-tested materials and methodology, this bestselling guide teaches iOS concepts and coding in tandem. The result is instruction that is relevant and useful. Throughout the book, the authors explain what's important and share their insights into the larger context of the iOS platform. You get a real understanding of how iOS development works, the many features that are available, and when and where to apply what you've learned. Here are some of the topics covered: Xcode 5, Instruments, and Storyboards Building interfaces using the iOS 7 aesthetic ARC and strong and weak references Handling touch events and gestures Toolbars, navigation controllers, and split view controllers Using Auto Layout to scale user interfaces Using Dynamic Type
《Android编程:The Big Nerd Ranch指南(第5版)》是一本针对Android编程的权威指南。这本书由Phillips、Stewart和Marsicano三位经验丰富的作者撰写,并由Big Nerd Ranch出版,对于想要学习和掌握Android编程的读者来说是一本必备的参考书。 这本书的第5版是一本全面更新和改进的指南,以帮助读者掌握最新的Android编程技术。它深入介绍了Android的核心概念和最佳实践,从而帮助读者全面了解Android应用的开发过程。 这本书以互动式的方式逐步引导读者进行Android应用开发,从创建一个简单的"Hello World"应用开始,逐渐深入介绍不同方面的开发技术,包括界面设计、数据存储和管理、网络通信、多媒体和设备功能等。 它采用了清晰明了的语言和丰富的示例代码,帮助读者理解和实践各种概念和技术。此外,这本书还通过挑战性练习和应用案例来培养读者的实际编程能力和解决问题的能力。 该书还引入了与现代开发实践相关的最新主题,例如响应式编程、单元测试和持续集成等。这些主题使读者能够更好地开发和维护高质量的Android应用。 总的来说,《Android编程:The Big Nerd Ranch指南(第5版)》是一本重要的Android编程指南,对于想要学习和应用这一技术的读者来说具有很高的实用价值。无论是初学者还是有经验的开发者,都可以通过这本书提供的深入理论和实践指导,加强他们的Android编程技能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值