动画UIButton背景图像
要更改按钮底色图像或文本对于这个问题 - 你需要改变它的特定UIControlState ...即突出显示,选择,残疾人
使用
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
我不知道你是否能它们的动画,我从来没有尝试过。如果你不能为它们设置动画,那么你可以把UIImageView放在一个透明的按钮后面,然后将图像制作成动画 - 只是一个想法。
是的,这是可能的。使用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++;
}
它为我工作。但添加像翻转等动画需要弄清楚。希望这也是一种方法来帮助你的需要。
您可以动画的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
这里就是我所做的(一个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];
}
这是一个绝妙的解决方案 – 2015-08-13 00:03:05
我已经把一个UIImageView后面的透明按钮。有点不好意思,但我可以说什么 - 这工作! 10x – Rizon 2012-01-17 15:47:48