我创建了一个UIView
和一个UIImageView
,它位于UIView
内部作为 subview ,然后我向添加了一个平移手势>UIImageView
在 UIView
内滑动,图像现在滑动,但我现在遇到的问题是当 slider 到达 View 末尾时if movex > xMax
,我只想打印一次 print("SWIPPERD movex")
。只要用户不将他/她的手从用于滑动的 UIImageView
上移开,我当前的代码就会继续打印 print("SWIPPERD movex")
private func swipeFunc() {
let swipeGesture = UIPanGestureRecognizer(target: self, action: #selector(acknowledgeSwiped(sender:)))
sliderImage.addGestureRecognizer(swipeGesture)
swipeGesture.delegate = self as? UIGestureRecognizerDelegate
}
@objc func acknowledgeSwiped(sender: UIPanGestureRecognizer) {
if let sliderView = sender.view {
let translation = sender.translation(in: self.baseView) //self.sliderView
switch sender.state {
case .began:
startingFrame = sliderImage.frame
viewCenter = baseView.center
fallthrough
case .changed:
if let startFrame = startingFrame {
var movex = translation.x
if movex < -startFrame.origin.x {
movex = -startFrame.origin.x
print("SWIPPERD minmax")
}
let xMax = self.baseView.frame.width - startFrame.origin.x - startFrame.width - 15 //self.sliderView
if movex > xMax {
movex = xMax
print("SWIPPERD movex")
}
var movey = translation.y
if movey < -startFrame.origin.y { movey = -startFrame.origin.y }
let yMax = self.baseView.frame.height - startFrame.origin.y - startFrame.height //self.sliderView
if movey > yMax {
movey = yMax
// print("SWIPPERD min")
}
sliderView.transform = CGAffineTransform(translationX: movex, y: movey)
}
default: // .ended and others:
UIView.animate(withDuration: 0.1, animations: {
sliderView.transform = CGAffineTransform.identity
})
}
}
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
return sliderImage.frame.contains(point)
}
最佳答案
根据您的要求,您可能希望使用 .ending
状态而不是 .changed
状态。你也提到过你只想得到正确的方向。您可以尝试以下方法来确定滑动是否从右向左,反之亦然,根据您的意愿进行更改:
let velocity = sender.velocity(in: sender.view)
let rightToLeftSwipe = velocity.x < 0
关于ios - 翻译时仅运行一次 swift 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55537842/