2013-04-10 90 views
0

我正在嘗試用UISlider更改線條筆畫,但它不起作用。滑塊值沒問題,但沒有任何變化。 滑塊在顯示屏上運行,並在標籤上返回一個值。我認爲這個函數在第一次加載時被繪製,而不是停下來。但我不知道如何解決這個問題。用uislider更改CGContextSetLineWidth

.h文件:

#import <UIKit/UIKit.h> 

@interface Draw : UIView { 
    IBOutlet UISlider *slider; 
    IBOutlet UILabel *label; 
} 

- (IBAction)sliderValueChanged:(id)sender; 

@end 

.m文件:

#import "Draw.h" 

@implementation Draw 


- (IBAction) sliderValueChanged:(UISlider *)sender { 
    label.text = [NSString stringWithFormat:@"%.1f", slider.value]; 
    NSLog(@"slider value = %f", sender.value); 

} 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 




    } 
    return self; 
} 



- (void)drawRect:(CGRect)rect; 
{ 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 0.0, 1.0); 
    CGContextBeginPath(context); 
    CGContextMoveToPoint(context, 50.0, 50.0); 
    CGContextAddLineToPoint(context, 250.0, 100.0); 
    CGContextAddLineToPoint(context, 250.0, 350.0); 
    CGContextAddLineToPoint(context, 50.0, 350.0); 
    CGContextClosePath(context); 
    CGContextSetLineWidth(context, slider.value); 
    CGContextStrokePath(context); 

} 

@end 

回答

1

在你的方法- (IBAction) sliderValueChanged:(UISlider *)sender你應該叫[self setNeedsDisplay]通知認爲,內容需要重繪。

+0

ya,正確的提示,謝謝! – greenchapter 2013-04-10 20:09:20

0

您需要告訴UI需要使用-[UIView setNeedsDisplay]消息重新繪製它。否則,它不知道任何與如何繪製視圖有關的任何東西

- (IBAction) sliderValueChanged:(UISlider *)sender { 
    label.text = [NSString stringWithFormat:@"%.1f", slider.value]; 
    NSLog(@"slider value = %f", sender.value); 
    [self setNeedsDisplay]; 
}