UIButton通过动画调整大小

Rud*_*ger 15 iphone animation uibutton uiview uiimage

我正在创建一个菜单,我希望按钮在屏幕上"弹出".基本上我想从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];
Run Code Online (Sandbox Code Playgroud)

所以Alpha工作但调整大小没有.我也玩过stretchchableImageWithLeftCapWidth尝试给它上下文或其他东西,但无济于事.

干杯,为您提供帮助.

dav*_*ryn 32

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

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];
Run Code Online (Sandbox Code Playgroud)

您的按钮应该稍微开始然后缩小.如果这可以正确缩放,只需调整前后比例因子即可.


Adr*_*ian 5

更新为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
Run Code Online (Sandbox Code Playgroud)