如何解决Swift 检测 UISlider 图片点击
所以我有一个 UiSlider
看起来像这样:
Image here
它是这样设置的,所以图像不是UIImageView
的,而是UiSlider
中的图像:
Image 2 here
我想添加一个功能,当用户按下左侧的图像时,它运行一个功能,当用户按下右侧的图像时,它运行另一个功能。这甚至可能吗?
解决方法
您可以这样做。请仔细阅读评论。
import UIKit
class ViewController: UIViewController {
// This is linked to your storyboard layout
@IBOutlet weak var slider: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
// Add a tap gesture recognizer to the slider
slider.addGestureRecognizer(UITapGestureRecognizer(target: self,action: #selector(sliderTapped(_:))))
}
@objc private func sliderTapped(_ tapRecognizer: UITapGestureRecognizer) {
let location = tapRecognizer.location(in: slider)
// UISlider can tell you min/max value image frames
// CAUTION: Use images those are big enough to be easy tap targets
// In my case,I used images of size 30x30
let minImageRect = slider.minimumValueImageRect(forBounds: slider.bounds)
let maxImageRect = slider.maximumValueImageRect(forBounds: slider.bounds)
// Now you can check which image out of min/max was tapped
if minImageRect.contains(location) {
print("Min Image Tapped")
} else if maxImageRect.contains(location) {
print("Max Image Tapped")
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。