【问题标题】:How To animate image using uibuttons如何使用 uibuttons 为图像设置动画
【发布时间】:2012-03-07 15:24:27
【问题描述】:

我有三个名为 1,2 和 3 的 uibuttons,在这些按钮的底部我有一个箭头图像,指示当前按下的按钮。 我希望当我按下任何按钮时,箭头开始动画并在按下的按钮底部滑动。

【问题讨论】:

  • 到目前为止你写了什么代码?

标签: iphone objective-c xcode animation sdk


【解决方案1】:
  • (void)setImage:(UIImage *)image forState:(UIControlState)state;

并从中选择 UIControlState

enum {
UIControlStateNormal       = 0,                       
UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set
UIControlStateDisabled     = 1 << 1,
UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)
UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use
UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use
};
typedef NSUInteger UIControlState;

【讨论】:

    【解决方案2】:

    将所有按钮与一个方法挂钩,或者在响应每个按钮单击的方法中也调用此方法。

    - (IBAction)buttonTapped:(UIButton *)sender;
    {
        CGPoint center = self.indicator.center;
        center.x = sender.center.x;
    
        [UIView animateWithDuration:0.25f
                         animations:^{
                             self.indicator.center = center;
                         }];
    }
    

    【讨论】:

      【解决方案3】:

      在您的按钮按下方法中执行以下操作:

      // For Button 1
      - (IBAction)pressedButton1:(id)sender{
      
              // Create an animation lasting 1 second
              [UIView beginAnimations:nil context:nil];
              [UIView setAnimationDuration:1.00];
              [UIView setAnimationDelegate:self];
      
              // Set your arrow image view's center x property to that of the button
              myArrowImageView.center = CGPointMake(myButton1.center.x, myArrowImageView.center.y);
      
              [UIView commitAnimations];
      
      }
      
      // For Button 2
      - (IBAction)pressedButton2:(id)sender{
      
          // Create an animation lasting 1 second
          [UIView beginAnimations:nil context:nil];
          [UIView setAnimationDuration:1.00];
          [UIView setAnimationDelegate:self];
      
          // Set your arrow image view's center x property to that of the 2nd button
          myArrowImageView.center = CGPointMake(myButton2.center.x, myArrowImageView.center.y);
      
          [UIView commitAnimations];
      
      }
      
      // For Button 3
      - (IBAction)pressedButton3:(id)sender{
      
          // Create an animation lasting 1 second
          [UIView beginAnimations:nil context:nil];
          [UIView setAnimationDuration:1.00];
          [UIView setAnimationDelegate:self];
      
          // Set your arrow image view's center x property to that of the 3rd button
          myArrowImageView.center = CGPointMake(myButton3.center.x, myArrowImageView.center.y);
      
          [UIView commitAnimations];
      
      }
      

      附:当您提出问题时,也发布一些源代码总是好的。这有助于人们为您提供更好的答案。

      【讨论】:

      • 你不能像这样分配center.x,你会得到一个Expression is not assignable warning
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多