2

How to detect the touch on the edge or border of the view in iOS Swift. I want to increase UIView width and height when the user drags the border of the UIView.

Jasmeet Kaur
  • 1,538
  • 14
  • 16
  • You might want to take a look at the following SO thread - http://stackoverflow.com/questions/8460119/how-to-resize-uiview-by-dragging-from-its-edges It pays to do some searching before you ask a question :) – Fahim Mar 26 '17 at 03:12
  • Possible duplicate of [How to resize UIView by dragging from its edges?](http://stackoverflow.com/questions/8460119/how-to-resize-uiview-by-dragging-from-its-edges) – Fahim Mar 26 '17 at 03:13
  • @ Fahim, I'm guessing it actually isn't a duplicate because the OP is asking how to *detect the touch on the edge* of a UIView, and this question addresses a UIView with "handles". That said, I'd advise the OP to both (1) look at this question and (2) consider how virtually every other CocoaTouch app does it - by using the pinch gesture. Detecting a touch is not very user-friendly... Apple suggests making your buttons 40x40 in size minimally. Can you really expect the same exactness that a mouse pointer yields? You should either use handles or the pinch gesture. –  Mar 26 '17 at 03:49
  • @ Fahim, that question solution is detecting the touch on corners of the UIView where as I want to detect the touch on the edges. – Jasmeet Kaur Mar 26 '17 at 04:53
  • @dfd that's what I want to know, do I need to add 4 different invisible views to detect the touch on the edges, is this the right view or there is any another way? – Jasmeet Kaur Mar 26 '17 at 04:55
  • Consider that Apple, back in 2007, and after a few years of user testing, concluded that the average touch hit needs a 40x40 button. (Or in other words, people have fat fingers.) Now consider what a "normal" edge of a UIView is, 1 point. My question for *you* is - what constitutes an edge? If you create *visible* handles, a user will see them and have the right cues to use them. If you use a pinch gesture, users have had 10 years to know it resizes. I suggest to you that you are not only fighting the known ways to do it, you are also not yet defining what a view's "edge" is. –  Mar 26 '17 at 05:01
  • @dfd if you check facebook iOS app, when we edit crop an image, the rectangle can be resized by touching on edge, so I want to implement the same. – Jasmeet Kaur Mar 26 '17 at 05:10

1 Answers1

1

got the solution Modified How to resize UIView by dragging from its edges? solution.

Swift Version and code to detect edges as well as corners

class OverlayView: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */


    static var kResizeThumbSize:CGFloat = 44.0
    private typealias `Self` = OverlayView

    var imageView = UIImageView()

    var isResizingLeftEdge:Bool = false
    var isResizingRightEdge:Bool = false
    var isResizingTopEdge:Bool = false
    var isResizingBottomEdge:Bool = false

    var isResizingBottomRightCorner:Bool = false
    var isResizingLeftCorner:Bool = false
    var isResizingRightCorner:Bool = false
    var isResizingBottomLeftCorner:Bool = false


        //Define your initialisers here

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let currentPoint = touch.location(in: self)

            isResizingBottomRightCorner = (self.bounds.size.width - currentPoint.x < Self.kResizeThumbSize && self.bounds.size.height - currentPoint.y < Self.kResizeThumbSize);
           isResizingLeftCorner = (currentPoint.x < Self.kResizeThumbSize && currentPoint.y < Self.kResizeThumbSize);
            isResizingRightCorner = (self.bounds.size.width-currentPoint.x < Self.kResizeThumbSize && currentPoint.y < Self.kResizeThumbSize);
            isResizingBottomLeftCorner = (currentPoint.x < Self.kResizeThumbSize && self.bounds.size.height - currentPoint.y < Self.kResizeThumbSize);

            isResizingLeftEdge = (currentPoint.x < Self.kResizeThumbSize)
            isResizingTopEdge = (currentPoint.y < Self.kResizeThumbSize)
            isResizingRightEdge = (self.bounds.size.width - currentPoint.x < Self.kResizeThumbSize)

            isResizingBottomEdge = (self.bounds.size.height - currentPoint.y < Self.kResizeThumbSize)

            // do something with your currentPoint

        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let currentPoint = touch.location(in: self)
            // do something with your currentPoint
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let currentPoint = touch.location(in: self)
            // do something with your currentPoint


            isResizingLeftEdge = false
             isResizingRightEdge = false
             isResizingTopEdge = false
             isResizingBottomEdge = false

             isResizingBottomRightCorner = false
             isResizingLeftCorner = false
             isResizingRightCorner = false
             isResizingBottomLeftCorner = false

        }
    }
Community
  • 1
  • 1
Jasmeet Kaur
  • 1,538
  • 14
  • 16