如何使用uibutton为图像添加动画效果

时间:2012-03-07 15:24:28

标签: iphone objective-c xcode animation sdk

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

将所有按钮挂钩到一个方法或按响应每个按钮点击的方法调用此方法。

- (IBAction)buttonTapped:(UIButton *)sender;
{
    CGPoint center = self.indicator.center;
    center.x = sender.center.x;

    [UIView animateWithDuration:0.25f
                     animations:^{
                         self.indicator.center = center;
                     }];
}

答案 2 :(得分:0)

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

// 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];

}

P.S。当你提出问题时,它总是很好地发布一些源代码。这有助于人们为您提供更好的答案。