我的问题是,我有一个自定义的UIView,其代表一个弹出。 里面这个观点我想有一个滑块,它具有获取每次我周围拖动滑块时更新标签。 滑块逻辑的作品,至少在我使用它在一个视图控制器直接,但是用我的自定义视图。 具体而言,在addTarget规定的方法不会被调用的。 我也尝试了滑盖的backgroundColor设置的东西,它实际上出现了,这意味着它有一个框架。 所以,我在这里失踪? 我有一个类似的问题在自定义的UIView子类很多以UIButton的等次。
class PriceVoteController: UIView {
var parentView: UIView = UIView()
let slider: UISlider = {
let slider = UISlider()
slider.isContinuous = true
slider.minimumValue = 1
slider.maximumValue = 3
slider.setMinimumTrackImage(#imageLiteral(resourceName: "minimumSliderTrackImage").resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: 9, bottom: 0, right: 9)), for: .normal)
slider.setMaximumTrackImage(#imageLiteral(resourceName: "maximumSliderTrackImage").resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: 9, bottom: 0, right: 9)), for: .normal)
let sliderThumb = #imageLiteral(resourceName: "sliderThumImage")
slider.setThumbImage(sliderThumb, for: .normal)
slider.addTarget(self, action: #selector(adjustPriceLabel), for: .valueChanged)
slider.setValue(2.0, animated: true)
return slider
}()
let priceLabel: UILabel = {
let label = UILabel()
label.textAlignment = .center
return label
}()
let blackView: UIView = {
let view = UIView()
view.backgroundColor = .black
view.alpha = 0.2
return view
}()
let popUpView: UIView = {
let view = UIView()
view.layer.backgroundColor = UIColor.white.cgColor
view.layer.cornerRadius = 10
return view
}()
@objc fileprivate func adjustPriceLabel() {
print("adjust label")
priceLabel.text = String(format: "%.1f", slider.value)
priceLabelLeftConstraint.constant = getXPositionForSliderValue()
}
fileprivate func getXPositionForSliderValue() -> CGFloat {
guard let currentThumbImage = slider.currentThumbImage else { return 0 }
let sliderRange = slider.frame.size.width - currentThumbImage.size.width;
let sliderOrigin = slider.frame.origin.x + (currentThumbImage.size.width / 2.0);
let sliderValueToPixels = ((CGFloat(slider.value - slider.minimumValue)/CGFloat(slider.maximumValue - slider.minimumValue)) * sliderRange) + sliderOrigin;
return sliderValueToPixels;
}
var priceLabelLeftConstraint: NSLayoutConstraint = NSLayoutConstraint()
override init(frame: CGRect) {
super.init(frame: frame)
self.isUserInteractionEnabled = true
}
fileprivate func setupSlider() {
self.popUpView.addSubview(slider)
slider.anchor(top: self.popUpView.topAnchor, left: self.popUpView.leftAnchor, bottom: nil, right: self.popUpView.rightAnchor, paddingTop: 10, paddingLeft: 50, paddingBottom: 0, paddingRight: 50, width: 0, height: 0)
self.popUpView.addSubview(priceLabel)
priceLabel.text = String(slider.value)
priceLabel.anchor(top: slider.bottomAnchor, left: nil, bottom: nil, right: nil, paddingTop: 10, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
priceLabelLeftConstraint = priceLabel.centerXAnchor.constraint(equalTo: self.popUpView.leftAnchor, constant: getXPositionForSliderValue())
priceLabelLeftConstraint.isActive = true
}
func showOn(view: UIView) {
parentView = view
parentView.addSubview(self.popUpView)
self.popUpView.anchor(top: nil, left: nil, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: view.frame.width - 100, height: 200)
self.popUpView.centerXAnchor.constraint(equalTo: parentView.centerXAnchor).isActive = true
self.popUpView.centerYAnchor.constraint(equalTo: parentView.centerYAnchor).isActive = true
setupSlider()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我真的希望有人能帮助我,因为我一直试图弄清楚这一点,在过去的时间......和无解:/正如我所说的,当一切是一个视图控制器和视图里面的方法被调用,但与自定义的UIView。
顺便说一句,在parentView属性是的viewController,在自定义的UIView被初始化的主视图。 我用空的构造函数初始化,然后调用show()与视图控制器的看法。
谢谢你的帮助! - 罗伯特·