学习笔记----UISlider

关于UISlider的简介就不多说了主要记录一下它的各种属性用法:

-(void)setProgressView{
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(20, 200, 300, 10)];
    backView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:backView];
    self.backView = backView;
    
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 300, 10)];
    UIImage *image5 = [UIImage imageNamed:@"player_slider_playback_thumb"];
    //设置slider圆形滚动的颜色
    slider.thumbTintColor = [UIColor purpleColor];
    //设置slider右边的颜色
    slider.maximumTrackTintColor = [UIColor redColor];
    //设置slider左边的颜色
    slider.minimumTrackTintColor = [UIColor orangeColor];
//    设置slider圆形滚动部分的背景图片
    [slider setThumbImage:[self OriginImage:image5 scaleToSize:CGSizeMake(20, 20)] forState:UIControlStateNormal];
    //设置slider右边部分的背景图片
    [slider setMaximumTrackImage:[UIImage imageNamed:@"63acf19b71ca34fa00d16a1761ee62e4"] forState:UIControlStateNormal];
    //设置slider左边部分的背景图片
    [slider setMinimumTrackImage:[UIImage imageNamed:@"6b81ebdb865c1cb4122a6237d6964865"] forState:UIControlStateNormal];
    
    [slider addTarget:self action:@selector(slickChange:) forControlEvents:UIControlEventValueChanged];
    //默认为yes,设置为NO的时候,滑动结束的时候slickChange方法才会被调用。
    slider.continuous = NO;
    [backView addSubview:slider];
    //添加手势,用于点击slider的时候滑动到指定位置
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [slider addGestureRecognizer:tap];
    self.slider = slider;
    
    
    NSLog(@"%f",slider.currentThumbImage.size.width);
    UIImageView *button  = [[UIImageView alloc] initWithFrame:CGRectMake(60, 0, 10, 10)];
    button.backgroundColor = [UIColor clearColor];
    button.image = [UIImage imageNamed:@"player_slider_playback_thumb"];
    [self.backView addSubview:button];
    self.button = button;
    
    UIView *lineview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 0.5)];
    lineview.backgroundColor = [UIColor purpleColor];
    lineview.center = backView.center;
    [backView addSubview:lineview];
    
    UIButton *playButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 300, 100, 40)];
    playButton.backgroundColor = [UIColor redColor];
    [playButton addTarget:self action:@selector(playClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:playButton];
}

手势方法:


-(void)tap:(UIGestureRecognizer*)tap{
    //当前手势点击点在slider上的位置
    CGPoint currentPoint = [tap locationInView:self.slider];
    //计算出需要滑动的比例值
    float value = currentPoint.x/self.backView.bounds.size.width;
    self.slider.value = value;
}

滑动事件响应方法:

-(void)slickChange:(UISlider*)slider{
    //计算出指定路径上的button在slider的范围
    CGFloat min = 42/300.0;
    CGFloat max = 75/300.0;
    //slider滑动到这个区间时将slider设置到最前面
    if (min

改变slider上圆形滑动块大小的一种办法(UITableviewcell上自带imageview大小也可以使用这个代码)

-(UIImage*) OriginImage:(UIImage*)image scaleToSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);//size为CGSize类型,即你所需要的图片尺寸
    [image drawInRect:CGRectMake(0,0, size.width, size.height)];
    UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

滑动到第一个小的分区上的图片:


屏幕快照 2017-05-31 上午9.50.06.png

滑过以后的图片:


屏幕快照 2017-05-31 上午9.49.51.png

你可能感兴趣的:(学习笔记----UISlider)