简体   繁体   中英

Setting the Min and Max width and height for resizable UIView

I wanted to make a resizable UIView using touches from corners, and I've found this answer here and it worked well. But how can I add the ability to stop resizing when the width/height is 200 for example?

This is the code:

CGFloat kResizeThumbSize = 45.0f;

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

And..

- (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 <kResizeThumbSize && touchStart.y <kResizeThumbSize);
isResizingUR = (self.bounds.size.width-touchStart.x < kResizeThumbSize && touchStart.y<kResizeThumbSize);
isResizingLL = (touchStart.x <kResizeThumbSize && self.bounds.size.height -touchStart.y <kResizeThumbSize);
}

 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint touchPoint = [[touches anyObject] locationInView:self];
CGPoint previous = [[touches anyObject] previousLocationInView:self];

CGFloat deltaWidth = touchPoint.x - previous.x;
CGFloat deltaHeight = touchPoint.y - previous.y;

// get the frame values so we can calculate changes below
CGFloat x = self.frame.origin.x;
CGFloat y = self.frame.origin.y;
CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height;

if (isResizingLR) {
    self.frame = CGRectMake(x, y, touchPoint.x+deltaWidth, touchPoint.y+deltaWidth);
} else if (isResizingUL) {
    self.frame = CGRectMake(x+deltaWidth, y+deltaHeight, width-deltaWidth, height-deltaHeight);
} else if (isResizingUR) {
    self.frame = CGRectMake(x, y+deltaHeight, width+deltaWidth, height-deltaHeight);      
} else if (isResizingLL) {
    self.frame = CGRectMake(x+deltaWidth, y, width-deltaWidth, height+deltaHeight);   
} else {
    // not dragging from a corner -- move the view
    self.center = CGPointMake(self.center.x + touchPoint.x - touchStart.x,
        self.center.y + touchPoint.y - touchStart.y);
}
} 

Thanks

if (isResizingLR) {
   if(touchPoint.x+deltaWidth < 200 && touchPoint.y+deltaWidth < 200)
     self.frame = CGRectMake(x, y, touchPoint.x+deltaWidth, touchPoint.y+deltaWidth);
} else if (isResizingUL) {
   if(width-deltaWidth < 200 && height-deltaHeight < 200)
     self.frame = CGRectMake(x+deltaWidth, y+deltaHeight, width-deltaWidth, height-deltaHeight);
} else if (isResizingUR) {
   if(width+deltaWidth < 200 && height-deltaHeight < 200)
     self.frame = CGRectMake(x, y+deltaHeight, width+deltaWidth, height-deltaHeight);      
} else if (isResizingLL) {
   if(width-deltaWidth < 200 && height+deltaHeight < 200)
      self.frame = CGRectMake(x+deltaWidth, y, width-deltaWidth, height+deltaHeight);   
}

This should work.

@property(nonatomic) CGRect frame

Description The frame rectangle, which describes the view's location and size in its superview's coordinate system.

Before setting self.frame with CGRect, check for width and height!

CGRect CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height);

Use width and height for actual size and delta for transformation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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