为可调整大小的 UIView 设置最小和最大宽度和高度
Posted
技术标签:
【中文标题】为可调整大小的 UIView 设置最小和最大宽度和高度【英文标题】:Setting the Min and Max width and height for resizable UIView 【发布时间】:2015-02-16 11:29:46 【问题描述】:我想使用角落的触摸来制作一个可调整大小的 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);
谢谢
【问题讨论】:
【参考方案1】: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);
这应该可行。
【讨论】:
【参考方案2】:@property(nonatomic) CGRect frame
描述框架矩形,描述视图在其父视图坐标系中的位置和大小。
在使用 CGRect 设置 self.frame 之前,检查宽度和高度!
CGRect CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height);
使用宽度和高度作为实际尺寸,使用增量进行变换。
【讨论】:
以上是关于为可调整大小的 UIView 设置最小和最大宽度和高度的主要内容,如果未能解决你的问题,请参考以下文章
Jquery 可调整大小的问题,其中 div 合同的宽度和高度为零