UIButton 通过动画调整大小

发布于 2024-09-07 13:13:47 字数 822 浏览 3 评论 0原文

我正在创建一个菜单,我想按钮“弹出”在屏幕上。基本上我想从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼睛会笑 2024-09-14 13:13:47

您可以尝试使用下面的代码吗?

button.transform = CGAffineTransformMakeScale(1.5,1.5);
button.alpha = 0.0f;

[UIView beginAnimations:@"button" context:nil];
[UIView setAnimationDuration:1];
     button.transform = CGAffineTransformMakeScale(1,1);
     button.alpha = 1.0f;
[UIView commitAnimations];

您的按钮应该开始时稍大一些,然后缩小。如果缩放正确,只需调整前后的比例因子即可。

Could you try using the following code instead?

button.transform = CGAffineTransformMakeScale(1.5,1.5);
button.alpha = 0.0f;

[UIView beginAnimations:@"button" context:nil];
[UIView setAnimationDuration:1];
     button.transform = CGAffineTransformMakeScale(1,1);
     button.alpha = 1.0f;
[UIView commitAnimations];

Your button should start slightly larger and then shrink back down. If this scales properly, just adjust the scalefactor before and after to suit.

つ可否回来 2024-09-14 13:13:47

针对 Swift 进行了更新:

您可以将其插入到绑定到按钮的任何 IBAction 中,以便在触摸按钮时为其设置动画。

    // animate button
    // play with the scale (1.25) to adjust to your liking
    yourButton.transform = CGAffineTransformMakeScale(1.25, 1.25)
    yourButton.alpha = 0.0

    UIView.beginAnimations("yourButton", context: nil)
    // play with the animationDuration to adjust to your liking
    UIView.setAnimationDuration(0.25)
    yourButton.transform = CGAffineTransformMakeScale(1.0, 1.0)
    yourButton.alpha =  1.0

Updated for Swift:

You can insert this in whatever IBAction you've got tied to a button to animate it when it's touched.

    // animate button
    // play with the scale (1.25) to adjust to your liking
    yourButton.transform = CGAffineTransformMakeScale(1.25, 1.25)
    yourButton.alpha = 0.0

    UIView.beginAnimations("yourButton", context: nil)
    // play with the animationDuration to adjust to your liking
    UIView.setAnimationDuration(0.25)
    yourButton.transform = CGAffineTransformMakeScale(1.0, 1.0)
    yourButton.alpha =  1.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文