(比例)在某一点放大UIView [英] (Scale) Zoom into a UIView at a point

查看:127
本文介绍了(比例)在某一点放大UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对转换不了解的东西。我想放大一下UIView(主视图)的右上角。我使用 CGAffineTransformScale 并尝试设置center / anchorPoint以及 CGAffineTransformMakeTranslation 无效。

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 CGAffineTransformScale and have tried both setting the center/anchorPoint as well as CGAffineTransformMakeTranslation to no avail.

我无法弄清楚如何正确设置翻译,以便放大这一点。

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


推荐答案

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

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.

试试这个

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.

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

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

在设置转换属性之前,您需要先保存 h ,因为之后你不应该依赖 frame 的价值。

You need to first save h before setting the transform property, because after that you should not rely on the value of frame.

编辑

要使其适用于任何比例 s ,请使用:

To make it work for any scale s, 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*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) {}];

参见:如何将UIView缩放(缩放)到给定的CGPoint

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

这篇关于(比例)在某一点放大UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