一个渐变的Slider

一个渐变的Slider

参考系统的API,修改为你自己的样式

  • 来看看效果
Untitled.gif

Demo地址觉得OK的, 记得

后面发现不太好滑动, 修改一下
在slider的类里面
写上

- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value{
    rect.origin.x = -10;
    rect.origin.y = rect.origin.y - 10;
    rect.size.height = bounds.size.height/2 + 20;
    rect.size.width = bounds.size.width + 20;
    self.lastBounds = rect;
    return [super thumbRectForBounds:bounds trackRect:rect value:value];
}

//检查点击事件点击范围是否能够交给self处理
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
//调用父类方法,找到能够处理event的view
    UIView* result = [super hitTest:point withEvent:event];
    if (result != self) {
        /*如果这个view不是self,我们给slider扩充一下响应范围,
          这里的扩充范围数据就可以自己设置了
        */
        if ((point.y >= -15) &&
            (point.y < (self.lastBounds.size.height + 20)) &&
            (point.x >= 0 && point.x < CGRectGetWidth(self.bounds))) {
            //如果在扩充的范围类,就将event的处理权交给self
            result = self;
        }
    }
    //否则,返回能够处理的view
    return result;
}
//检查是点击事件的点是否在slider范围内
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    //调用父类判断
    BOOL result = [super pointInside:point withEvent:event];
    
    if (!result) {
        //同理,如果不在slider范围类,扩充响应范围
        if ((point.x >= (self.lastBounds.origin.x - 20)) && (point.x <= (self.lastBounds.origin.x + self.lastBounds.size.width + 20))
            && (point.y >= -20) && (point.y < (self.lastBounds.size.height + 20))) {
            //在扩充范围内,返回yes
            result = YES;
        }
    }
    
    //否则返回父类的结果
    return result;
}

你可能感兴趣的:(一个渐变的Slider)