ios (Scale) 在一个点放大 UIView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5446899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:23:59  来源:igfitidea点击:

(Scale) Zoom into a UIView at a point

iphoneioscore-animationquartz-graphicscgaffinetransform

提问by akaru

Something I don't understand about transformations. I want to zoom in to say the top right corner of a UIView (the main view). I use CGAffineTransformScaleand have tried both setting the center/anchorPoint as well as CGAffineTransformMakeTranslationto no avail.

我不明白关于转换的一些事情。我想放大说一个 UIView(主视图)的右上角。我使用CGAffineTransformScale并尝试过设置中心/锚点以及CGAffineTransformMakeTranslation无济于事。

I can't figure out how to set the translation properly so that it will zoom in on this point.

我不知道如何正确设置翻译,以便在这一点上放大。

CGAffineTransform tr = CGAffineTransformScale(self.view.transform, 2, 2);

[UIView animateWithDuration:2.5 delay:0 options:0 animations:^{
            self.view.transform = tr;
            self.view.center = CGPointMake(480,0);

} completion:^(BOOL finished) {}];

回答by mvds

You are setting the center of the view to the top right corner. That means you're zooming in on somewhere to the lower left of the center.

您将视图的中心设置为右上角。这意味着您正在放大中心左下角的某个地方。

Try this

尝试这个

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

This will set the center of the blown-up view to the left bottom corner, effectively zooming in with the top right corner fixed.

这会将放大视图的中心设置为左下角,在右上角固定的情况下有效地放大。

This code doesn't have the height hardcoded, which is a little more portable (to an iPad of iPhone 5).

这段代码没有硬编码的高度,这更便于携带(对于 iPhone 5 的 iPad)。

You need to first save hbefore setting the transformproperty, because after that you should not rely on the value of frame.

h在设置transform属性之前,您需要先保存,因为之后您不应该依赖frame.

edit

编辑

To make it work for any scale s, use this:

要使其适用于任何比例s,请使用以下命令:

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

To make it work for bottom left, use this:

要使其适用于左下角,请使用以下命令:

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*s/2,h-h*s/2);
} completion:^(BOOL finished) {}];

To make it work for bottom right, use this:

要使其适用于右下角,请使用以下命令:

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-h*s/2);
} completion:^(BOOL finished) {}];

See also: How to scale (zoom) a UIView to a given CGPoint

另请参阅:如何将 UIView 缩放(缩放)到给定的 CGPoint

回答by Nighthawk

In case anyone is still interested, here is the Swift 3.0 solution, to which I added the possibility to fade out correctly.

如果有人仍然感兴趣,这里是 Swift 3.0 解决方案,我添加了正确淡出的可能性。

(I know there are two enums missing, but the code is still readable and I think you will get the concept)

(我知道缺少两个枚举,但代码仍然可读,我想你会明白这个概念)

func animateZoom(direction: ZoomDirection, type: ZoomType, duration: Double = 0.3, scale: CGFloat = 1.4) {
    var cx, cy: CGFloat
    var tr: CGAffineTransform = CGAffineTransform.identity

    if (type == .IN) { tr = self.transform.scaledBy(x: scale, y: scale) }

    let h: CGFloat = self.frame.size.height;
    let w: CGFloat = self.frame.size.width;

    if (type == .IN) {
        switch direction {
        case .LEFT_DOWN:
            cx = w*scale/2
            cy = h-h*scale/2
            break
        case .LEFT_UP:
            cx = w*scale/2
            cy = h*scale/2
            break
        case .RIGHT_UP:
            cx = w-w*scale/2
            cy = h*scale/2
            break
        case .RIGHT_DOWN:
            cx = w-w*scale/2
            cy = h-h*scale/2
            break
        }
    } else {
        cx = w/scale/2
        cy = h/scale/2
    }

    UIView.animate(withDuration: duration, delay: 0, options: [.curveEaseInOut], animations: {
        self.transform = tr
        self.center = CGPoint(x: cx, y: cy)
    }, completion: { finish in })
}