如何让UIImageView的边缘捕捉到屏幕边缘(超视图)?

时间:2016-03-04 13:16:35

标签: ios swift uiimageview frame

我不希望任何人为我编写代码。我正在寻找朝着框架和/或方法使用的正确方向。我想要做的是,在iOS Swift中,允许用户选择一张照片并将其放在屏幕上。我已经让那部分工作了。我也有它的工作,你可以拖动图像来定位它,并捏,使其变大或变小。然而,"棘手"部分是以下...当一个边缘(或边缘)接近超视图(屏幕)的框架时,我希望ImageView能够"捕捉"在边缘到位。基本上,我希望图像的边缘是"磁性"吸引到屏幕的边缘。我不太确定哪种方法组合最能实现这一目标。任何建议表示赞赏。

编辑: 这就是我到目前为止所拥有的......

class AdjustableImageView: UIImageView {

    var parent:ViewController!

    // last location for view
    var lastSavedLocation = CGPointZero

    override init(frame: CGRect) {
        super.init(frame: frame)

        // add pan gesture to view
        let panGesture = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
        let longPressGesture = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")
        let pinchGesture = UIPinchGestureRecognizer(target: self, action: "pinchRecognized:")
        self.addGestureRecognizer(panGesture)
        self.addGestureRecognizer(longPressGesture)
        self.addGestureRecognizer(pinchGesture)
        self.userInteractionEnabled = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func pinchRecognized(pinch: UIPinchGestureRecognizer) {
        // change view scale based on pinch
        self.transform = CGAffineTransformScale(self.transform, pinch.scale, pinch.scale)
        pinch.scale = 1.0
    }

    func handlePanGesture(gesture: UIPanGestureRecognizer) {
        // find translation in main view
        let newTranslation = gesture.translationInView(self.superview)

        // set current object to new position
        self.center = CGPointMake(self.lastSavedLocation.x + newTranslation.x , self.lastSavedLocation.y + newTranslation.y)
    }

    // detect touch for the current view and change last save postion
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        // move touched view to front.
        self.superview?.bringSubviewToFront(self)

        self.layer.borderColor = UIColor.greenColor().CGColor
        self.layer.borderWidth = 2

        // save view location
        self.lastSavedLocation = self.center

        parent.imageSelected(self)
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.layer.borderWidth = 0
    }
}

2 个答案:

答案 0 :(得分:0)

听起来我想要一个像最热门的东西。

Hittest

所以我认为你想要拖放工作。一旦hittest触发marge(5-10或15dp / px)你就锁定了图像=&gt;停止拖动并以编程方式定位视图...我没有测试这个,但是这将是我的第一个方法。

hittest subview

虽然您不想使用子视图进行测试,但是我认为可以使用子视图边缘进行测试。

snapping to edge

上面的链接看起来像一个平滑的边缘裁剪。颠倒逻辑,你应该能够剪辑到界限......

答案 1 :(得分:0)

您可以尝试使用一种方法来检查UIImageView的边缘是否在superview框架的某个距离内。

func checkEdge(view: UIImageView) {

    let left   = view.frame.minX
    let right  = view.frame.maxX
    let bottom = view.frame.maxY
    let top    = view.frame.minY

    if left <= 10 {
        // Move view to left side
    } else if right <= 10 {
        // Move view to right side
    } else if bottom <= 10 {
        // Move view to bottom
    } else if top <= 10 {
        // Move view to top
    } else {
        // Do nothing
    }
}

让我知道这是否有效和/或您正在寻找什么!