在 UIView 的中心缩放 UIImageView
我正在尝试制作一个缩放环,它在 UIView 的中心缩放。
设置如下:
在IB中,我有一个视图和附加到它的ImageView(只是一个带环的透明PNG)。 设置一个 IBOutlet 到 ImageView (theRing) 和一个按钮来触发操作。
ImageView 在 IB 中设置为“scaleToFill”。
该操作的代码是:
-(IBAction)buttonPressed:(id)sender{
CGRect theFrame = theRing.frame;
theFrame.size.width = 16;
theFrame.size.height = 16;
[theRing setFrame:theFrame]; //Scale down the ring first
theRing.center = [self.view center]; // Center the ring in the center of the view
theFrame.size.width = 300;
theFrame.size.height = 300;
[theRing setAlpha:1.0];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[theRing setFrame: theFrame];
[theRing setAlpha:0.0];
theRing.center = [self.view center];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector: @selector(animEnd:finished:context:)];
[UIView commitAnimations];
}
现在 - 当我第一次点击按钮时,图像会以 ImageView 左上角的参考点进行缩放(意味着它会缩放,但环会扩展到图像的右下角)屏幕)。
但是:当我再次按下按钮时,整个事情按预期工作(意味着环停留在屏幕中间并缩放)。
有什么想法吗?
I'm trying to make a scaling ring, which scales in the center of a UIView.
Setup is the following:
In IB, I have a view and my ImageView attached to it (just a transparent PNG with a ring).
Setup an IBOutlet to the ImageView (theRing) and a button to trigger the action.
ImageView is set to «scaleToFill» in IB.
The code for the action is:
-(IBAction)buttonPressed:(id)sender{
CGRect theFrame = theRing.frame;
theFrame.size.width = 16;
theFrame.size.height = 16;
[theRing setFrame:theFrame]; //Scale down the ring first
theRing.center = [self.view center]; // Center the ring in the center of the view
theFrame.size.width = 300;
theFrame.size.height = 300;
[theRing setAlpha:1.0];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[theRing setFrame: theFrame];
[theRing setAlpha:0.0];
theRing.center = [self.view center];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector: @selector(animEnd:finished:context:)];
[UIView commitAnimations];
}
Now - when I hit the button the first time, the image is scaled with the reference point being the upper left corner of the ImageView (means, it scales, but the ring expands to the lower right of the screen).
BUT: When I hit the button again, the whole thing works as expected (means, the ring stays in the middle of the screen and is scaled).
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 ImageView 模式 Aspect Fill
try to use ImageView mode Aspect Fill