简体   繁体   English

iPhone-如何在按下按钮时为按钮设置动画?

[英]iPhone - How to animate a button when pressed?

Is there a way to make a custom animation when clicking on an iPhone button? 单击iPhone按钮时,是否可以制作自定义动画? 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. 我想要类似App Store的按钮-它显示价格,然后单击它,它会更改颜色,文本更改为立即购买,然后再次单击它,便完成了购买。

The UIViewAnimationTransition only contains a few values which don't provide the full functionality. UIViewAnimationTransition仅包含一些不能提供全部功能的值。 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];

}

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"? 是否可以轻松制作这样的动画,我是否必须创建UIButton子类并“手动”制作动画? If so, what method would I have to override? 如果是这样,我将不得不覆盖哪种方法?

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. 将其包装在一些UIView动画块中,您应该获得动画。

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. 因此,拥有“容器视图”可能很方便,然后将您的按钮添加为该视图的子视图。

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];

A Standard UIView will do what you want. 标准的UIView可以满足您的需求。 UIView's backgroundColor is an animatable property. UIView的backgroundColor是可设置动画的属性。

I would make own "button" by subclassing UIView OR alternatively subclass a UIButton and add subviews to it. 我将通过子类化UIView来制作自己的“按钮”,或者子类化UIButton并为其添加子视图。 (note: UIButtons were a pain about this in 2.2, not sure in 3.0) (注意:UIButton在2.2中对此感到痛苦,在3.0中不确定)

You need a UILabel for each text state: 每个文本状态都需要一个UILabel:

UILabel *textForStateOne;
UILabel *textForStateTwo;

And you can change the background color of the parent view. 您可以更改父视图的背景颜色。

So then you make a method in your new button class: 因此,您可以在新的按钮类中创建一个方法:

 -(void)toggleState{

myState = !myState;

  if(myState){

      [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationDuration:0.8];

      [textForStateOne setAlpha:1.0]
      [textForStateTwo setAlpha:0.0]

      [self setBackgroundColor:[UIColor greenColor]];

      [UIView commitAnimations];


  } else{

       //do the opposite here

  }




}
UIView *btnView4=button;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:btnView4 cache:YES];
[UIView commitAnimations];``

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按下iPhone时如何切换到下一个视图? - How to switch to next view when button is pressed iPhone? iPhone Dev,UINavigationController,如何以编程方式判断何时按下后退按钮 - iPhone Dev, UINavigationController, How to Programmatically Tell when Back Button is Pressed 当按下UINavigationController上的后退按钮时,如何调用方法? (苹果手机) - How to call a method when back button on a UINavigationController is pressed? (iPhone) 按下时如何更改iPhone上按钮的默认颜色? - How can I change the default color of the button on iPhone when pressed? 按下按钮按下iphone时翻转mapView? - Flip mapView when button Pressed in iphone? 当我按下iPhone上的其他按钮时,更改按钮上的图像 - Change image on button when I pressed other button on iPhone iPhone识别按钮被按下 - iPhone identify button pressed 按下主页按钮后,如何在后台为CABasicAnimation设置动画? - How to animate the CABasicAnimation in background after the home button is pressed? 如何在UINavigationController中按下后退按钮时调用viewDidLoad方法? (iPhone / iPad) - How to call viewDidLoad method when back button is pressed in UINavigationController? (iPhone / iPad) 在屏幕关闭时在iPhone上按下按钮时收到通知 - Getting a notification when a button is pressed on the iPhone when the screen is off
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM