2012-10-17 205 views
2

所以我搜索了很多,但我沒有找到我的問題的答案。按鈕按動畫

我想做一個按鈕,它會有兩個圖像,第一個圖像是當按鈕處於正常狀態時,另一個是處於高亮狀態時。問題是,我希望這些圖像以動畫方式進行更改,例如,當第二張圖像淡入時,第一張圖像會淡出。而且它們會以動畫時間保持不變的方式進行更改。

我認爲唯一的辦法,使自定義按鈕,添加ImageViews夫婦和按鈕向下觸摸事件,一個ImageView淡入,其他淡出,按鈕觸摸完成事件,我可以做相反的事情。但是這種方法似乎並不適合。有沒有更好的選擇,我沒有看到?

+0

你的意思是你想改變選擇或取消選擇按鈕的圖像布頓...? –

+0

只是綁定您的圖像的動畫事件在您的按鈕的觸摸和觸及事件... – HarshIT

+0

我有一個按鈕。按下時,我想讓它的圖像更改爲其他指定圖像的動畫。在touchup我想讓它再次成爲原始圖像的動畫。 – Lukas

回答

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爲/目標 - 動作方法只是調用該方法,如:

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

如果你想淡出/淡入淡出按鈕的動畫我可以取代你自定義按鈕與UIImageView有相同的背景圖像作爲你最初的UIBUTTON.This將是第一件事,以在行動中的按鈕。當按鈕被替換爲uiview時,可以將淡出動畫應用於UIImageview,從最初的backgorund圖像到新的(選定的版本)。當動畫結束時,在完成塊中開始另一個動畫,將選定的圖像淡出爲正常背景圖像。然後第二個動畫結束刪除你的圖像並添加按鈕。

//聲明你的UIImageViews作爲高德

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

在操作您的按鈕

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



     }]; 

UIControlEventTouchDown在行動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;  

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

可以設置相應的動畫和持續時間也阿爾法。

+0

的UIControlEventTouchUp動作中,但我認爲這不會按照我想要的方式工作。如果用戶觸摸按鈕並在動畫未完成時釋放按鈕,則用戶將看到輕彈。所以我需要動畫順利。 – Lukas

+0

是的,你是對的,這就是我要求你改變α的原因,也許你可以看到不同之處。 –

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]; 
+0

是的,它的工作..... !!!!!!偉大的工作..... thx的傢伙.... –

+0

歡迎.... –

+1

這是工作,但不是我想要的方式。它只是正常的方式沒有動畫.. – Lukas

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

謝謝你的回答爲我工作。 – Ramakrishna

+0

謝謝@Ramakrishna,投給我PLZ。 –