美文网首页
UISlider 定制

UISlider 定制

作者: Summer_YJL | 来源:发表于2017-09-01 11:02 被阅读0次
    1. .h文件

      #import <UIKit/UIKit.h>
      /**滑杆*/
      @interface YJL_Slider : UISlider
      @property(nonatomic , assign) CGFloat sliderHight;  //滑条的高度
      //初始化方法
      -(instancetype)initWithFrame:(CGRect)frame andSliderHight:(CGFloat)sliderHight;
      @end
      
    2. .m文件

      #import "YJL_Slider.h"
      #define thumbBound_x 10
      #define thumbBound_y 20
      
      @implementation YJL_Slider
      {
            CGRect lastBounds;
      }
      -(instancetype)initWithFrame:(CGRect)frame andSliderHight:(CGFloat)sliderHight{
             if (self = [super initWithFrame:frame]) {
                     _sliderHight = sliderHight;
             }
             return self;
      }
      -(instancetype)init{
             if (self = [super init]) {
                //
             }
              return self;
       }
      
      
       -(CGRect)trackRectForBounds:(CGRect)bounds{
                 if (_sliderHight == 0.00) {
                         _sliderHight = 4;
                  }
                   return CGRectMake(0, 0, self.frame.size.width, _sliderHight);
         }
      
      
        //- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value{
        //        //y轴方向改变手势范围
        //        rect.origin.y = rect.origin.y - 10;
        //        rect.size.height = rect.size.height + 20;
        //        //
        //        rect.origin.x = rect.origin.x - 10;
        //        rect.size.width = rect.size.width + 20;
        //        return CGRectInset ([super thumbRectForBounds:bounds trackRect:rect value:value], 10 ,10);
        //}
      
        - (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value{
                rect.origin.x = rect.origin.x;
                rect.size.width = rect.size.width ;
                CGRect result = [super thumbRectForBounds:bounds trackRect:rect value:value];
                 lastBounds = result;
                 return result;
       }
      
       //滑杆上的点击事件
      - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
             UIView *result = [super hitTest:point withEvent:event];
             if ((point.y >= -thumbBound_y) && (point.y < lastBounds.size.height + thumbBound_y)) {
                 float value = 0.0;
                value = point.x - self.bounds.origin.x;
                value = value/self.bounds.size.width;
                value = value < 0? 0 : value;
                value = value > 1? 1: value;
                value = value * (self.maximumValue - self.minimumValue) + self.minimumValue;
                [self setValue:value animated:YES];
            }
            return result;
      

      }

      //扩大触摸点的响应范围
      - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
           BOOL result = [super pointInside:point withEvent:event];
          if (!result && point.y > -10) {
                 if ((point.x >= lastBounds.origin.x - thumbBound_x) && (point.x <= (lastBounds.origin.x + lastBounds.size.width + thumbBound_x)) && (point.y < (lastBounds.size.height + thumbBound_y))) {
                 result = YES;
                 }
          }
           return result;
      }
      @end
      

    相关文章

      网友评论

          本文标题:UISlider 定制

          本文链接:https://www.haomeiwen.com/subject/xjmhjxtx.html