2013-04-10 4 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 파일 : 내용이 다시 그려야 할 필요가 뷰를 알려 [self setNeedsDisplay]를 호출해야 - (IBAction) sliderValueChanged:(UISlider *)sender 당신의 방법에서

#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 

답변

0

UI에 -[UIView setNeedsDisplay] 메시지를 사용하여 다시 그려야한다고 알려줘야합니다. 그렇지 않으면 뷰를 그리는 방법과 관련된 내용이 변경되었음을 알 수 없습니다.

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