重要:
我已經想通了這個問題,但主持人刪除它由於某種原因:setNeedsDisplay不會叫的drawRect
我試圖直接從按鈕調用該函數,而不必在視圖功能控制器。
我仍然不完全明白爲什麼它不起作用,所以我會接受任何問題的答案。
主帖
我想按一個UIButton後做一些繪圖。我的繪圖是在UIView的子類中完成的。我在我的ViewController中有這個子類的一個實例。我的ViewController中也有一個UIButton,它調用子類的一個函數。在該功能我叫
[self setNeedsDisplay];
當我加載了我的應用程序,是的drawRect在視圖加載時調用一次,但絕不會在再次調用,雖然setNeedsDisplay被稱爲每次按下按鈕時。我看了很多類似的問題,但我還沒有找到解決方案。
我會後我的所有代碼在這裏:
AEViewController.h
#import <UIKit/UIKit.h>
#import "AEGraphView.h"
@interface AEViewController : UIViewController{
IBOutlet AEGraphView *theView;
}
-(IBAction)updateAEGraphView:(id)sender;
@end
AEViewController.m
#import "AEViewController.h"
@interface AEViewController()
@end
@implementation AEViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
theView = [[AEGraphView alloc] init];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
else {
return YES;
}
}
-(IBAction)updateAEGraphView:(id)sender{
[theView UpdateView];
}
@end
AEGraphView.h
#import <UIKit/UIKit.h>
@interface AEGraphView : UIView{
CGContextRef mainContext;
int power;
int multiplier;
}
-(void)prepareGraphics;
- (void) drawLineFrom:(CGPoint)p1 To:(CGPoint)p2;
-(void)UpdateView;
@end
AEGraphView.m
#import "AEGraphView.h"
@implementation AEGraphView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
[self prepareGraphics];
// Drawing code
NSLog(@"called");
if (power == 0) {
[self drawLineFrom:CGPointMake(100,100) To:CGPointMake(-50, -2)];
}
else {
[self drawLineFrom:CGPointMake(0, 0) To:CGPointMake(150, 150)];
}
[super drawRect:rect];
}
-(void)prepareGraphics{
mainContext = UIGraphicsGetCurrentContext();
CGContextSetShouldAntialias(mainContext, TRUE);
CGContextSetLineWidth(mainContext, 2);
CGContextSetLineCap(mainContext, kCGLineCapRound);
}
- (void) drawLineFrom:(CGPoint)p1 To:(CGPoint)p2{
NSLog(@"%f,%f to %f,%f", p1.x,p1.y,p2.x,p2.y);
// Begin a path.
CGContextBeginPath(mainContext);
// Add a line to the path.
CGContextMoveToPoint(mainContext, p1.x, p1.y);
CGContextAddLineToPoint(mainContext, p2.x, p2.y);
// Stroke and reset the path.
CGContextStrokePath(mainContext);
}
-(void)UpdateView{
if (power == 0) {
power = 1;
}
else {
power = 0;
}
[self setNeedsDisplay];
}
@end
請確保您的視圖是可見的,因爲'-drawRect'不會觸發,如果它不可見。 – Vervious 2012-07-16 02:18:34
沒有什麼基本的東西,例如不可見,出口沒有設置,錯字等等。就是說,如果調用setNeedsDisplay結束的IBAction函數是從不同的類調用的,那麼drawRect將不會被調用,即使調用了setNeedsDisplay 。現在的問題是爲什麼IBAction需要直接發送到AEGraphView? – WolfLink 2012-07-16 02:26:27
然後,IBAction必須並且應該每次都以相同的方式工作(它相當於一個void類型的函數)。請仔細檢查nielsbot的答案,因爲'theView = [[AEGraphView alloc] init];'看起來很可疑(如果IBOutlet已正確連接,它應該已經被inited/extant),並且將視圖引用替換爲一個新的引用可見並且不會有drawRect調用。 嘗試'NSLog(「%@」,self)'看看你是否觸發了UIView類的正確實例的方法,因爲它看起來不像它。 – Vervious 2012-07-16 22:55:05