UIView

本文為大地瓜原創(chuàng),歡迎知識(shí)共享,轉(zhuǎn)載請(qǐng)注明出處。
雖然你不注明出處我也沒什么精力和你計(jì)較。
作者微信號(hào):christgreenlaw


UIView對(duì)于iOS開發(fā)者來說有多重要,不必多言。
本文是對(duì)蘋果官方UIView Class文檔的記錄及部分翻譯。
大地瓜重溫開始了。

不方便翻譯的地方會(huì)直接保留原文。


UIView class在屏幕上定義了一個(gè)矩形區(qū)域,以及在這個(gè)區(qū)域中管理內(nèi)容的接口(interface)。


OverView

在運(yùn)行時(shí)(At runtime),一個(gè)view對(duì)象處理它區(qū)域中的內(nèi)容繪制,也處理和其內(nèi)容相關(guān)的交互。UIView class自身對(duì)帶有背景色的矩形區(qū)域的(內(nèi)容)填充提供了基本行為。通過繼承UIView,并實(shí)現(xiàn)必要的繪圖、事件處理代碼,更加復(fù)雜的內(nèi)容可以得到展示。UIKit框架也包含了一組標(biāo)準(zhǔn)的子類,從簡(jiǎn)單的按鈕到復(fù)雜的表格,可以直接使用。例如:一個(gè)UILabel對(duì)象繪制一個(gè)文本字符串,一個(gè)UIImageView對(duì)象繪制一張圖片。

由于view對(duì)象是應(yīng)用于用戶交互的主要方式,他們就有很多責(zé)任。例如:

  • 繪制動(dòng)畫

    • Views用UIKit、Core Graphics、OpenGL ES這類技術(shù)在矩形區(qū)域內(nèi)繪制內(nèi)容
    • 一些view的屬性可以animated to new values
  • 布局和子view管理

    • 一個(gè)view可以有0個(gè)或多個(gè)子view
    • 每個(gè)view都可以根據(jù)父view定義自己的默認(rèn)resizing behavior
    • 一個(gè)view可以根據(jù)需要調(diào)整子view的大小和位置
  • 事件處理

    • A view is a responder and can handle touch events and other events defined by the UIResponder class.

    • Views can use the addGestureRecognizer: method to install gesture recognizers to handle common gestures.

Views可以嵌套其他的views,建立復(fù)雜的可視化層級(jí)。這就在被嵌套的view(subview)和進(jìn)行嵌套的parent view(superview)之間建立了父子關(guān)系。一般來說,子view的可視區(qū)域不會(huì)被superview的邊界剪切掉,但是在iOS中你可以通過修改clipToBounds屬性來修改這個(gè)行為。

clipToBounds

parent view可以包含任意數(shù)量的subview,一個(gè)subview只能有一個(gè)superview,此superview負(fù)責(zé)適當(dāng)?shù)財(cái)[放subview。

一個(gè)view的幾何形狀位置(geometry)由其frame、bounds、center屬性來決定。frame定義在其superview的坐標(biāo)系內(nèi)的origin和dimensions,通常在布局階段用來調(diào)整view的大小和位置。center屬性可用于在不改變大小的情況下調(diào)整view的位置。bounds定義了view的內(nèi)部dimension,通常在自定義繪圖代碼中獨(dú)立使用。framebounds的size是綁定到一起的,所以改變?nèi)我粋€(gè)rectangle的size都會(huì)把兩個(gè)屬性的size同時(shí)更新。

For detailed information about how to use the UIView class, see View Programming Guide for iOS.

Note

iOS 2.x中,UIView對(duì)象最大是1024*1024 points. 在iOS 3.0及以后,view的大小沒有要求,但是仍受到內(nèi)存的限制。最好還是盡可能的讓view越小越好。不管是運(yùn)行的iOS哪個(gè)版本,你都應(yīng)該考慮處理遠(yuǎn)大于screen尺寸的內(nèi)容。

Creating A View

To create a view programmatically, you can use code like the following:

