移动UIView高于图像

时间:2018-02-05 10:01:29

标签: ios swift xcode uiview uiimageview

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

if imageRect.contains(selectedText.frame)

问题在于,当我将视图移出imageRect时,它会卡住。 我无法将它移回去。 我希望能够将UIView移动到图像的CGRect中,但不能移出它。

编辑: selectedText是UIView,在touchesBegan func中我创建了变量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
}

EDIT2:

这是代码的更大部分: (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
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

做这样的事,

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
}

如果仍然不清楚,请询问。

答案 1 :(得分:0)

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

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