设置可调整大小的UIView的最小和最大宽度和高度

时间:2015-02-16 11:29:47

标签: ios objective-c iphone uiview ios8

我想使用角落的触摸来制作可调整大小的UIView,我已经找到了这个答案here并且效果很好。但是,当宽度/高度为200时,如何添加停止调整大小的功能?

这是代码:

CGFloat kResizeThumbSize = 45.0f;

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

和..

- (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);
}
} 

由于

2 个答案:

答案 0 :(得分:2)

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

这应该有用。

答案 1 :(得分:0)

@property(nonatomic) CGRect frame

描述框架矩形,用于描述超视图坐标系中视图的位置和大小。

在使用CGRect设置self.frame之前,请检查宽度和高度!

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

使用宽度和高度作为实际尺寸和delta进行转换。