#import "ViewController.h"
@interface ViewController ()
{
UISlider *slider;
UIView *view;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
slider = [[UISlider alloc] initWithFrame:CGRectMake(80, 300, 200, 20)];
[slider addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];
slider.minimumValue = 1;
slider.maximumValue = 100;
slider.minimumValueImage = [UIImage imageNamed:@"test.png"];
slider.maximumValueImage = [UIImage imageNamed:@"test.png"];
slider.continuous = YES;
slider.minimumTrackTintColor = [UIColor grayColor];
slider.maximumTrackTintColor = [UIColor greenColor];
slider.thumbTintColor = [UIColor grayColor];
[self.view addSubview:slider];
view = [[UIView alloc] initWithFrame:CGRectMake(80, 350, 20, 20)];
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];
}
-(void)action:(UISlider *)slider{
view.frame = CGRectMake(slider.value + view.frame.origin.x / 100, 350, 20, 20);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[slider setValue:arc4random()%10 animated:YES];
}
@end
https://www.jianshu.com/p/692c966d37ea