UIGraphicsGetCurrentContext和setNeedsDisplay和setNeedsLayout

想在initWithFrame或者其他函数里调用UIGraphicsGetCurrentContext()函数,

结果被告知Invalid Context,查资料。

按照文档中的说法,系统会维护一个CGContextRef的栈,而UIGraphicsGetCurrentContext()会取栈顶的CGContextRef,

正确的做法是只在drawRect里调用UIGraphicsGetCurrentContext(),

因为在drawRect之前,系统会往栈里面压入一个valid的CGContextRef,

除非自己去维护一个CGContextRef,否则不应该在其他地方取CGContextRef。

 

UIView的setNeedsDisplay和setNeedsLayout方法。

首先两个方法都是异步执行的。

而setNeedsDisplay会自动调用drawRect方法,这样可以拿到UIGraphicsGetCurrentContext,就可以画画了。

而setNeedsLayout会默认调用layoutSubViews,就可以处理子视图中的一些数据。

综上所述,setNeedsDisplay方便绘图,而layoutSubViews方便出来数据。


setNeedsDisplay

Marks the receiver’s entire bounds rectangle as needing to be redrawn.

- (void)setNeedsDisplayDiscussion

You can use this method or the setNeedsDisplayInRect: to notify the system that your view’s contents need to be redrawn. This method makes a note of the request and returns immediately. The view is not actually redrawn until the next drawing cycle, at which point all invalidated views are updated.

Note: If your view is backed by a CAEAGLLayer object, this method has no effect. It is intended for use only with views that use native drawing technologies (such as UIKit and Core Graphics) to render their content.

You should use this method to request that a view be redrawn only when the content or appearance of the view change. If you simply change the geometry of the view, the view is typically not redrawn. Instead, its existing content is adjusted based on the value in the view’s contentMode property. Redisplaying the existing content improves performance by avoiding the need to redraw content that has not changed.

Availability
  • Available in iOS 2.0 and later.
See Also Related Sample Code Declared InUIView.h setNeedsDisplayInRect:

Marks the specified rectangle of the receiver as needing to be redrawn.

- (void)setNeedsDisplayInRect:(CGRect) invalidRect Parameters
invalidRect

The rectangular region of the receiver to mark as invalid; it should be specified in the coordinate system of the receiver.

Discussion

You can use this method or the setNeedsDisplay to notify the system that your view’s contents need to be redrawn. This method adds the specified rectangle into the view’s current list of invalid rectangles and returns immediately. The view is not actually redrawn until the next drawing cycle, at which point all invalidated views are updated.

Note: If your view is backed by a CAEAGLLayer object, this method has no effect. It is intended for use only with views that use native drawing technologies (such as UIKit and Core Graphics) to render their content.

You should use this method to request that a view be redrawn only when the content or appearance of the view change. If you simply change the geometry of the view, the view is typically not redrawn. Instead, its existing content is adjusted based on the value in the view’s contentMode property. Redisplaying the existing content improves performance by avoiding the need to redraw content that has not changed.

Availability
  • Available in iOS 2.0 and later.
See Also Declared InUIView.h setNeedsLayout

Invalidates the current layout of the receiver and triggers a layout update during the next update cycle.

- (void)setNeedsLayoutDiscussion

Call this method on your application’s main thread when you want to adjust the layout of a view’s subviews. This method makes a note of the request and returns immediately. Because this method does not force an immediate update, but instead waits for the next update cycle, you can use it to invalidate the layout of multiple views before any of those views are updated. This behavior allows you to consolidate all of your layout updates to one update cycle, which is usually better for performance.

Availability
  • Available in iOS 2.0 and later.
See Also Declared InUIView.h
layoutIfNeeded

Lays out the subviews immediately.

- (void)layoutIfNeededDiscussion

Use this method to force the layout of subviews before drawing. Starting with the receiver, this method traverses upward through the view hierarchy as long as superviews require layout. Then it lays out the entire tree beneath that ancestor. Therefore, calling this method can potentially force the layout of your entire view hierarchy. The UIViewimplementation of this calls the equivalent CALayer method and so has the same behavior as CALayer.

Availability
  • Available in iOS 2.0 and later.
See Also Declared InUIView.h layoutSubviews

Lays out subviews.

- (void)layoutSubviewsDiscussion

The default implementation of this method does nothing.

Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.

You should not call this method directly. If you want to force a layout update, call the setNeedsLayout method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded method.

Availability
  • Available in iOS 2.0 and later.
See Also Related Sample Code

Declared in UIView.h




http://hi.baidu.com/threewind/item/62e86b322e0fbef2a8842883

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值