CGRect  viewRect = CGRectMake(10, 10, 100, 100);
UIView* myView = [[UIView alloc] initWithFrame:viewRect];

這段代碼創(chuàng)建了view,將其放置于superview坐標(biāo)系的(10,10)上(一旦這個(gè)view被添加到superview上就立即生效)。要將subview添加到另一個(gè)view上,you use the addSubview: method。在iOS中,兄弟(sibling)視圖會(huì)相互覆蓋,不會(huì)引發(fā)任何錯(cuò)誤,這樣就可以實(shí)現(xiàn)復(fù)雜的擺放。你可以使用insertSubview:aboveSubview: and insertSubview:belowSubview: 方法在添加時(shí)聲明subview的順序。你也可以使用exchangeSubviewAtIndex:withSubviewAtIndex:方法來交換已經(jīng)添加的subview的位置順序。

創(chuàng)建view時(shí),給autoresizingMask屬性賦值合適的值極其重要,以此保證view可以正確的resize。

autoresizingMask

view的resize主要在APP的orientation改變時(shí)發(fā)生,但是也可能在其他任何時(shí)候發(fā)生。比如,調(diào)用setNeedsLayout 會(huì)強(qiáng)制view更新其布局。

The View Drawing Cycle

視圖的繪制是按需的。當(dāng)一個(gè)view第一次顯示時(shí),或者當(dāng)其全部或部分由于布局變化而可見時(shí),系統(tǒng)就會(huì)要求view繪制其內(nèi)容。對(duì)于包含使用UIKit或Core Graphics自定義內(nèi)容view來說(For views that contain custom content using UIKit or Core Graphics,)系統(tǒng)調(diào)用view的drawRect:方法。此方法的實(shí)現(xiàn)負(fù)責(zé)在當(dāng)前的graphics上下文中繪制view的內(nèi)容,which is set up by the system automatically prior to calling this method.這就給接下來展示到屏幕上的內(nèi)容創(chuàng)建了一個(gè)靜態(tài)可視化的展示。

當(dāng)你的實(shí)際內(nèi)容變化時(shí),你有責(zé)任通知系統(tǒng)你的view需要重繪。You do this by calling your view’s setNeedsDisplay or setNeedsDisplayInRect: method of the view. These methods let the system know that it should update the view during the next drawing cycle. Because it waits until the next drawing cycle to update the view, you can call these methods on multiple views to update them at the same time.

For detailed information about the view drawing cycle and the role your views have in this cycle, see View Programming Guide for iOS.

Animations

某些屬性的改變是可以動(dòng)畫的,有兩種方式來進(jìn)行動(dòng)畫:

  • iOS 4及以后,可以使用基于block的動(dòng)畫方法(推薦使用)
  • 使用begin/commit 動(dòng)畫方法

基于block的動(dòng)畫方法(例如 animateWithDuration:animations:)極大地簡(jiǎn)化了動(dòng)畫的創(chuàng)建。通過一個(gè)方法調(diào)用,你可以指定展現(xiàn)的動(dòng)畫以及動(dòng)畫的options。然而,基于block的動(dòng)畫僅僅在iOS 4及以后可以使用,如果你的APP運(yùn)行在這之前的版本上,you must use the beginAnimations:context: and commitAnimations class methods to mark the beginning and ending of your animations.

可以進(jìn)行動(dòng)畫的屬性如下:

  • frame
  • bounds
  • center
  • transform
  • alpha
  • backGroundColor

For more information about how to configure animations, see View Programming Guide for iOS.

Threading Considerations

APP用戶界面的操作必須在主線程發(fā)生。因此,你應(yīng)該永遠(yuǎn)從APP運(yùn)行的主線程調(diào)用UIView的動(dòng)畫方法。唯一不必這樣做的情況就是創(chuàng)建view本身。其他的操作都應(yīng)該在主線程中執(zhí)行。

Subclassing Notes

繼承的注意事項(xiàng)。

