我以编程方式创建了UIView
,并使用UIImageView
来约束UI放置了PureLayout
初始化。
但是,我在UI上附加了一个滑动器功能,现在可以正常使用,当我决定以编程方式实现UIView
时,滑动器功能不起作用。我不知道为什么,但这就是我能够做到的
protocol SwipeDelegate: class {
func swiped(for view: TripView)
}
class TripView: UIView {
var shouldSetupConstraints = true
var startingFrame: CGRect?
weak var swipeDelegate: SwipeDelegate?
let screenSize = UIScreen.main.bounds
let sliderView: UIView = UIImageView(frame: CGRect.zero)
let sliderImage: UIImageView = UIImageView(frame: CGRect.zero)
override init(frame: CGRect){
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
sliderView.backgroundColor = UIColor.green
sliderView.autoSetDimension(.height, toSize: screenSize.width / 6)
self.addSubview(sliderView)
sliderImage.backgroundColor = UIColor.clear
sliderImage.image = UIImage(named: "icons8-double_right_filled.png")
sliderImage.contentMode = .scaleAspectFit
sliderImage.autoSetDimension(.width, toSize: screenSize.width / 6)
sliderImage.autoSetDimension(.height, toSize: screenSize.width / 6)
self.addSubview(sliderImage)
swipeFunc()
}
override func updateConstraints() {
if(shouldSetupConstraints) {
sliderView.autoPinEdgesToSuperviewSafeArea(with: UIEdgeInsets.zero, excludingEdge: .bottom)
sliderImage.autoPinEdge(toSuperviewEdge: .left)
sliderImage.autoPinEdge(.bottom, to: .bottom, of: sliderView, withOffset: 0.0)
shouldSetupConstraints = false
}
super.updateConstraints()
}
}
extension TripView {
private func swipeFunc() {
let swipeGesture = UIPanGestureRecognizer(target: self, action: #selector(acknowledgeSwiped(sender:)))
sliderImage.addGestureRecognizer(swipeGesture)
sliderImage.isUserInteractionEnabled = true
swipeGesture.delegate = self as? UIGestureRecognizerDelegate
}
@objc func acknowledgeSwiped(sender: UIPanGestureRecognizer) {
if let sliderView = sender.view {
let translation = sender.translation(in: sliderView)
switch sender.state {
case .began:
startingFrame = sliderImage.frame
fallthrough
case .changed:
if let startFrame = startingFrame {
var movex = translation.x
if movex < -startFrame.origin.x { movex = -startFrame.origin.x }
let xMax = sliderView.frame.width - startFrame.origin.x - startFrame.width
if movex > xMax {
movex = xMax
swipeDelegate?.swiped(for: self)
// dismiss(animated: true, completion: nil)
}
var movey = translation.y
if movey < -startFrame.origin.y { movey = -startFrame.origin.y }
let yMax = sliderView.frame.height - startFrame.origin.y - startFrame.height
if movey > yMax {
movey = yMax
}
sliderView.transform = CGAffineTransform(translationX: movex, y: movey)
}
default: // .ended and others:
UIView.animate(withDuration: 0.1, animations: {
sliderView.transform = CGAffineTransform.identity
})
}
}
}
}
任何帮助将不胜感激
答案 0 :(得分:1)
请查看您的实例变量名称,它们会被ConfirmSwiped()中的局部变量替换。 保持变量名的不同。
@objc func acknowledgeSwiped(sender: UIPanGestureRecognizer) {
if let sliderView = sender.view {
let translation = sender.translation(in: self.sliderView) //self.sliderView
switch sender.state {
case .began:
startingFrame = sliderImage.frame
fallthrough
case .changed:
if let startFrame = startingFrame {
var movex = translation.x
if movex < -startFrame.origin.x { movex = -startFrame.origin.x }
let xMax = self.sliderView.frame.width - startFrame.origin.x - startFrame.width //self.sliderView
if movex > xMax {
movex = xMax
swipeDelegate?.swiped(for: self)
// dismiss(animated: true, completion: nil)
}
var movey = translation.y
if movey < -startFrame.origin.y { movey = -startFrame.origin.y }
let yMax = self.sliderView.frame.height - startFrame.origin.y - startFrame.height //self.sliderView
if movey > yMax {
movey = yMax
}
sliderView.transform = CGAffineTransform(translationX: movex, y: movey)
}
default: // .ended and others:
UIView.animate(withDuration: 0.1, animations: {
sliderView.transform = CGAffineTransform.identity
})
}
}
}