iPhone - 如何动画按钮时pressed? [英] iPhone - How to animate a button when pressed?

查看:98
本文介绍了iPhone - 如何动画按钮时pressed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在iPhone上点击按钮时,使自定义动画?我想类似App Store的按钮 - 它显示的价格,然后当你点击它,它会改变颜色和文字的变化,现在购买,那么当您再次单击它,它完成了购买

Is there a way to make a custom animation when clicking on an iPhone button? I would like something like the App Store button - it shows the price, and then when you click on it, it changes color and the text changes to buy now, then when you click it again, it completes the purchase.

的UIViewAnimationTransition只包含不提供的全部功能的几个值。是否有可能做这样的事情,但与动画:

The UIViewAnimationTransition only contains a few values which don't provide the full functionality. Is it possible to do something like this but with the animation:

- (IBAction) changeButtonDisplay:(id)sender {

  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:0.8];
  [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:purchaseButton cache:NO]; //the built in UIViewAnimationTransition values don't provide enough flexibility

  [purchaseButton setTitle:@"Buy Now" forState:UIControlStateNormal];
  [purchaseButton setBackgroundColor:[UIColor greenColor]];
  [purchaseButton addTarget:self action:@selector(purchaseItems:) forControlEvents:UIControlEventTouchUpInside];

  [UIView commitAnimations];

}

这code工程,并显示适当的过渡,并允许第二次点击做适当的行动,但标题和颜色的变化瞬间,而不是在一个平滑的动画。是否可以很容易地做这样的动画,做我必须创建一个UIButton子类,做动画手动?如果是这样,我想有什么方法重写?

This code works and will display the proper transition and allow for the second click to do the proper action, but the title and color changes instantly, not in a smooth animation. Is it possible to do such an animation easily, of do I have to create a UIButton subclass and do the animation "manually"? If so, what method would I have to override?

推荐答案

而不是仅仅改变了按钮的标题和目标等,有两个单独的按钮,做每个动作和神态各异。然后,当你preSS按钮,调用其超视图中删除当前按钮,添加新的按钮,在其位置视图的方法。总结起来说一些的UIView动画块,你应该得到你的动画。

Rather than just changing the title and target, etc of the button, have two separate buttons that do each action and look different. Then when you press the button, call a method that removes the current button from its super view and adds the new button to the view in its place. Wrap that up in some UIView animation block and you should get your animation.

另外,对于这个工作,你需要设置按钮的上海华动画过渡。所以它可能是很方便有一个容器视图,因为它可能是然后添加按钮为子视图。

Also, for this to work, you'll need to set the animation transition on the superview of the button. So it might be handy to have a 'container view' as it may be and then add you buttons as subviews of that.

伪code可能看起来像这样:

Pseudo code might look something like so:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:buttonConteinerView cache:NO]; //the built in UIViewAnimationTransition values don't provide enough flexibility

[purchaseButton retain];
[purchaseButton removeFromSuperView];

[buttonContainerView addSubview:secondPurchaseButton];

[UIView commitAnimations];

这篇关于iPhone - 如何动画按钮时pressed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