setNeedsDisplay不会调用drawRect

时间:2012-07-15 22:59:47

标签: iphone objective-c drawrect setneedsdisplay

重要的:

我已经找到了问题但主持人因某些原因将其删除了:

我尝试直接从按钮调用该函数,而不是在视图控制器中使用该函数。

我仍然不完全理解为什么它不起作用,所以我会接受这个问题的任何答案。

主要帖子

按下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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您确定指向您的自定义UIView的插座已连接好吗?检查它是否为nil。另外,你确认你的功能正在被调用吗?

相关问题