2012-01-15 191 views
7

有沒有辦法使用一組圖像爲UIButton背景圖像視圖設置動畫效果?動畫UIButton背景圖像

我已經成功與ImageView的物業這樣做,也沒有找到一個等效背景屬性。

10X

回答

1

要更改按鈕底色圖像或文本對於這個問題 - 你需要改變它的特定UIControlState ...即突出顯示,選擇,殘疾人

使用

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state

我不知道你是否能它們的動畫,我從來沒有嘗試過。如果你不能爲它們設置動畫,那麼你可以把UIImageView放在一個透明的按鈕後面,然後將圖像製作成動畫 - 只是一個想法。

+0

我已經把一個UIImageView後面的透明按鈕。有點不好意思,但我可以說什麼 - 這工作! 10x – Rizon 2012-01-17 15:47:48

0

是的,這是可能的。使用NSTimer,

NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:5.00 target:self selector:@selector(changeImage)userInfo:nil repeatats:YES];

然後,方法如下,

- (void)changeImage 
{ 

      static int counter = 0; 

      if([bannerArray count] == counter) 
      { 
        counter = 0; 
      } 
      [button setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:bannerArray[counter]]]] forState:UIControlStateNormal]; 

      counter++; 
} 

它爲我工作。但添加像翻轉等動畫需要弄清楚。希望這也是一種方法來幫助你的需要。

1

您可以動畫的UIButton你的動畫UIImageView這樣的方式......

-(void) addAnimationToButton:(UIButton*) button{ 
    UIImageView* animationView = [button imageView]; 
    NSArray* animations=[NSArray arrayWithObjects: 
         [UIImage imageNamed:@"sprite1.png"], 
         [UIImage imageNamed:@"sprite2.png"], 
         [UIImage imageNamed:@"sprite2.png"], 
         //and so on if you need more 
         nil]; 
    CGFloat animationDuration = 2.0; 
    [animationView setAnimationImages:animations]; 
    [animationView setAnimationDuration:animationDuration]; 
    [animationView setAnimationRepeatCount:0]; //0 is infinite 
} 

並且你使用這樣的:

[self addAnimationToButton:yourButton]; 
[[yourButton imageView] startAnimating]; //or stopAnimating 
11

這裏就是我所做的(一個UIButton子類中, ):

設置按鈕圖像,像常規:

[self setBackgroundImage:[[UIImage imageNamed:@"button"] resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)] forState:UIControlStateNormal]; 
[self setBackgroundImage:[[UIImage imageNamed:@"button_pressed"] resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)] forState:UIControlStateHighlighted]; 

改寫強調:

- (void)setHighlighted:(BOOL)highlighted { 
    [UIView transitionWithView:self duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowAnimatedContent animations:^{ 
     [super setHighlighted:highlighted]; 
    } completion:nil]; 
} 
+0

這是一個絕妙的解決方案 – 2015-08-13 00:03:05