setNeedsDisplay仍然无法正常工作

时间:2013-04-29 15:12:53

标签: ios objective-c drawrect setneedsdisplay

以前我在stackoverflow中问了一个问题,那就是setNeedsDisplay无法正常工作。 我不是一个懒惰的人,我已经尝试了一切,但它仍然无法正常工作。 也许我找不到问题所在。 任何人都可以帮我找出问题吗?

//viewcontroller.m
#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController
@synthesize frequency;
- (void)viewDidLoad
{
    hi = [[cro alloc]init];
    [self.view addSubview:hi];
    CGFloat fu = frequency.value;
    [hi changefreq:fu];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)change:(id)sender
{
    CGFloat fu = frequency.value;
    [hi changefreq:fu];
}
@end
//cro.m
#import "cro.h"

@implementation cro
@synthesize fffff;
CGFloat freee;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
-(void)drawRect:(CGRect)rect
{

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 1);
    CGContextSetLineJoin(context, kCGLineJoinRound);
    NSLog(@"hi i am here %f",fffff);
    const CGFloat amplitude = 200/2;
    for(CGFloat x = 0; x < 600; x += 0.5)
    {
        CGFloat y = amplitude * cosf(2 * M_PI * (x / 600) * freee) + 200;
        if(x == 0)
            CGContextMoveToPoint(context, x, y);
        else
            CGContextAddLineToPoint(context, x, y);
    }
    CGContextSetStrokeColorWithColor(context, [[UIColor greenColor] CGColor]);
    self.clearsContextBeforeDrawing = NO;
    CGContextStrokePath(context);
}
-(void)changefreq:(CGFloat)fre
{
    NSLog(@"fre= %f",fre);
    freee = fre;
    [self setNeedsDisplay];
}

@end

这是项目 http://www.sendspace.com/file/lzt4b0

3 个答案:

答案 0 :(得分:2)

您可以致电[[cro alloc] init]初始化您的观点。由于您没有调用initWithFrame:,因此会导致视图的宽度和高度为零。在这样的视图上调用setNeedsDisplay没有任何效果,因为没有任何内容可以显示。

viewDidLoad中的第一行更改为

hi = [[cro alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];

(根据需要调整大小)

或者,您可能希望使用故事板中已有的cro实例,而不是实例化新实例。故事板实例是您正在看到的实例,hi实例实际上对您当前的代码不可见。 (顺便说一句,如果你希望其他人阅读你的代码,你可能想要开始使用合理的变量和类名。)

答案 1 :(得分:0)

ViewController.m ViewdidLoad方法中的以下更改将执行修复

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    hi = [[cro alloc]initWithFrame:CGRectMake(201, 58, 414, 571)];
    [self.view addSubview:hi];
    CGFloat fu = frequency.value;
    [hi changefreq:fu];
}

答案 2 :(得分:0)

如果您执行以下操作,则对我有用:

  • 使用属性而不是ivar
    • 现在,您可以通过cro
    • 访问self.cro个实例了
    • 使用子视图在您的故事板中连接视图控制器的插座cro(在您上传的项目中已有一个名为此属性的属性)
  • -[ViewController viewDidLoad:]中删除您创建新cro个实例的行,并将其作为子视图添加到self
  • 使用cro - fffff -[cro changefreq:]-[cro drawRect:]
  • 中的{1}} {

(只有我的更改才能从您上传的项目中复制)

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet cro *cro;
@property (strong, nonatomic) IBOutlet UISlider *frequency;
-(IBAction)change:(id)sender;
@end

@implementation ViewController
@synthesize frequency;
- (void)viewDidLoad
{
    CGFloat fu = frequency.value;
    [self.cro changefreq:fu];
    //NSLog(@"%f",hi.fffff);
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)change:(id)sender
{
    CGFloat fu = frequency.value;
    [self.cro changefreq:fu];
    //[hi setNeedsDisplay];
}
@end

@interface cro : UIView
@property (nonatomic) CGFloat fffff;
-(void)changefreq:(CGFloat)fre;
@end

@implementation cro
@synthesize fffff;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        fffff = 15;
        // Initialization code
    }
    return self;
}
-(void)drawRect:(CGRect)rect
{

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 1);
    CGContextSetLineJoin(context, kCGLineJoinRound);
    NSLog(@"hi i am here %f",fffff);
    const CGFloat amplitude = 200/2;
    for(CGFloat x = 0; x < 600; x += 0.5)
    {
        CGFloat y = amplitude * cosf(2 * M_PI * (x / 600) * fffff) + 200;
        if(x == 0)
            CGContextMoveToPoint(context, x, y);
        else
            CGContextAddLineToPoint(context, x, y);
    }
    CGContextSetStrokeColorWithColor(context, [[UIColor greenColor] CGColor]);
    self.clearsContextBeforeDrawing = NO;
    CGContextStrokePath(context);
}
-(void)changefreq:(CGFloat)fre
{
    NSLog(@"fre= %f",fre);
    fffff = fre;
    [self setNeedsDisplay];
}

@end