aka*_*aru 20 iphone core-animation quartz-graphics cgaffinetransform ios
我不了解转化的东西.我想放大一下UIView的右上角(主视图).我使用CGAffineTransformScale并试过设置center/anchorPoint以及CGAffineTransformMakeTranslation无效.
我无法弄清楚如何正确设置翻译,以便放大这一点.
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) {}];
Run Code Online (Sandbox Code Playgroud)
mvd*_*vds 34
您正在将视图的中心设置在右上角.这意味着你要放大到中心左下角的某个地方.
试试这个
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) {}];
Run Code Online (Sandbox Code Playgroud)
这会将放大视图的中心设置为左下角,有效地放大,右上角固定.
此代码没有硬编码的高度,这是一个更便携(到iPhone 5的iPad).
您需要先h在设置transform属性之前保存,因为之后您不应该依赖于该值frame.
编辑
要使其适用于任何比例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) {}];
Run Code Online (Sandbox Code Playgroud)
要使其适用于左下角,请使用以下命令:
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) {}];
Run Code Online (Sandbox Code Playgroud)
要使其适用于右下角,请使用以下命令:
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) {}];
Run Code Online (Sandbox Code Playgroud)
另请参见:如何将UIView缩放(缩放)到给定的CGPoint