我想通过触摸图像上方来移动UIView。 我计算了图像在UIImageView
和触摸中的CGRect
开始了,touchesMoved
函数可以移动视图。 我在这些函数(touch in touches
)中有一个for
循环,在这个循环中,有通过触摸移动视图的代码。 我只想仅在视图位于图像本身上方时才移动视图,因此我将if
放在下一个 for 循环中:
if imageRect.contains(selectedText.frame)
问题是当我将视图移出 imageRect 时,它卡住了。 我无法将其移回。 我希望能够在图像的CGRect中移动UIView,但不能移出图像。
编辑: selectedText 是 UIView,在 touchesBegin func 中,我使变量toucheLocation
(我在 touchesMoving func 中有相同的代码,除了var toucheLocation = location
部分)
if selectedText.frame.contains(toucheLocation){
selectedText.center.x = selectedText.center.x + (location.x - toucheLocation.x)
selectedText.center.y = selectedText.center.y + (location.y - toucheLocation.y)
toucheLocation = location
}
编辑2:
这是代码的更大部分: (var toucheLocation = CGPoint()
部分在课程开始时)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: imageView)
toucheLocation = location
if imageRect.contains(selectedText.frame){
if selectedText.frame.contains(toucheLocation){
selectedText.center.x = selectedText.center.x + (location.x - toucheLocation.x)
selectedText.center.y = selectedText.center.y + (location.y - toucheLocation.y)
toucheLocation = location
}
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: imageView)
if imageRect.contains(selectedText.frame){
if selectedText.frame.contains(toucheLocation){
selectedText.center.x = selectedText.center.x + (location.x - toucheLocation.x)
selectedText.center.y = selectedText.center.y + (location.y - toucheLocation.y)
toucheLocation = location
}
}
}
}
好吧,现在当view
离开图像时,if
语句将阻止它移动。所以你将不得不改变这一点。我提出以下建议。
因此,尝试使用以下方法,而不是您当前的if
分支(if imageRect.contains(selectedText.frame){
):
if selectedText.frame.contains(toucheLocation) {
let proposedFrame = selectedText.frame
proposedFrame.center.x = proposedFrame.center.x + (location.x - toucheLocation.x)
proposedFrame.center.y = proposedFrame.center.y + (location.y - toucheLocation.y)
if imageRect.contains(proposedFrame) {
selectedText.frame = proposedFrame
}
toucheLocation = location
}
现在它将一直尝试基于触摸计算新帧。但是
if imageRect.contains(proposedFrame) {
selectedText.frame = proposedFrame
}
将确保仅当视图位于图像内部时,才会更新视图框。
做这样的事情,
var imgView : UIImageView // your imageview on which you wanted to move the UIView
var viewMove : UIView // It is the view which move only over the
在联系中开始功能
let minX = imgView.frame.origin.x
let minY = imgView.frame.origin.y
let maxX = imgView.frame.origin.x + imgView.frame.size.width
let maxY = imgView.frame.origin.y + imgView.frame.size.height
let touchPoint : CGPoint // touch point
if (touchPoint.x >= minX) && // check left margin of View
(touchPoint.y >= minY) && // check top margin of View
(touchPoint.x + viewMove.frame.size.width <= maxX) && // check right margin of View
(touchPoint.y + viewMove.frame.size.height <= maxY) { // check bottom margin of View
/*- If any of above condition is false, it means your view is moving out of imageview boundry else it is within imageview boundry.-*/
// give your view new origin to move
}
else{
// do code to restrict you view to move out of image boundry
}
如果仍然不清楚,请询问。