关于使用Objective C在iPhone中更新View

时间:2010-04-05 07:03:35

标签: objective-c iphone-sdk-3.0 core-graphics

我有一个名为testScene的场景,它的工作原理如下:

@interface testScene : myScene {
 IBOutlet UIView *subview;
 IBOutlet UIView *drawingCanvasView;
 IBOutlet UIButton *update;
}

- (void)updateDrawingCanvas: (id) sender;

当用户点击按钮更新时,它将运行updateDrawingCanvas方法。 所以,我有一个drawingCanvasView,它给出了一个drawingCanvas.h和.m,就像这样:

#import <UIKit/UIKit.h>


@interface DrawingCanvasView : UIView {
 CGImageRef image;

}

-(void)setNeedsDisplayInRect:(CGContextRef)context;

@end

在DrawingCanvasView中,我有一个像这样的drawRect方法:

 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextSetLineWidth(context, 2.0); 
 CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 
 CGContextMoveToPoint(context, 0.0f, 0.0f);
 CGContextAddLineToPoint(context, 100.0f, 100.0f); 
 CGContextStrokePath(context);

我希望用户点击按钮,然后执行此操作,所以我添加了一个名为setNeedsDisplayInRect的新方法:

 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextSetLineWidth(context, 2.0); 
 CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor); 
 CGContextMoveToPoint(context, 0.0f, 0.0f);
 CGContextAddLineToPoint(context, 200.0f, 200.0f); 
 CGContextStrokePath(context);

但我无法在updateDrawingCanvas方法中调用它,它的工作方式如下:

- (void)updateDrawingCanvas: (id) sender{
 NSLog(@"loaded");
 [DrawingCanvasView setNeedsDisplayInRect:UIGraphicsGetCurrentContext()];
}

我的逻辑/概念对吗?或者我做错了什么,thx。

1 个答案:

答案 0 :(得分:3)

不,您不会覆盖setNeedsDisplayInRect:。您在drawRect:中实施了绘图代码,当您致电setNeedsDisplayInRect:时,框架将确保您的drawRect:被调用。