我的問題是,我有一個自定義的UIView,它代表一個彈出窗口。在這個視圖裏面我想要一個滑塊,它有一個標籤,每次我拖動滑塊時都會更新。至少當我在ViewController中直接使用滑塊邏輯時,滑塊邏輯起作用,但與我的自定義視圖無關。具體來說,在addTarget中指定的方法根本不會被調用。我也嘗試將滑塊的backgroundColor設置爲某種東西,並且它實際上顯示出來了,這意味着它有一個框架。那麼,我在這裏錯過了什麼?我在自定義UIView子類中使用過UIButton等很多次類似的問題。自定義UIView:UIButton addTarget未調用
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")
}
}
我真的希望有人能幫助我,因爲我一直試圖弄清楚這一點,在過去的時間......和無解:/正如我所說的,該方法被調用時,一切都在ViewController及其視圖中,但不與自定義UIView。
順便說一下,parentView屬性是viewController的主視圖,其中自定義UIView被初始化。我用空的構造函數初始化它,然後用ViewController的視圖調用show()。
感謝您的幫助! - 羅伯特
我剛纔看到我從來沒有將popupView添加到自定義UIView中......所以我做了這個,現在滑塊根本不會移動,我可以在自定義視圖「後面」觸發觸摸事件...所以事件從它背後的視圖控制器...有人可以幫忙嗎? –