使用变换旋转时调整UIView的大小

当我的UIView旋转变换属性( CGAffineTransformMakeRotation ),我需要拖动它的一个angular落,说右下angular,调整它的大小。 在此过程中,当用户拖动angular落时,视图的angular落必须跟随用户的手指,并通过增加2个边(右下angular和右下angular拖动的底部尺寸)来调整框的大小。

调整UIView大小并不那么困难,因为当您不能转换盒子时,可以使用框架和触摸位置。 例如,我会记得初始框架和触摸附加到UIPanGestureRecognizer这个可resize的视图的UIPanGestureRecognizer handler ,并在StateChanged我会计算从初始触摸的触摸点X,Y差异,并将这些值添加到初始帧的宽度和高度。

但是,在应用转换时,对于旋转的UIView ,帧是可靠的。 所以我只能靠boundscenter 。 我创build了这个代码,但它几乎工作:我只能按比例放大所有4个方向,而不是一个。

 - (void)handleResizeGesture:(UIPanGestureRecognizer *)recognizer { CGPoint touchLocation = [recognizer locationInView:self.superview]; CGPoint center = self.center; switch (recognizer.state) { case UIGestureRecognizerStateBegan: { deltaAngle = atan2f(touchLocation.y - center.y, touchLocation.x - center.x) - CGAffineTransformGetAngle(self.transform); initialBounds = self.bounds; initialDistance = CGPointGetDistance(center, touchLocation); initialLocation = touchLocation; if ([self.delegate respondsToSelector:@selector(stickerViewDidBeginRotating:)]) { [self.delegate stickerViewDidBeginRotating:self]; } break; } case UIGestureRecognizerStateChanged: { CGFloat scale = CGPointGetDistance(center, touchLocation)/initialDistance; CGFloat minimumScale = self.minimumSize/MIN(initialBounds.size.width, initialBounds.size.height); scale = MAX(scale, minimumScale); CGRect scaledBounds = CGRectScale(initialBounds, scale, scale); self.bounds = scaledBounds; [self setNeedsDisplay]; if ([self.delegate respondsToSelector:@selector(stickerViewDidChangeRotating:)]) { [self.delegate stickerViewDidChangeRotating:self]; } break; } case UIGestureRecognizerStateEnded: if ([self.delegate respondsToSelector:@selector(stickerViewDidEndRotating:)]) { [self.delegate stickerViewDidEndRotating:self]; } break; default: break; } } 

下图显示已知值(A,B,x,y,N,M,NM距离)的旋转视图:

在这里输入图像说明

很好的问题。 我刚刚创build了类似的东西。 在我的课堂上,你可以改变UIViewA的规模,它会改变UIViewB的规模,同时保持相同的比例。

检查左上方的小白方块:

在这里输入图像说明

这个类可以在这里find: https : //github.com/sSegev/SSUIViewMiniMe

你所问的有点不同。 我正在改变正方形的总大小,而只想根据用户拖动的距离来改变相对于其中一个angular的大小。

因为在animation中你不能点击button(可以,但animation只是眼睛的糖果,视图已经处于最终状态)我使用了CABasicAnimation,这使得它更容易。

 - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; //Rotate the UIView if ([_myRedView.layer animationForKey:@"SpinAnimation"] == nil) { CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; animation.fromValue = [NSNumber numberWithFloat:0.0f]; animation.toValue = [NSNumber numberWithFloat: 2*M_PI]; animation.duration = 50.0f; animation.repeatCount = INFINITY; [self.myRedView.layer addAnimation:animation forKey:@"SpinAnimation"]; } } // That's the main chunk of code: - (void)dragBegan:(UIControl *)c withEvent:ev { UITouch *touch = [[ev allTouches] anyObject]; CGPoint touchPoint = [touch locationInView:_myRedView]; NSLog(@"Touch x : %fy : %f", touchPoint.x, touchPoint.y); if(_myRedView.width/2<touchPoint.x && _myRedView.height/2<touchPoint.y) //Checks for right bottom corner { _myRedView.width =touchPoint.x; _myRedView.height = touchPoint.y; dragButton.frame = CGRectMake(0, 0, _myRedView.width, _myRedView.height); } } 

我确定需要一些调整,但现在看起来像这样:

在这里输入图像说明 *

非常酷的只有6行代码哈?

根据您的手势,而不是尝试更改框架,调整视图的scale属性。 这应该适当调整您的视图,而不会干扰旋转。

将您的视图embedded到容器视图中。 然后转换容器 ,使可拖动的视图保持不变。 使用您用于未旋转视图的相同逻辑。