UISlider显示进度(并且实现图片缩放)

时间:2023-03-09 19:43:24
UISlider显示进度(并且实现图片缩放)

图片展示效果如下:

UISlider显示进度(并且实现图片缩放)UISlider显示进度(并且实现图片缩放)

其他没什么好说的,直接上代码:

RootView.h:

 #import <UIKit/UIKit.h>

 @interface RootView : UIView
@property (nonatomic, strong) UISlider *slider;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIImageView *imageView; @end

RootView.m:

 #import "RootView.h"

 @implementation RootView

 - (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// 添加子视图
[self add];
}
return self;
} // 添加子视图
- (void)add { // 添加slider
self.slider = [[UISlider alloc] initWithFrame:CGRectMake(, , self.frame.size.width - * , )];
self.slider.minimumValue = 0.0;
self.slider.maximumValue = 100.0;
[self addSubview:self.slider]; // 添加label
self.label = [[UILabel alloc] init];
self.label.textAlignment = NSTextAlignmentCenter;
self.label.layer.cornerRadius = ;
self.label.layer.borderWidth = ;
self.label.layer.borderColor = [UIColor greenColor].CGColor;
self.label.font = [UIFont fontWithName:@"Gill Sans" size:];
[self addSubview:self.label]; // 添加图片
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
self.imageView.image = [UIImage imageNamed:@"u=347478478,461363313&fm=11&gp=0.jpg"];
[self addSubview:self.imageView]; } @end

RootViewController.m:

 #import "RootViewController.h"
#import "RootView.h" @interface RootViewController ()
@property (nonatomic, strong) RootView *rootView; @end @implementation RootViewController - (void)loadView {
self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.rootView;
} - (void)viewDidLoad {
[super viewDidLoad]; // 为slider添加一个滑动事件(注意添加的事件是改变数据的事件UIControlEventValueChanged)
[self.rootView.slider addTarget:self action:@selector(slding:) forControlEvents:UIControlEventValueChanged];
} #pragma mark - 实现滑动事件
- (void)slding:(UISlider *)sender { // 1.实现滑动显示进度(百分比)
// 创建一个图片控制器,并且获取slider控件的某一个子控件(就是进度条的滑块,位置是2)
UIImageView *imageView = [self.rootView.slider.subviews objectAtIndex:];
// 获取滑块在父视图上的位置
CGRect nowRect = [self.rootView convertRect:imageView.frame fromView:imageView.superview];
// 根据滑块的位置来设置label的位置
self.rootView.label.frame = CGRectMake(nowRect.origin.x - , nowRect.origin.y - , nowRect.size.width, nowRect.size.height);
// 定义一个NSInteger类型的变量来接收滑块当前滑动到的位置
NSInteger x = self.rootView.slider.value;
// 在label上显示滑块的位置
self.rootView.label.text = [NSString stringWithFormat:@"%ld%%",x]; // 2.实现图片缩放
// 创建一个图片控制器,并且获取slider控件的某一个子控件(就是进度条的滑块,位置是2)
UIImageView *imageView1 = [self.rootView.slider.subviews objectAtIndex:];
// 获取滑块在父视图上的位置
CGRect imageRect = [self.rootView convertRect:imageView1.frame fromView:imageView.superview];
// 重新设置图片的frame
self.rootView.imageView.frame = CGRectMake( - (imageRect.origin.x - ) / , - (imageRect.origin.x - ) / 2, + imageRect.origin.x - , + imageRect.origin.x - );
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end