UIView類對(duì)于需要用戶交互的可視化內(nèi)容來說是一個(gè)關(guān)鍵的繼承點(diǎn)(subclassing point)。盡管有很多理由繼承UIView,還是建議你只有系統(tǒng)提供的標(biāo)準(zhǔn)view不能完成你需要的功能時(shí)你再這樣做。繼承需要你做更多的工作來實(shí)現(xiàn)view,調(diào)整其性能。

For information about ways to avoid subclassing, see Alternatives to Subclassing.

Methods to Override

When subclassing UIView, there are only a handful of methods you should override and many methods that you might override depending on your needs. Because UIView is a highly configurable class, there are also many ways to implement sophisticated view behaviors without overriding custom methods, which are discussed in the Alternatives to Subclassing section. In the meantime, the following list includes the methods you might consider overriding in your UIView subclasses:

  • Initialization:

    • initWithFrame: - It is recommended that you implement this method. You can also implement custom initialization methods in addition to, or instead of, this method.

    • initWithCoder: - Implement this method if you load your view from an Interface Builder nib file and your view requires custom initialization.

    • layerClass Use this property only if you want your view to use a different Core Animation layer for its backing store. For example, if your view uses tiling to display a large scrollable area, you might want to set the property to the CATiledLayer class.

  • Drawing and printing:

    • drawRect: - Implement this method if your view draws custom content. If your view does not do any custom drawing, avoid overriding this method.

    • drawRect:forViewPrintFormatter: - Implement this method only if you want to draw your view’s content differently during printing.

  • Constraints:

  • Layout:

    • sizeThatFits: - Implement this method if you want your view to have a different default size than it normally would during resizing operations. For example, you might use this method to prevent your view from shrinking to the point where subviews cannot be displayed correctly.

    • layoutSubviews - Implement this method if you need more precise control over the layout of your subviews than either the constraint or autoresizing behaviors provide.

    • didAddSubview:, willRemoveSubview: - Implement these methods as needed to track the additions and removals of subviews.

    • willMoveToSuperview:, didMoveToSuperview - Implement these methods as needed to track the movement of the current view in your view hierarchy.

    • willMoveToWindow:, didMoveToWindow - Implement these methods as needed to track the movement of your view to a different window.

-Event Handling:

Alternatives to Subclassing

Many view behaviors can be configured without the need for subclassing. Before you start overriding methods, consider whether modifying the following properties or behaviors would provide the behavior you need.

  • addConstraint: - Define automatic layout behavior for the view and its subviews.

  • autoresizingMask - Provides automatic layout behavior when the superview’s frame changes. These behaviors can be combined with constraints.

  • contentMode - Provides layout behavior for the view’s content, as opposed to the frame
    of the view. This property also affects how the content is scaled to fit the view and whether it is cached or redrawn.

  • hidden or alpha - Change the transparency of the view as a whole rather than hiding or applying alpha to your view’s rendered content.

  • backgroundColor - Set the view’s color rather than drawing that color yourself.

  • Subviews - Rather than draw your content using a drawRect: method, embed image and label subviews with the content you want to present.

  • Gesture recognizers - Rather than subclass to intercept and handle touch events yourself, you can use gesture recognizers to send an Target-Action to a target object.

  • Animations - Use the built-in animation support rather than trying to animate changes yourself. The animation support provided by Core Animation is fast and easy to use.

  • Image-based backgrounds - For views that display relatively static content, consider using a UIImageView object with gesture recognizers instead of subclassing and drawing the image yourself. Alternatively, you can also use a generic UIView
    object and assign your image as the content of the view’s CALayer object.

Animations are another way to make visible changes to a view without requiring you to subclass and implement complex drawing code. Many properties of the UIView
class are animatable, which means changes to those properties can trigger system-generated animations. Starting animations requires as little as one line of code to indicate that any changes that follow should be animated. For more information about animation support for views, see Animations.
For more information about appearance and behavior configuration, see About Views in UIKit User Interface Catalog.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容