Iphone 按键动画

Iphone 按键动画,iphone,objective-c,ios,ipad,uianimation,Iphone,Objective C,Ios,Ipad,Uianimation,所以我搜索了很多,但是我没有找到我问题的答案 我想制作一个有两个图像的按钮,第一个图像是当按钮处于正常状态时,另一个是当它处于高亮状态时。问题是,我希望这些图像以动画方式更改,例如,当第二个图像淡入时,第一个图像淡出。它们的变化方式使动画时间保持不变 我认为唯一的方法是,要制作一个自定义按钮,添加两个ImageView和on button touch down事件,一个ImageView淡入,另一个淡出,on button touch up事件,我可以做相反的事情。但这种方法似乎不是最合适的。有

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

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


我认为唯一的方法是,要制作一个自定义按钮,添加两个
ImageView
和on button touch down事件,一个
ImageView
淡入,另一个淡出,on button touch up事件,我可以做相反的事情。但这种方法似乎不是最合适的。有没有更好的选项我看不到?

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

//将UIImageView声明为IVAR

            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)
         {



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

              }];
在按钮内部的uicontrolEventTouchUp操作中

             // 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)
         {



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

              }];

我是这样做的,正如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];
                                                                  } 
                                                              }
                                               ];
                                          }
                          ];
                     }
     ];
}
很多块动画级联,丑陋我知道! 但无论如何,您可以根据需要调整动画块并排除代理回调

然后在按钮的iAction/target action方法中,只需调用该方法,如:

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

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

使用以下代码,它工作正常,我在项目中使用了此代码:

    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];
xCode 7,IO09

//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){}];

你的意思是你想改变按钮被选中或被取消选中的图像吗?只需将你的图像的动画事件绑定到你的按钮的触动和触动事件上…我有一个按钮。按下时,我希望其图像将动画更改为其他指定图像。在触摸时,我希望它再次设置为原始图像的动画。在这一点上,您不需要第二个动画块,您必须将其设置为按钮的uicontrol事件触摸操作,但我认为这将不是我想要的方式。如果用户按下按钮,并在动画未完成时释放按钮,则用户将看到该动画。所以我需要动画平滑。是的,你是对的,这就是我要求你改变alpha的原因,也许你能看到区别。它正在工作,但不是我想要的方式。这只是没有动画的正常方式。。