Skip to content

Instantly share code, notes, and snippets.

@acalism
Last active June 16, 2017 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acalism/f7de69e13e9e6ae0fe8c6a0726b9318d to your computer and use it in GitHub Desktop.
Save acalism/f7de69e13e9e6ae0fe8c6a0726b9318d to your computer and use it in GitHub Desktop.
如何调整UISlider的thumbImage的size?
class ProgressSlider: UISlider {
override func thumbRect(forBounds bounds: CGRect, trackRect rect: CGRect, value: Float) -> CGRect {
let max = maximumValue == 0 ? 1.0 : maximumValue
let side: CGFloat = 8
let centerX: CGFloat = rect.width * CGFloat(value / max)
let r = CGRect(x: ceil(centerX) - side / 2, y: rect.midY - side / 2, width: side, height: side)
return r
}
override func layoutSubviews() {
super.layoutSubviews()
// SDK里的原图像素 57 * 44,中间圆圈 28 * 28
if let innerThumbView = self.value(forKey: "innerThumbView") as? UIImageView,
let thumbViewNeue = self.value(forKey: "thumbViewNeue") as? UIImageView,
let sv = innerThumbView.superview as? UIImageView, sv == thumbViewNeue {
innerThumbView.contentMode = .scaleAspectFit
if let image = innerThumbView.image {
innerThumbView.frame.size = CGSize(width: image.size.width * 8 / 14, height: image.size.height * 8 / 14)
innerThumbView.center = CGPoint(x: sv.bounds.width / 2, y: sv.bounds.height / 2)
}
}
// 这个方法仍不成功,原因是,image是个tiff图,而且是所谓.9图,所以scale这种事就变成拼尸块了,而不是按意愿缩小。
// 最终的解决办法,还得是提供thumbImage,图片大小就是滑块大小。
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment