Comment resize UIView en le faisant glisser depuis ses bords?

Dans mon application iPad, je souhaite que les users puissent resize un UIView en faisant glisser la vue depuis ses bords. Je vais utiliser iOS 5 SDK, alors quelle est l'approche la plus propre à faire cela? Existe-t-il des alternatives pour y parvenir sans toucher aux touchesBegan, touchesMoved, … etc?

Je suppose que votre interface user implique un certain type de poignées sur les côtés de la vue, et attacher un simple UIPanGestureRecognizer à ces controls de poignée rend le problème assez facile.

Dans la méthode d'action de l' -translationInView: reconnaissance de gestes, obtenez simplement -translationInView: rapport à la vue que vous redimensionnez, enregistrez le cadre d'origine lorsque l'état de reconnaissance gestuelle est UIGestureRecognizerStateBegan et ajustez continuellement le cadre de la vue pendant que l'état est UIGestureRecognizerStateChanged .

Vous pouvez le faire en vérifiant le sharepoint départ. S'il atteint l'un de vos quatre coins, vous pouvez le resize en fonction de la distance entre ce sharepoint départ et le sharepoint contact actuel. (Si le sharepoint départ tactile n'a pas atteint un coin, nous déplaçons simplement la vue au lieu de resize.)

Définissez la taille de vos coins pouvant être déplacés.

 CGFloat kResizeThumbSize = 45.0f; 

Ajoutez ces variables d'instance à votre class pour garder une trace de l'état tactile et de la manière dont nous le redimensionnons.

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

Gérer les events de démarrage / changement tactile.

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

Version rapide de la solution @Prerna Chavan, la solution Prerna ne détecte pas si l'user touche sur les bords, il ne détecte que les coins, mais le code ci-dessous détecte tout

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