如何通过从边缘拖动来调整UIView的大小?

在我的iPad应用程序中,我希望用户能够通过从其边缘拖动视图来调整UIView的大小。 我将使用iOS 5 SDK,那么最干净的方法是什么? 有没有其他的办法来实现这个没有处理touchesBegan,touchesMoved,等等?

我猜你的用户界面涉及视图两侧的某种types的句柄,并且将一个简单的UIPanGestureRecognizer到这些句柄控件中,这使得整个问题变得非常容易。

在手势识别器的操作方法中,只需获取相对于要resize的视图的-translationInView:当手势识别器的状态为UIGestureRecognizerStateBegan时保存原始帧,并在状态为UIGestureRecognizerStateChanged不断调整视图的帧。

您可以通过检查触摸起点来做到这一点。 如果它碰到四个angular中的一个,则可以根据该触摸起点与当前触点之间的距离来resize。 (如果触摸起点没有碰到拐angular,我们只是移动视图而不是resize。)

定义可拖动angular的大小。

 CGFloat kResizeThumbSize = 45.0f; 

将这些实例variables添加到您的类中,以跟踪触摸状态以及调整方式。

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

@Prerna Chavan解决scheme的快捷版本,Prerna解决scheme不检测用户是否触摸边缘,它只检测angular落,但下面的代码检测所有

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