本文介绍了如何将UIView缩放(缩放)到给定的CGPoint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很多时间试图找到一种方法来使用CGAffineScale将视图转换为给定点,包括弄乱锚点,在转换之前和之后移动视图的中心以及全面的Google搜索。我知道使用UIScrollview会更简单;但是我知道技术上可以不用一个,而且它在我脑海里变成了分裂。

I've spent a lot of time trying to find a way to use CGAffineScale to transform a view to a given point, including messing around with anchor points, moving the centre of a view before and after transforming and comprehensive Googling. I am aware this would be a lot simpler with a UIScrollview; but I know it's technically possible to do without one, and it's become a splinter in my mind.

非常接近我想要实现的目标,但答案只是通过巧妙地将中心移动到角落来提供有关如何缩放到给定角落(而不是给定点)的详细信息与您要放大的那个相对。

This answer gets remarkably close to what I want to achieve, but the answer only gives details on how to zoom to a given corner (instead of a given point) by cleverly moving the centre to the corner opposite the one you want to zoom in to.

如何修改将UIView缩放到UIView中的任何给定点?

How can I modify mvds' code to scale a UIView to any given point in a UIView?

CGFloat s = 3;
CGAffineTransform tr = CGAffineTransformScale(self.view.transform, s, s);
CGFloat h = self.view.frame.size.height;
CGFloat w = self.view.frame.size.width;
[UIView animateWithDuration:2.5 delay:0 options:0 animations:^{
    self.view.transform = tr;
    self.view.center = CGPointMake(w-w*s/2,h*s/2);
} completion:^(BOOL finished) {}];


推荐答案

涉及两个步骤:首先你扩大规模查看要放大的视图。然后设置这个放大视图的中心,使得您想要看到的部分最终位于视图的中间。

There are 2 steps involved: First you scale up the view you want to zoom in to. Then you set the center of this blown up view such that the part you want to see ends up in the middle of the view.

您应该在纸上画出来公式将遵循:(未经测试)

You should draw this out on paper and the formulas will follow: (untested)

CGFloat s = 3;
CGPoint p = CGPointMake(100, 200);
CGAffineTransform tr = CGAffineTransformScale(self.view.transform, s, s);
CGFloat h = self.view.frame.size.height;
CGFloat w = self.view.frame.size.width;
[UIView animateWithDuration:2.5 delay:0 options:0 animations:^{
    self.view.transform = tr;
    CGFloat cx = w/2-s*(p.x-w/2);
    CGFloat cy = h/2-s*(p.y-h/2);
    self.view.center = CGPointMake(cx, cy); //was: (w*s/2,h-h*s/2);
} completion:^(BOOL finished) {}];

这篇关于如何将UIView缩放(缩放)到给定的CGPoint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:50