按钮按动画

Luk*_*kas 3 iphone objective-c uianimation ipad ios

所以我搜索了很多,但我找不到问题的答案.

我想创建一个具有两个图像的按钮,第一个图像是按钮处于正常状态,另一个是处于突出显示状态时.问题是我希望这些图像以动画方式改变,例如第一个图像淡出,当第二个图像淡入时.它们以动画时间保持不变的方式改变.

我想到的唯一方法是,制作一个自定义按钮,添加几个ImageViews按钮触摸事件,一个ImageView淡入,其他淡出,按钮触摸事件,我可以做相反的事情.但这种方法似乎并不合适.我没有看到更好的选择吗?

小智 6

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(80, 50, 150, 40);
    [myButton setTitle:@"Say, Hi!" forState:UIControlStateNormal];
    [myButton setBackgroundImage:[UIImage imageNamed:@"xyz.png"] forState:UIControlStateNormal];
    [myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myButton];


-(IBAction)buttonClicked:(UIButton *)sender
{
    [UIView animateWithDuration:0.7f
                     animations:^
     {
         [sender setAlpha:0.5f];
     }
                     completion:^(BOOL finished)
     {
         [sender setAlpha:1.0f];
        [sender setBackgroundImage:[UIImage imageNamed:@"abc.png"] forState:UIControlStateNormal];   }
     ];
}
Run Code Online (Sandbox Code Playgroud)

您可以相应地设置动画持续时间和alpha.


Nen*_*d M 5

我已经按照以下方式完成了它,就像Ilker Baltaci描述的那样:我将UIButton子类化(尽管它不建议因为它是一个类集群)并为它添加了一个函数:

- (void) blinkAndFlipButtonAnimationWithText: (NSString*) buttonText {
    [UIView animateWithDuration: 0.2 delay: 0 options: UIViewAnimationOptionCurveLinear 
                     animations: ^{ 
                         self.alpha = 0.1;
                     } 
                     completion: ^(BOOL finished) {
                         [UIView animateWithDuration: 0.2 delay: 0 options: UIViewAnimationOptionCurveLinear 
                                          animations: ^{ 
                                              self.alpha = 1;
                                          } 
                                          completion: ^(BOOL finished) {
                                              [UIView transitionWithView: self duration: 0.25 options: (UIViewAnimationOptionTransitionFlipFromBottom | UIViewAnimationOptionCurveEaseInOut)
                                                              animations: ^{ 
                                                                  [self setTitle: buttonText forState: UIControlStateNormal];
                                                              } 
                                                              completion: ^(BOOL finished) { 
                                                                  if ([self.delegate respondsToSelector: @selector(primaCustomButtonDidFinishFlipAnimation:)]) {
                                                                      [self.delegate primaCustomButtonDidFinishFlipAnimation: self];
                                                                  } 
                                                              }
                                               ];
                                          }
                          ];
                     }
     ];
}
Run Code Online (Sandbox Code Playgroud)

很多块动画级联,丑陋我知道!但无论如何,您可以根据需要调整动画块并排除委托回调.

然后在您的IBAction/target-action方法中按下按钮,只需调用方法,如:

- (IBAction) facebookButtonPresse: (id) sender {
    [self.facebookButton blinkAndFlipButtonAnimationWithText: @"+3"];
}
Run Code Online (Sandbox Code Playgroud)