当在 uiview 上使用捏合手势时,它的边界会改变吗?

发布于 2024-12-01 23:46:39 字数 3057 浏览 0 评论 0原文

我正在使用 UIPinchGestureRecognizer 来扩展/缩小 uiview。

UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleElement:)];
[pinchGesture setDelegate:self];
[element addGestureRecognizer:pinchGesture];
[pinchGesture release];

//缩放元素方法

- (void)scaleElement:(UIPinchGestureRecognizer *)gestureRecognizer {
    UIView *element = [gestureRecognizer view];
    [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan){
        lastTouchPosition = [gestureRecognizer locationInView:element];
    } 
    else if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged){
            CGPoint currentTouchPosition = [gestureRecognizer locationInView:element];
            CGPoint deltaMove = [self calculatePointDistancewithPoint1:currentTouchPosition andPoint2:lastTouchPosition];
            float distance = sqrt(deltaMove.x*deltaMove.x + deltaMove.y*deltaMove.y);
            float hScale = 1 - deltaMove.x/distance * (1-gestureRecognizer.scale);
            float vScale = 1 - deltaMove.y/distance * (1-gestureRecognizer.scale);
            if (distance == 0) {
                hScale = 1;
                vScale = 1;
            }
            element.transform = CGAffineTransformScale([element transform], hScale, vScale);
            CGAffineTransform transform = CGAffineTransformMakeScale(hScale, vScale);
            element.bounds = CGRectApplyAffineTransform(element.bounds, transform);
                [gestureRecognizer setScale:1];
                lastTouchPosition = currentTouchPosition;
            }
            if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) {
                NSLog(@"scaling over");
                NSLog(@"bounds = %@",NSStringFromCGRect(element.bounds));
                NSLog(@"frame = %@", NSStringFromCGRect(element.frame));
            }
            NSLog(@"scalePiece exit");
    }

//adjustAnchorPointForGestureRecognizer method

    - (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
        if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
            UIView *elementToAdjust = gestureRecognizer.view;
            [self.view bringSubviewToFront:gestureRecognizer.view];
            CGPoint locationInView = [gestureRecognizer locationInView:elementToAdjust];
            CGPoint locationInSuperview = [gestureRecognizer locationInView:elementToAdjust.superview];
            elementToAdjust.layer.anchorPoint = CGPointMake(locationInView.x / elementToAdjust.bounds.size.width, locationInView.y / elementToAdjust.bounds.size.height);
            elementToAdjust.center = locationInSuperview;
        }
    }

控制台打印:

bounds = {{0, 0}, {178.405, 179.018}}
frame = {{300.642, 566.184}, {192.899, 194.227}}

为什么框架改变时边界没有调整? 它与自动调整蒙版大小有什么关系,因为我有此视图的子视图?

I am using the UIPinchGestureRecognizer to expand/reduce a uiview.

UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleElement:)];
[pinchGesture setDelegate:self];
[element addGestureRecognizer:pinchGesture];
[pinchGesture release];

//Scale element method

- (void)scaleElement:(UIPinchGestureRecognizer *)gestureRecognizer {
    UIView *element = [gestureRecognizer view];
    [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan){
        lastTouchPosition = [gestureRecognizer locationInView:element];
    } 
    else if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged){
            CGPoint currentTouchPosition = [gestureRecognizer locationInView:element];
            CGPoint deltaMove = [self calculatePointDistancewithPoint1:currentTouchPosition andPoint2:lastTouchPosition];
            float distance = sqrt(deltaMove.x*deltaMove.x + deltaMove.y*deltaMove.y);
            float hScale = 1 - deltaMove.x/distance * (1-gestureRecognizer.scale);
            float vScale = 1 - deltaMove.y/distance * (1-gestureRecognizer.scale);
            if (distance == 0) {
                hScale = 1;
                vScale = 1;
            }
            element.transform = CGAffineTransformScale([element transform], hScale, vScale);
            CGAffineTransform transform = CGAffineTransformMakeScale(hScale, vScale);
            element.bounds = CGRectApplyAffineTransform(element.bounds, transform);
                [gestureRecognizer setScale:1];
                lastTouchPosition = currentTouchPosition;
            }
            if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) {
                NSLog(@"scaling over");
                NSLog(@"bounds = %@",NSStringFromCGRect(element.bounds));
                NSLog(@"frame = %@", NSStringFromCGRect(element.frame));
            }
            NSLog(@"scalePiece exit");
    }

//adjustAnchorPointForGestureRecognizer method

    - (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
        if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
            UIView *elementToAdjust = gestureRecognizer.view;
            [self.view bringSubviewToFront:gestureRecognizer.view];
            CGPoint locationInView = [gestureRecognizer locationInView:elementToAdjust];
            CGPoint locationInSuperview = [gestureRecognizer locationInView:elementToAdjust.superview];
            elementToAdjust.layer.anchorPoint = CGPointMake(locationInView.x / elementToAdjust.bounds.size.width, locationInView.y / elementToAdjust.bounds.size.height);
            elementToAdjust.center = locationInSuperview;
        }
    }

Console print:

bounds = {{0, 0}, {178.405, 179.018}}
frame = {{300.642, 566.184}, {192.899, 194.227}}

Why isn't the bounds adjusting when the frame is changing ?
Does it have anything to do with auto resizing masks as I have subviews to this view ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2024-12-08 23:46:39

这与UIPinchGestureRecognizer无关。这与您设置 transform 以执行缩放有关。更改变换不会更改边界。它只是更改此视图的坐标空间 (bounds) 映射到父视图的坐标空间 (frame) 的方式。如果您需要bounds来匹配frame,则必须直接更改其中一个,而不是使用transform。也就是说,您通常应该使用 transform 因为它要快得多。

编辑

如果您不想缩放,而是想调整视图大小,请调用setBounds:。您可以通过将转换应用于 bounds 而不是元素来找到新的 bounds

CGPoint currentTouchPosition = [gestureRecognizer locationInView:element];
CGPoint deltaMove = [self calculatePointDistancewithPoint1:currentTouchPosition andPoint2:lastTouchPosition];
...

CGAffineTransform transform = CGAffineTransformMakeScale(hScale, vScale);
self.bounds = CGRectApplyAffineTransform(self.bounds, transform);

This has nothing to do with UIPinchGestureRecognizer. This has everything to do with your setting the transform to perform scaling. Changing the transform does not change bounds. It just changes how this view's coordinate space (bounds) maps to the superview's coordinate space (frame). If you need bounds to match frame, you have to change one of them directly, not use transform. That said, you should generally use transform because it's much faster.

EDIT

If you don't mean to scale, but rather mean to resize the view, call setBounds:. You can find the new bounds by applying the transform to the bounds rather than the element.

CGPoint currentTouchPosition = [gestureRecognizer locationInView:element];
CGPoint deltaMove = [self calculatePointDistancewithPoint1:currentTouchPosition andPoint2:lastTouchPosition];
...

CGAffineTransform transform = CGAffineTransformMakeScale(hScale, vScale);
self.bounds = CGRectApplyAffineTransform(self.bounds, transform);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文