简体   繁体   English

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

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

I don't expect anyone to write the code for me. 我不希望有人为我编写代码。 I'm just looking for a push in the right direction toward the framework and/or methods to use. 我只是想朝着正确的方向推动使用的框架和/或方法。 What I want to do is, in iOS Swift, allow the user to select a photo and place it on the screen. 我想做的是,在iOS Swift中,允许用户选择一张照片并将其放在屏幕上。 I've already got that part working. 我已经把那部分工作了。 I also have it working whereby you can drag the image to position it, and pinch to make it larger or smaller. 我也可以使用它,您可以拖动图像将其定位,然后捏捏以使其更大或更小。 However, the "tricky" part is the following... when an edge (or edges) approach the frame of the superview (the screen), I would like the ImageView to "snap" into place at the edge. 但是,“棘手”部分如下...当一条边缘(或多个边缘)接近超级视图(屏幕)的框架时,我希望ImageView在边缘处“捕捉”到位。 Basically, I want the edges of the image to be "magnetically" attracted to the edges of the screen. 基本上,我希望图像的边缘被“磁性”吸引到屏幕的边缘。 I'm not quite sure which combination of methods would be best to accomplish this. 我不确定哪种方法最适合完成此任务。 Any suggestions appreciated. 任何建议表示赞赏。

Edit: Here's what I have so far... 编辑:这是我到目前为止...

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

Sounds to me you want somthing like a hittest. 在我看来,您想要像最热门的东西。

Hittest 命中测试

So i think you'd want to work with drag and drop. 因此,我认为您想使用拖放功能。 As soon the hittest triggers with a marge (5-10 or 15dp/px) you lock the image => stop drag and position the view programmatically... I did not test this, but trhat would be my first approach be. 命中触发(marth)(5-10或15dp / px)时,您立即锁定图像=>停止拖动并以编程方式定位视图...我没有对此进行测试,但是trhat是我的第一种方法。

hittest subview Hittest子视图

Although you don't want to test with subview but with an edge of a subview i think it would be possible. 虽然您不想使用子视图进行测试,但是要使用子视图的边缘进行测试,但我认为这是可能的。

snapping to edge 紧贴边缘

Above link looks like a smooth edge clipping. 链接上方看起来像一个平滑的边缘裁剪。 Reversing the logic, you should be able to clip to bounds... 颠倒逻辑,您应该可以将边界剪裁为...

You could try using a method that checks if the edge(s) of the UIImageView are within a certain distance of the the superview's frame. 您可以尝试使用一种方法来检查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
    }
}

Let me know if that works and/or what you were looking for! 让我知道这是否奏效和/或您正在寻找什么!

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

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