2012-07-15 79 views
0

重要:

我已经想通了这个问题,但主持人删除它由于某种原因: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 
+0

请确保您的视图是可见的,因为'-drawRect'不会触发,如果它不可见。 – Vervious 2012-07-16 02:18:34

+0

没有什么基本的东西,例如不可见,出口没有设置,错字等等。就是说,如果调用setNeedsDisplay结束的IBAction函数是从不同的类调用的,那么drawRect将不会被调用,即使调用了setNeedsDisplay 。现在的问题是为什么IBAction需要直接发送到AEGraphView? – WolfLink 2012-07-16 02:26:27

+1

然后,IBAction必须并且应该每次都以相同的方式工作(它相当于一个void类型的函数)。请仔细检查nielsbot的答案,因为'theView = [[AEGraphView alloc] init];'看起来很可疑(如果IBOutlet已正确连接,它应该已经被inited/extant),并且将视图引用替换为一个新的引用可见并且不会有drawRect调用。 尝试'NSLog(“%@”,self)'看看你是否触发了UIView类的正确实例的方法,因为它看起来不像它。 – Vervious 2012-07-16 22:55:05

回答

1

解决方案具有从按钮调用的函数,而不是从按钮调用的视图控制器中的函数调用。

0

你确定你的插座指向你的自定义UIView被连接起来吗?检查它是否是nil。另外,你是否确认你的函数被调用?

+0

视图已设置,我在第一次调用它时画了一些东西,如果再次调用它,它应该绘制其他东西。 – WolfLink 2012-07-15 23:06:21

+0

我有一个调用setNeedsDisplay的函数中的NSLog,setNeedsDisplay本身中的NSLog和drawRect中的NSLog。调用drawRect中的NSLog被调用一次,每次按下按钮时调用所有其他NSLog。 – WolfLink 2012-07-15 23:07:30

+0

你是否在'-setNeedsDisplay'和'-drawRect'中调用'super'? – nielsbot 2012-07-15 23:33:26

相关问题