拖动 Swift UIView 时出现故障
我在 iOS 项目中有一个视图,需要在主视图中拖动。景色。我已将平移手势附加到该视图(paletteView),并且移动它效果很好。
但是,行动开始时就会出现“小故障”。这不会发生在第一步,而是发生在后续的动作中。我认为发生的事情与paletteView 上的约束有关。它在主视图中的起始位置为 100,100,但当然,每当拖动结束时,除非我更改它,否则它会弹回到该位置。因此,每当平移开始时,我都会获取调色板的位置,并更新约束。这也有效,但当下一个手势开始时,它似乎会产生闪光。这是主要代码:
@objc func palettePan(_ sender: UIPanGestureRecognizer) {
let translation = sender.translation(in: view)
switch sender.state {
case .began:
paletteLeading?.constant = paletteView!.frame.origin.x
paletteTop?.constant = paletteView!.frame.origin.y
paletteView?.setNeedsUpdateConstraints()
case .changed:
paletteView!.transform = CGAffineTransform(translationX: translation.x, y: translation.y)
case .ended:
print("done \(paletteView!.frame)")
default:
_ = true
}
}
同样,这工作正常,但对于调色板的短暂“闪光”,位置不正确。我不知道这会从哪里来。
I have a view in an iOS project that is meant to be dragged within the main view. The view. I've attached a pan gesture to that view (paletteView), and moving it around works fine.
But, the "glitches" when moves begin. This doesn't happen on the first move, but on subsequent ones. I think what is happening is related to the constraints on the paletteView. It starts located at 100,100 in the main view, but of course whenever a drag ends, unless I change that, it will snap back to that location. So whenever the pan begins, I grab location of the palette, and update the constraints. This also works, but it seems to produce a flash when the next pangesture begins. Here's the main code:
@objc func palettePan(_ sender: UIPanGestureRecognizer) {
let translation = sender.translation(in: view)
switch sender.state {
case .began:
paletteLeading?.constant = paletteView!.frame.origin.x
paletteTop?.constant = paletteView!.frame.origin.y
paletteView?.setNeedsUpdateConstraints()
case .changed:
paletteView!.transform = CGAffineTransform(translationX: translation.x, y: translation.y)
case .ended:
print("done \(paletteView!.frame)")
default:
_ = true
}
}
Again, this works fine, but for the brief "flash" of the palette, out of position. I don't know where this would be coming from.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在更新约束的同时重置
transform
,此时您正在将约束更新到新位置,但视图仍然是根据先前的最终平移进行变换的平移,直到第一个changed
平移手势事件发生。因此,您的
palettePan
函数将变为:尽管我实际上会在平移手势结束时更新约束并进行转换,而不是在新手势开始时:
You need to reset the
transform
at the same time as you update the constraints, at the moment you are updating the constraints to to the new position, but the view is still transformed by the final translation of the previous pan until the firstchanged
pan gesture event comes through.So your
palettePan
function would become:Though I would actually update the constraints and transform when the pan gesture ends, rather than when a new one begins: