UIButton 通过动画调整大小
我正在创建一个菜单,我想按钮“弹出”在屏幕上。基本上我想从 0px 尺寸开始,然后达到按钮的完整尺寸。如果我愿意的话,我可以为 Alpha 和位置设置动画,但无法确定尺寸,我认为这是因为它是按钮上的图像。
如果我执行 UIButtonTypeRoundRect,您可以看到按钮在图像后面进行动画处理,但图像是静态的。
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"settings.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(20, 20, 0, 0);
button.alpha = 0;
[self.view addSubview:button];
CGRect frame = button.frame;
[UIView beginAnimations:@"button" context:nil];
[UIView setAnimationDuration:1];
button.alpha = 1;
frame.size.width += 53;
frame.size.height += 53;
button.frame = frame;
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
所以 Alpha 可以工作,但调整大小却不行。我还尝试过使用stretchableImageWithLeftCapWidth来尝试为其提供上下文或其他内容,但无济于事。
为你的帮助干杯。
I'm creating a menu and I want the buttons to "pop" I guess on the screen. Basically I want to start at 0px dimensions and then go up to the full size of the buttons. I can animate the alpha and the position if I want to but can't do the dimensions and I think its because its an image on the button.
If I do a UIButtonTypeRoundRect you can see the button being animated behind the image but the image is static.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"settings.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(20, 20, 0, 0);
button.alpha = 0;
[self.view addSubview:button];
CGRect frame = button.frame;
[UIView beginAnimations:@"button" context:nil];
[UIView setAnimationDuration:1];
button.alpha = 1;
frame.size.width += 53;
frame.size.height += 53;
button.frame = frame;
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
So the Alpha works but the resize doesn't. I've also played with stretchableImageWithLeftCapWidth to try and give it context or something but to no avail.
Cheers for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用下面的代码吗?
您的按钮应该开始时稍大一些,然后缩小。如果缩放正确,只需调整前后的比例因子即可。
Could you try using the following code instead?
Your button should start slightly larger and then shrink back down. If this scales properly, just adjust the scalefactor before and after to suit.
针对 Swift 进行了更新:
您可以将其插入到绑定到按钮的任何 IBAction 中,以便在触摸按钮时为其设置动画。
Updated for Swift:
You can insert this in whatever IBAction you've got tied to a button to animate it when it's touched.