I'm trying to use the UISlider
to change specified image size in UIImageView
. The value range of the slider is 0 ~ 100. So the idea is that if I move the slider smaller, a specified image shows smaller, and If I move the slider bigger, the image size getting bigger.
This is the case when the image getting smaller: getting smaller image by UISlider
And this is the case when the image getting bigger: getting bigger image by UISlider
Can someone please help?
You will need to add Width and Aspect Ratio constraints to your image view -and obviously the other desired needed constraints-:
In my example, these are the constraints for the image view:
Next, you could implemnt the following:
class ViewController: UIViewController {
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var imageViewWidth: NSLayoutConstraint!
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
slider.maximumValue = 100.0
slider.value = 0.0
imageViewWidth.constant = 20
}
@IBAction func sliderValueChanged(_ sender: Any) {
imageViewWidth.constant = CGFloat(20 + slider.value)
}
}
the imageViewWidth
IBOutlet will be the width constraint of your image view that you want to resize.
Output:
Make the slider's value range from .01 to 1.0 (.01 should work better than 0.)
Control-drag from your slider into your view controller and create an action on the slider's valueChanged event. Let's call it sliderChanged
.
In the slider's action method, change the scale on the image view:
@IBOutlet myImageView: UIImageView*
@IBAction func sliderChanged(sender: UISlider) {
let scale = sender.value
let transform = CGAffineTransform.scale(scaleX: scale, y: scale)
myImageView.transform = transform
}
That should do it.
Update frames based on slider's value
Try multiplying UISlider's value with UIImageView's constraints
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.