更改UISlider thumbImage的大小 [英] Change the size of a UISlider thumbImage
问题描述
如何更改我用作thumbImage的UIImage的大小。我用来设置图像的代码如下。
How can I change the size of a UIImage that I am using as a thumbImage. The code I use to set the image is as follows.
let image: UIImage = UIImage(assetIdentifier: UIImage.AssetIdentifier.Slider)
我使用Enum来获取图像,没问题。
I use an Enum to get the image, no problem there.
我用来设置thumbImage的代码如下。
The code I use to set the thumbImage is as follows.
self.setThumbImage(image, forState: UIControlState.Normal)
我尝试使用下面的代码更改拇指图像大小但不能找到解决方案。
I have tried using the code below to change the thumb images size but cannot find the solution.
let trackRect: CGRect = CGRect(x: 0.0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
let thumbRect: CGRect = CGRect(x: 0.0, y: 0.0, width: 12.0, height: 12.0)
thumbRectForBounds(thumbRect, trackRect: trackRect, value: 0.0)
图像目前大约是40pt到40pt(它是一个圆圈)。我希望图像大约在24pt到24pt左右。
The image is currently about 40pt by 40pt (Its a circle). I would like the image to be around 24pt by 24pt.
此外,这是在XCode 7上的Swift 2.我理解这些方法适用于iOS8.0或更高版本这是我的部署目标。
Also, this is in Swift 2 on XCode 7. I understand those methods are for iOS8.0 or above and that is my deployment target.
推荐答案
拇指将是您提供的图像的大小。
要获得一个24pt乘24pt的拇指,你必须提供48px乘48px(和72x72 @ 3)图像。这也会影响滑块框架。
The thumb will be the size of the image you provide. To get a 24pt by 24pt thumb, you must provide 48px by 48px (and 72x72 for @3) image. This also affects the slider frame.
如果需要,您可以定义一个扩展来动态调整图像大小:
You can define an extension to resize images on the fly if needed:
extension UIImage
{
func imageWithImage(image: UIImage, scaledToSize newSize: CGSize) -> UIImage
{
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
image.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage;
}
}
这样你甚至可以做一些像拇指增大和缩小的滑块,因为它接近最大值和最小值。
That way you can even do cool stuff like have a slider where the thumb grows and shrinks, as it approaches max and min values.
这篇关于更改UISlider thumbImage的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!