3

所以我搜索了很多,但我没有找到我的问题的答案。

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

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

4

5 回答 5

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

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

于 2012-10-17T12:25:27.000 回答
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];
                                                                  } 
                                                              }
                                               ];
                                          }
                          ];
                     }
     ];
}

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

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

- (IBAction) facebookButtonPresse: (id) sender {
    [self.facebookButton blinkAndFlipButtonAnimationWithText: @"+3"];
}
于 2012-10-17T12:17:22.297 回答
2

如果您想要按钮的淡出/淡入动画,我可以用 UIImageview 替换您的自定义按钮,该 UIImageview 具有与初始 uibutton 相同的背景图像。这将是按钮操作中的第一件事。当按钮替换为 uiview cou 可以将淡出动画从初始背景图像应用到 UIImageview 到新的(选定版本)。当动画结束时,在完成块中开始另一个动画,它将所选的 iamge 淡出到正常背景 iamge。然后第二个动画结束删除你的 uiimageview 并添加你的按钮。

//将你的uiimageviews声明为ivars

            UIImageView *imageViewnormal = [[UIimageView alloc] initWithImage:normalIamge];
            UIImageView *imageViewhighlighted = [[UIimageView alloc] initWithImage:highlightImage];

UIControlEventTouchDown按钮的操作中

             // make your button invisible
             yourbutton.alpha = 0;
             //add to same rect 2 imageviews with initial and selected backgrounds of uibutton

            imageViewnormal.alpha = 1;
            imageViewhighlighted.alpha = 0;

           [self.view addSubview:imageViewhighlighted];
           [self.view addSubview:imageViewnormal];

            // newImageViewWithInitialBackground needs to be inserted secondly and alpha value 1
           // newImageViewWithSelectedBackground has alpha 0 and is behind newImageViewWithInitialBackground

        [UIView animateWithDuration:0.3
                         animations:^
         {
             imageViewnormalalpha = 0;
             imageViewhighlighted.alpha = 1;
         }
                         completion:^(BOOL finished)
         {



         }];

在按钮的 UIControlEventTouchUpInside 的操作中

[UIView animateWithDuration:0.3
                              animations:^
              {

             imageViewnormal.alpha = 1;
             imageViewhighlighted.alpha = 0;                  

              }
                              completion:^(BOOL finished)
              {

               //Remove both of the uiimageviews aand make your button visible
             [imageViewnormal removeFromSuperview];
             [mageViewhighlighted removeFromSuperview];
             yourbutton.alpha  = 1;     

              }];
于 2012-10-17T12:05:10.600 回答
2

使用下面的代码,它工作正常,我在我的项目中使用了这个代码:

    UIButton *myButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame=CGRectMake(40, 20, 80, 25);
    [myButton setImage:[UIImage imageNamed:@"normalImage.png"] forState:UIControlStateNormal];
    [myButton setImage:[UIImage imageNamed:@"highlightedImage.png"] forState:UIControlStateHighlighted];
    [myButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myButton];
于 2012-10-17T13:08:39.667 回答
2

xCode 7,iOS9

//for zoom in
    [UIView animateWithDuration:0.5f animations:^{

        self.sendButton.transform = CGAffineTransformMakeScale(1.5, 1.5);
    } completion:^(BOOL finished){}];
// for zoom out
        [UIView animateWithDuration:0.5f animations:^{

            self.sendButton.transform = CGAffineTransformMakeScale(1, 1);
        }completion:^(BOOL finished){}];
于 2016-08-16T10:04:21.817 回答