ios - 使用 Transform 旋转时调整 UIView 的大小

标签 ios iphone objective-c uigesturerecognizer

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

当您可以在框未转换时使用框架和触摸位置时,调整 UIView 的大小并不难。例如,我会记得 UIPanGestureRecognizer handler 中的初始帧和触摸 StateBeganStateChanged 我会计算触摸点 X、Y 与初始触摸的差异,并将这些值添加到初始帧宽度和高度。

但是,当应用变换时,框架对于旋转的 UIView 是可靠的。因此我只能依靠 boundscenter。我创建了这段代码,但它几乎可以工作:我只能在所有 4 个方向上按比例放大 View ,而不是一个。

- (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 距离)的旋转 View :

enter image description here

最佳答案

好问题。我刚刚创建了使用类似内容的类。在我的类(class)中,您可以更改 UIViewA 的比例,它会更改 UIViewB 的比例,同时保持相同的比例。

检查左上角的白色小方 block :

enter image description here

类可以在这里找到:https://github.com/sSegev/SSUIViewMiniMe

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

因为你不能在按钮动画时点击它(好吧,你可以,但动画只是眼睛糖果, View 已经处于它的最终状态)我使用了 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 : %f y : %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);
        }
}

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

enter image description here *

仅仅 6 行代码就很酷了哈?

关于ios - 使用 Transform 旋转时调整 UIView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20083281/

相关文章:

ios - cdvfile ://不支持的 URL 错误

iphone - AudioSession 实际上和 OpenAL 是一样的吗?

iOS 在键盘之前启动 UIView?

ios - 在 iOS 应用程序中集成 CCAvenue 时出现问题

iphone - 自定义 UIButton 图片按钮类型

ios - 调用 touchesEnded : when tapped but not swiped

ios - 为什么 subview Controller 会占据整个屏幕?

ios - 如果来自 iOS 上的同一对话,如何在通知中心显示 "Group/Collapse"多个通知

iphone - 返回 autorelease NSString 仍然会导致内存泄漏

iphone - 在 iOS 上添加与 tiff 文件的文件关联