使用手势缩放 UIlabel 文本

发布于 2024-12-10 19:46:01 字数 1181 浏览 0 评论 0原文

我正在使用 UIPinchGestureRecognizer 缩放 UILabel 文本。但我无法实现文本的平滑边缘或平滑度。

在此处输入图像描述

缩放代码:

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
        [pinchRecognizer setDelegate:self];
        [holderView addGestureRecognizer:pinchRecognizer];


  -(void)scale:(id)sender {

        [self bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];

        if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

            lastScale = 1.0;
            return;
        }


        CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);


        CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
        CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

        [[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];

        lastScale = [(UIPinchGestureRecognizer*)sender scale];
    }

在此输入图像描述

I am scaling UILabel text using UIPinchGestureRecognizer. But I am not able achieve the smooth edges or smoothness of Text.

enter image description here

code for scaling:

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
        [pinchRecognizer setDelegate:self];
        [holderView addGestureRecognizer:pinchRecognizer];


  -(void)scale:(id)sender {

        [self bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];

        if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

            lastScale = 1.0;
            return;
        }


        CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);


        CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
        CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

        [[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];

        lastScale = [(UIPinchGestureRecognizer*)sender scale];
    }

enter image description here

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

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

发布评论

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

评论(1

迷爱 2024-12-17 19:46:01

问题是您正在使用转换进行缩放。这会采用原始​​渲染并将其放大或缩小,但不会改变它。换句话说:“质量”不会改变,只会改变显示的尺寸。相反,您想要的可能是执行以下步骤:

  • 将转换重置回身份(下一步需要执行此操作)。
  • 计算并应用新的框架尺寸,重新定位视图。
  • 应用旋转变换。

通过更改帧大小,将使用新的大小再次绘制视图,这将比放大较小的视图获得更好的质量。

我不确定在捏合过程中是否可以更改框架(这可能会干扰识别器),也许您需要应用您现在正在执行的转换,并在手势时执行更改框架大小方法完成后,捏合完成后可以获得高质量版本(有时您可以在滚动视图中看到类似的图案)。

The problem is that you're scaling using a transformation. That takes the original rendering and scales it up or down, but doesn't change it. In other words: the "quality" doesn't change, only at what size you're displaying it. What you want instead is probably to do the following steps:

  • Reset the transformation back to identity (need to do this for the next step).
  • Calculate and apply new frame size, reposition view.
  • Apply rotation transformation.

By changing the frame size the view is drawn again with the new size which will result in better quality than scaling up a smaller view.

I'm not sure whether changing the frame is possible during pinching (it might be messing with the recognizer), maybe you need to apply the transformation you're doing right now and do the change-frame-size-method when the gesture is done to get a high quality version after the pinch is finished (you can see a similar pattern sometimes with scroll views).

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