【问题标题】:How to animate change button title?如何动画更改按钮标题?
【发布时间】:2012-12-13 14:10:20
【问题描述】:

我想动画更改按钮标题。例如,按钮的标题是“one”,点击该按钮后,淡出“one”,然后淡入“NewTitle”。

【问题讨论】:

    标签: ios animation


    【解决方案1】:

    可以使用CATransition 类进行简单的淡入淡出转换,而不是使用重复的视图。

    // CATransition defaults to fade
    CATransition *fade = [CATransition animation];
    // fade.duration = ...
    [button.layer addAnimation:fade];
    
    [button setTitle:@"New title" forControlState:UIControlStateNormal];
    

    按钮将淡出到它的新状态。这适用于标签、整个视图层次结构等。

    【讨论】:

      【解决方案2】:

      所以创建两个按钮不是一个好主意,所以我创建了一个简单的项目来测试你的问题的代码,这就是我想出的

      viewcontroller.h

         #import <UIKit/UIKit.h>
      
          @interface ViewController : UIViewController{
      
              IBOutlet UIButton *myButton;
          }
      
      
          @property(nonatomic,strong) IBOutlet UIButton *myButton;
      
          -(IBAction)animateFadeOutButtonTitle:(id)sender;
          -(void)animateFadeInButtonTitle;
      
      
          @end
      

      viewcontroller.m

      #import "ViewController.h"
      
      @interface ViewController ()
      
      @end
      
      @implementation ViewController
      
      @synthesize myButton=_myButton;
      
      - (void)viewDidLoad
      {
          [super viewDidLoad];
          // Do any additional setup after loading the view, typically from a nib.
          [_myButton setTitle:@"One" forState:UIControlStateNormal];
      
      
      
      }
      
      -(IBAction)animateFadeOutButtonTitle:(id)sender
      {
          [UIView animateWithDuration:0.25 animations:^{_myButton.titleLabel.alpha = 0.0;}];
      
          [self performSelector:@selector(animateFadeInButtonTitle) withObject:self afterDelay:1.0];
      }
      
      -(void)animateFadeInButtonTitle;
      {
      
          [_myButton setTitle:@"New Title" forState:UIControlStateNormal];
          [UIView animateWithDuration:2.0
                                delay:0.0
                              options: UIViewAnimationCurveEaseInOut
                           animations:^{_myButton.titleLabel.alpha = 1.0;}
                           completion:nil];
      
      }
      
      
      - (void)didReceiveMemoryWarning
      {
          [super didReceiveMemoryWarning];
          // Dispose of any resources that can be recreated.
      
      
      
      }
      
      @end
      

      【讨论】:

        猜你喜欢
        • 2015-07-12
        • 1970-01-01
        • 2021-07-11
        • 2016-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-26
        • 2013-09-01
        相关资源
        最近更新 更多