菜鸟教程小白 发表于 2022-12-11 18:19:58

ios - UISlider 缩略图位置偏移


                                            <p><p>我正在尝试使用自定义图像实现标准 UISlider。</p>

<p>目前看起来是这样</p>

<p> <a href="/image/bLQ9o.png" rel="noreferrer noopener nofollow"><img src="/image/bLQ9o.png" alt="enter image description here"/></a> </p>

<p>但我需要它看起来像这样</p>

<p> <a href="/image/lV0xZ.png" rel="noreferrer noopener nofollow"><img src="/image/lV0xZ.png" alt="enter image description here"/></a> </p>

<p>如何将缩略图向上偏移,以便只有尖端几乎不接触线?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果您想更改拇指图像的偏移量,您必须在自定义 <code>UISlider</code> 子类中覆盖 <code>thumbRectForBounds:trackRect:value:</code>。</p>

<pre><code>class CustomSlider: UISlider {
    override func thumbRect(forBounds bounds: CGRect, trackRect rect: CGRect, value: Float) -&gt; CGRect {
      let originalRect = super.thumbRect(forBounds: bounds, trackRect: rect, value: value)
      let yOffset: CGFloat = 16 // any value you want
      let newY = originalRect.origin.y - yOffset

      return CGRect(x: originalRect.origin.x, y: newY, width: originalRect.width, height: originalRect.height)
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UISlider 缩略图位置偏移,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45221860/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45221860/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UISlider 缩略图位置偏移