animationUIButton背景图像

有没有办法使用一组图像animationUIButton背景图像视图?

我设法做到了imageView属性,但无法find一个等效的背景属性。

10倍

要改变一个button的背景图像,或文本的事情 – 你需要改变它的特定的UIControlState …即突出,选定,禁用

使用

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

我不确定你是否可以制作animation,我从来没有尝试过。 如果你不能animation它们,那么你可以把一个UIImageView放在一个透明的button后面,然后在这个图像上设置animation效果 – 只是一个想法。

这是我做的(在UIButton子类中):

设置button图像,像常规:

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

你可以animation一个UIButton的animation方式像这样的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 

对的,这是可能的。 使用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++; } 

它为我工作。 但添加像翻转等animation需要弄清楚。 希望这也是一种方法来帮助你的需要。