【问题标题】:How to scale (zoom) a UIView to a given CGPoint如何将 UIView 缩放(缩放)到给定的 CGPoint
【发布时间】:2013-08-22 03:56:06
【问题描述】:

我花了很多时间试图找到一种使用 CGAffineScale 将视图转换为给定点的方法,包括处理锚点、在转换前后移动视图中心以及全面的谷歌搜索。我知道使用 UIScrollview 会简单得多;但我知道从技术上讲,没有它是可行的,而且它在我的脑海中变成了碎片。

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

如何修改 mvds' code 以将 UIView 缩放到 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) {}];

【问题讨论】:

    标签: ios objective-c zooming cgaffinetransform


    【解决方案1】:

    涉及两个步骤:首先,放大要放大的视图。然后你设置这个放大视图的中心,这样你想看到的部分就在视图的中间。

    你应该在纸上画出来,公式如下:(未经测试)

    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) {}];
    

    【讨论】:

    • 非常感谢您的回答。我确实尝试过,但它仍然放大到不正确的点。我也试过在纸上画这个,但逻辑不通。
    【解决方案2】:

    实际上我自己也遇到了同样的问题。为了解决这个问题,我所做的只是更改了我正在缩放的​​视图的锚点,因为 CGAffineTransforms 是在相对于其锚点的视图上执行的,因此根据锚点的位置,变换将缩放、平移或旋转看法不同。基本思路如下:

    CGPoint pointToScaleTo = CGPointMake(x, y); //Just enter the coordinates you 
                                                //want to scale your view towards
    
    CGFloat viewWidth = self.view.bounds.size.width;
    CGFloat viewHeight = self.view.bounds.size.height;
    
    CGFloat scaleFactorX = ...;
    CGFloat scaleFactorY = ...;
    
    CGAffineTransform scaleTransform = CGAffineTransformMakeScale(scaleFactorX, scaleFactorY);
    
    [UIView animateWithDuration:2.5f delay:0.0f options:0 animations:^{
    
        //I divide the x and y coordinates by the view width and height
        //because the anchor point coordinates are normalized to the range
        //0.0-1.0.
        self.view.layer.anchorPoint = CGPointMake(pointToScaleTo.x/viewWidth, pointToScaleTo.y/viewHeight);
    
        //Now that the anchor point has been changed, apply the scale transform
        self.view.layer.transform = scaleTransform;
    
    } completion:^(BOOL finished) {}];
    

    【讨论】:

    • 感谢您的回答。由于 iOS7(我认为),我将 CGAffineTransform 行更改为 CATransform3D scaleTransform3D = CATransform3DMakeScale(scaleFactorX, scaleFactorY, 1.0);(view.layer.transform 必须是 CATransform3D)。现在发生的情况是视图跳转,使pointToScaleTo 位于中心(这并不理想——理想情况下,我想朝该点缩放而不是跳转到然后放大),然后旋转-90º。我不确定我从哪里得到轮换。你知道如何让它缩放到某个点而不是跳跃然后缩放吗?
    • 您可以使用self.view.layer.transform = CATransform3DMakeAffineTransform(scaleTransform); 将仿射变换转换为CATransform3D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多