在图像上方移动 UIView

Moving UIView above image

我想通过触摸在图像上方移动 UIView。 我在 UIImageView 和 touchesBegan 和 touchesMoved 函数中计算了图像的 CGRect 可以移动视图。 我在这些函数中有一个 for 循环 (touch in touches),在这个循环中有通过触摸移动视图的代码。 我只想移动位于图像本身上方的视图,所以我将 if 放入下一个 for 循环中:

if imageRect.contains(selectedText.frame)

问题是,当我将视图移出 imageRect 时,它卡住了。 我不能把它移回去。 我希望能够将 UIView 移动到图像的 CGRect 中,但不能移出它。

编辑: selectedText 是 UIView,在 touchesBegan 函数中我创建了变量 toucheLocation(我在 touchesMoved 函数中有相同的代码,除了 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() 部分在 class 的开头)

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
            }
        }
    }
}

做这样的事情,

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
}

如果还有什么不明白的再问。

好吧,现在当 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
}

将确保只有在图像内部时才会更新视图框架。