【问题标题】:iPhone - How to animate a button when pressed?iPhone - 按下时如何为按钮设置动画?
【发布时间】:2009-06-22 22:54:08
【问题描述】:

有没有办法在点击 iPhone 按钮时制作自定义动画?我想要 App Store 按钮之类的东西 - 它显示价格,然后当您单击它时,它会改变颜色并且文本会变为现在购买,然后当您再次单击它时,它会完成购买。

UIViewAnimationTransition 仅包含一些不提供完整功能的值。是否可以使用动画来做这样的事情:

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

}

此代码有效,将显示正确的过渡,并允许第二次单击以执行正确的操作,但标题和颜色会立即更改,而不是平滑的动画。是否可以轻松地制作这样的动画,我是否必须创建一个 UIButton 子类并“手动”制作动画?如果是这样,我必须重写什么方法?

【问题讨论】:

    标签: iphone animation button


    【解决方案1】:

    不仅仅是更改按钮的标题和目标等,还有两个独立的按钮,它们执行每个操作并且看起来不同。然后,当您按下按钮时,调用一个方法,将当前按钮从其超级视图中移除,并将新按钮添加到视图中的位置。将其包装在一些 UIView 动画块中,您应该会获得动画。

    此外,要使其正常工作,您需要在按钮的父视图上设置动画过渡。因此,拥有一个“容器视图”可能会很方便,然后将按钮添加为它的子视图。

    伪代码可能看起来像这样:

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

    【讨论】:

    • 另外,默认动画过渡是淡入淡出,因此您可能不需要设置动画过渡。
    【解决方案2】:

    一个标准的 UIView 会做你想做的事。 UIView 的 backgroundColor 是一个动画属性。

    我会通过子类化 UIView 来制作自己的“按钮”,或者子类化 UIButton 并向其添加子视图。 (注意:UIButtons 在 2.2 中对此很痛苦,在 3.0 中不确定)

    每个文本状态都需要一个 UILabel:

    UILabel *textForStateOne;
    UILabel *textForStateTwo;
    

    您可以更改父视图的背景颜色。

    那么你在你的新按钮类中创建一个方法:

     -(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
    
      }
    
    
    
    
    }
    

    【讨论】:

      【解决方案3】:
      UIView *btnView4=button;
      [UIView beginAnimations:nil context:nil];
      [UIView setAnimationDuration:0.5];
      [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:btnView4 cache:YES];
      [UIView commitAnimations];``
      

      【讨论】:

      • 请学习如何格式化代码(输入框顶部有一个问号:-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多