繁体   English   中英

如何获取UIImageView的边缘以捕捉到屏幕的边缘(超级视图)?

[英]How can I get the edges of a UIImageView to snap to the edges of the screen (superview)?

我不希望有人为我编写代码。 我只是想朝着正确的方向推动使用的框架和/或方法。 我想做的是,在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
    }
}

在我看来,您想要像最热门的东西。

命中测试

因此,我认为您想使用拖放功能。 命中触发(marth)(5-10或15dp / px)时,您立即锁定图像=>停止拖动并以编程方式定位视图...我没有对此进行测试,但是trhat是我的第一种方法。

Hittest子视图

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

紧贴边缘

链接上方看起来像一个平滑的边缘裁剪。 颠倒逻辑,您应该可以将边界剪裁为...

您可以尝试使用一种方法来检查UIImageView的边缘是否在超级视图的框架的一定距离内。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM