How to resize UIView by dragging from its edges?

后端 未结 6 1135
-上瘾入骨i
-上瘾入骨i 2020-11-28 21:29

In my iPad app, I want the users to be able to resize a UIView by dragging the view from its edges. I\'ll be using iOS 5 SDK, so what\'s the cleanest approach t

6条回答
  •  情深已故
    2020-11-28 21:49

    You can do this by checking the touch-start point. If it hits one of your four corners you can resize based on the distance between that touch-start point and the current-touch point. (If the touch-start point didn't hit a corner, we just move the view instead of resizing.)

    Define the size of your draggable corners.

    CGFloat kResizeThumbSize = 45.0f;
    

    Add these instance variables to your class to keep track of touch state and which way we're resizing.

    @interface MY_CLASS_NAME : UIView {
        BOOL isResizingLR;
        BOOL isResizingUL;
        BOOL isResizingUR;
        BOOL isResizingLL;
        CGPoint touchStart;
    }
    

    Handle the touch start / change events.

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [[event allTouches] anyObject];
        touchStart = [[touches anyObject] locationInView:self];
        isResizingLR = (self.bounds.size.width - touchStart.x < kResizeThumbSize && self.bounds.size.height - touchStart.y < kResizeThumbSize);
        isResizingUL = (touchStart.x 

提交回复
热议问题