2012-01-15 190 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