cocoa touch——UIView——draw

来源:互联网 发布:美工基础知识培训 编辑:程序博客网 时间:2024/04/29 07:00

draw

- (void)drawRect:(CGRect)rect;- (void)setNeedsDisplay;- (void)setNeedsDisplayInRect:(CGRect)rect;@property(nonatomic)                 BOOL              clipsToBounds;              // When YES, content and subviews are clipped to the bounds of the view. Default is NO.
注意:rect参考自身坐标系

演示

@interface FBView : UIView@end@implementation FBView- (void)drawRect:(CGRect)rect{    NSLog(@"drawRect rect = %@", NSStringFromCGRect(rect));}@end
- (void)viewDidLoad{    [super viewDidLoad];    NSLog(@"viewDidLoad");        fbView = [[FBView alloc] initWithFrame:CGRectMake(10, 20, 100, 200)];    fbView.contentMode = UIViewContentModeRedraw;    [self.view addSubview:fbView];}- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    NSLog(@"viewWillAppear");}- (void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    NSLog(@"viewDidAppear");}
output:
viewDidLoadviewWillAppeardrawRect rect = {{0, 0}, {100, 200}}viewDidAppear
first display(draw)位于viewViewAppear和viewDidAppear之间
draw不要直接调用,下列情况会发生draw调用:
  • first display
  • 调用setNeedsDisplay或setNeedsDisplayInRect
  • contentMode为UIViewContentModeRedraw,bounds change
分析:
UIView有一个draw flag和draw rect,默认draw flag为YES,draw rect为bounds,调用draw以后,draw flag设为NO,setNeedDisplay和setNeedsDisplayInRect就是设置draw flag和draw rect的(setNeedDisplay设置draw rect为bounds),当contentMode为UIViewContentModeRedraw,bounds change时,本质就是调用setNeedDisplay
0 0
原创粉丝点击