2013-04-23 74 views
0

我通過使用drawrectUIView上繪製正弦波,並鏈接到UIViewController。 我第一次加載視圖控制器時可以畫一次。 我希望當我在ViewController中更改UISlider的值時,它會改變正弦波的頻率。 我在IBAction中使用setNeedsDisplay函數,但它不起作用。 這裏是我的代碼setNeedsDisplay尚未被稱爲

//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 
+0

PS它可以使用初始滑塊值的數據來產生波,因此CROO類可以獲得該值。 – IvyBB 2013-04-23 16:02:04

回答

0

初始化只調用一次上井時,init。從另一個控制器調用setNeedsDisplay再次彈出drawRect。

+0

那我應該改變什麼? – IvyBB 2013-04-23 15:57:15

+0

我添加了一個函數來測試setNeedsDisplay,但它仍然沒有工作。 (IBAction)測試:(id)寄件人 { [cro setFrequency:10]; [cro setNeedsDisplay]; } 我點擊一個按鈕並調用此函數 – IvyBB 2013-04-23 16:07:44

+0

您是否試圖從同一個視圖調用setNeedsDisplay?你需要從viewController中調用它。像[otherView setNeedsDisplay]; – 2013-04-23 16:42:36

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]; 

} 
+0

當我添加設置功能時,它仍然不工作。 – IvyBB 2013-04-23 16:09:09

+0

是頻率在正確的內部值正確的內部正確的...因爲只有這樣你纔會看到正弦波變化... – BhushanVU 2013-04-24 05:07:01

+0

我已經檢查了值的頻率和fffff在UIView.But類中更改,但它不能進入​​正確的...我能做什麼? – IvyBB 2013-04-24 05:25:53