当我的UIView
使用transform属性(CGAffineTransformMakeRotation
)旋转时,我需要拖动它的一个角,比如右下角,来调整它的大小。在此过程中,当用户拖动角时,视图的角必须跟随用户的手指,并通过增加2个边来调整框的大小(右下角拖动时为右大小和下大小)。
当框不被变换时,当你可以使用框架和触摸位置时,调整UIView
的大小并不是那么困难。例如,我会记住StateBegan上这个可调整大小的视图附带的UIPanGestureRecognizer
handler
中的初始帧和触摸,在StateChanged上,我会计算触摸点X,Y与初始触摸的差异,并将这些值添加到初始帧宽度和高度上。
然而,当应用变换时,帧对于旋转UIView
是可靠的。因此,我只能依靠bounds
和center
。我创建了这段代码,但它几乎可以工作:我只能在所有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、N-M距离)的旋转视图:
https://stackoverflow.com/questions/20083281
复制相似问题