首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在使用变换旋转UIView时调整其大小

在使用变换旋转UIView时调整其大小
EN

Stack Overflow用户
提问于 2013-11-20 06:19:33
回答 2查看 2.8K关注 0票数 21

当我的UIView使用transform属性(CGAffineTransformMakeRotation)旋转时,我需要拖动它的一个角,比如右下角,来调整它的大小。在此过程中,当用户拖动角时,视图的角必须跟随用户的手指,并通过增加2个边来调整框的大小(右下角拖动时为右大小和下大小)。

当框不被变换时,当你可以使用框架和触摸位置时,调整UIView的大小并不是那么困难。例如,我会记住StateBegan上这个可调整大小的视图附带的UIPanGestureRecognizer handler中的初始帧和触摸,在StateChanged上,我会计算触摸点X,Y与初始触摸的差异,并将这些值添加到初始帧宽度和高度上。

然而,当应用变换时,帧对于旋转UIView是可靠的。因此,我只能依靠boundscenter。我创建了这段代码,但它几乎可以工作:我只能在所有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距离)的旋转视图:

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20083281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档