Ios 使用uislider更改CGContextSetLineWidth
我正在尝试使用Ios 使用uislider更改CGContextSetLineWidth,ios,objective-c,uislider,Ios,Objective C,Uislider,我正在尝试使用UISlider更改线条笔划,但它不起作用。滑块值正常,但未更改任何内容。 滑块在显示屏上运行,并在标签上返回值。我认为函数是在第一次加载时绘制的,然后停止。但我不知道如何解决这个问题 我的.h文件: #import <UIKit/UIKit.h> @interface Draw : UIView { IBOutlet UISlider *slider; IBOutlet UILabel *label; } - (IBAction)sliderVal
UISlider
更改线条笔划,但它不起作用。滑块值正常,但未更改任何内容。
滑块在显示屏上运行,并在标签上返回值。我认为函数是在第一次加载时绘制的,然后停止。但我不知道如何解决这个问题
我的.h
文件:
#import <UIKit/UIKit.h>
@interface Draw : UIView {
IBOutlet UISlider *slider;
IBOutlet UILabel *label;
}
- (IBAction)sliderValueChanged:(id)sender;
@end
#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
在您的方法
-(iAction)sliderValueChanged:(UISlider*)sender
中,您应该调用[self-setNeedsDisplay]
通知视图需要重画内容。您需要告诉UI需要使用-[UIView-setNeedsDisplay]
消息重画它。否则,它不知道与视图的绘制方式有关的任何内容都已更改
- (IBAction) sliderValueChanged:(UISlider *)sender {
label.text = [NSString stringWithFormat:@"%.1f", slider.value];
NSLog(@"slider value = %f", sender.value);
[self setNeedsDisplay];
}