0

UIViewを使用してに正弦波を描き、 にdrawrectリンクしていUIViewControllerます。ビューコントローラーを最初にロードしたときに一度だけ描画できます。の値を変更すると、正弦波の周波数が変更UISliderされます。ViewControllerの機能を使用していますが、setNeedsDisplay機能しIBActionません。これが私のコードです

//CROO.m
#import "CROO.h"

@implementation CROO
@synthesize frequency;
CGFloat fffff;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frequency = fffff;
        [self setNeedsDisplay];
        // 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)setFrequency:(CGFloat)f
{
    frequency = f;
    [self hello];
    NSLog(@"fre = %f",fffff);
}
-(void)hello
{
    fffff = frequency;
    [self setNeedsDisplay];
}
@end
//CosViewController.m
#import "CosViewController.h"
@interface CosViewController ()
@end

@implementation CosViewController
@synthesize frequency,currfrelab;
@synthesize cro;
float tempfreq;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //cro = [[CROO alloc]initWithFrame:cro.frame];
    cro = [[CROO alloc]initWithFrame:cro.frame];

    //[self.view addSubview:cro];
    [cro setFrequency:frequency.value];
    //[self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:YES];
    NSLog(@"freq = %f",cro.frequency);
    currfrelab.text = [NSString stringWithFormat:@"Frequency = %f",frequency.value];
    //[self.view setNeedsDisplayInRect:cro.frame];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)updatefreq:(id)sender
{
   // cro = [[CROO alloc]init];
    CGFloat freq = [(UISlider *)sender value];
   // [self.view addSubview:cro];
    [cro setNeedsDisplay];
    //[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate date]];
    currfrelab.text = [NSString stringWithFormat:@"Frequency = %f",freq];
    //[self.view performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:YES];

}
@end
4

2 に答える 2

0

init は、よく init で 1 回だけ呼び出されます。もう一方のコントローラーから setNeedsDisplay を呼び出して、drawRect を再度ポップします。

于 2013-04-23T12:46:35.553 に答える
0
-(IBAction)updatefreq:(id)sender
{
   // cro = [[CROO alloc]init];
    CGFloat freq = [(UISlider *)sender value];
    **[cro setFrequency:freq];**
   // [self.view addSubview:cro];
    [cro setNeedsDisplay];
    //[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate date]];
    currfrelab.text = [NSString stringWithFormat:@"Frequency = %f",freq];
    //[self.view performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:YES];

}
于 2013-04-23T12:49:26.733 に答える