我正在尝试根据 UISlider 的值移动在 UIView 中绘制的点。下面的代码适用于在 UIViewController 上具有自定义类 (WindowView) 的 UIView(子视图?)。
窗口视图.h
#import <UIKit/UIKit.h>
@interface WindowView : UIView
- (IBAction)sliderValue:(UISlider *)sender;
@property (weak, nonatomic) IBOutlet UILabel *windowLabel;
@end
窗口视图.m
#import "WindowView.h"
@interface WindowView ()
{
float myVal; // I thought my solution was using an iVar but I think I am wrong
}
@end
@implementation WindowView
@synthesize windowLabel;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)sliderValue:(UISlider *)sender
{
myVal = sender.value;
windowLabel.text = [NSString stringWithFormat:@"%f", myVal];
}
- (void)drawRect:(CGRect)rect
{
// I need to get the current value of the slider in drawRect: and update the position of the circle as the slider moves
UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(myVal, myVal, 10, 10)];
[circle fill];
}
@end