1
我有一個非常簡單的項目,它涉及用drawRect繪製一個形狀,但我不相信我的drawRect函數實際上被調用。這是代碼。 (其他一切只是「單一視圖應用程序」的默認設置。)Objective C:實現drawRect
-(void)drawRect:(CGRect)aRect{
NSLog(@"testing...");
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextMoveToPoint(context, 50, 50);
CGContextAddLineToPoint(context, 100, 120);
CGContextAddLineToPoint(context, 20, 100);
CGContextClosePath(context);
[[UIColor greenColor] setFill];
[[UIColor redColor] setStroke];
CGContextDrawPath(context,kCGPathFillStroke);
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setNeedsDisplay];
}
任何建議都會很棒 - 感謝您的閱讀!
看起來您已將'drawRect:'放在viewcontroller類中。 Viewcontrollers不實現'drawRect:','UIView'的子類。 –
是的,你只需要在'self'上調用'setNeedsDisplay'。 –
好的,謝謝!仍然在學習如何做到這一點,所以將繼續留在它。 – Rogare