-3

以前我在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

4

3 回答 3

2

你通过调用初始化你的视图[[cro alloc] init]。由于您没有调用initWithFrame:这将导致视图的宽度和高度为零。调用setNeedsDisplay这样的视图没有任何效果,因为没有可显示的内容。

将您的第一行更改viewDidLoad为类似

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

(根据需要调整大小)

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

于 2013-04-29T15:47:48.940 回答
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];
}
于 2013-04-29T15:48:45.797 回答
0

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

  • 使用属性而不是 ivar
    • 现在您可以cro通过self.cro
    • 在情节提要中将视图控制器的出口cro(在您上传的项目中已经有一个像这样命名的属性)与子视图连接
  • -[ViewController viewDidLoad:]在创建新实例的行中删除cro并将其作为子视图添加到self
  • 在你的-class中cro使用 ivar和fffff-[cro changefreq:]-[cro drawRect:]

(所有内容都是从您上传的项目中复制的,仅包含我的更改)

@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
于 2013-04-29T16:00:46.233 回答