【问题标题】:Animating UIButton background image动画 UIButton 背景图像
【发布时间】:2012-01-15 20:11:42
【问题描述】:

有没有办法用一组图像为 UIButton 背景图像视图设置动画?

我已经设法使用 imageView 属性来做到这一点,但找不到等效的背景属性。

10 倍

【问题讨论】:

    标签: iphone objective-c ios ios4 animation


    【解决方案1】:

    这是我所做的(在 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]; 
    }
    

    【讨论】:

    • 这是一个绝妙的解决方案
    【解决方案2】:

    要更改按钮背景图像或文本 - 您需要为特定的 UIControlState 更改它...即突出显示、选定、禁用

    使用

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

    我不确定你是否可以为它们制作动画,我从未尝试过。如果你不能为它们设置动画,那么你可以将 UIImageView 放在一个透明按钮后面,然后为其中的图像设置动画 - 只是一个想法。

    【讨论】:

    • 我在透明按钮后面放了一个 UIImageView。有点 hacky,但我能说什么 - 那行得通! 10x
    【解决方案3】:

    您可以像这样为UIImageView 设置动画的方式为UIButton 设置动画...

    -(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
    

    【讨论】:

      【解决方案4】:

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

      NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:5.00 target:self 选择器:@selector(changeImage) userInfo:nil repeats: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++;
      }
      

      它对我有用。但是添加像翻转等动画需要弄清楚。希望这也是满足您需求的一种方式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-23
        • 2011-01-08
        • 2023-04-04
        • 2010-12-09
        • 2016-03-31
        • 2017-05-11
        • 1970-01-01
        相关资源
        最近更新 更多