简体   繁体   English

如何使用uibutton动画图像

[英]How To animate image using uibuttons

I have three uibuttons named 1,2 and 3 and right at the bottom of those buttons i have an arrow image which indicates that which is the current button pressed. 我有三个分别名为1,2和3的uibutton,在这些按钮的底部,我有一个箭头图像,该箭头图像指示当前按下的按钮。 I want that when i press any of the button the arrow starts animating and slides at the bottom of the button which is pressed. 我希望当我按下任意按钮时,箭头开始动画并在按下的按钮的底部滑动。

  • (void)setImage:(UIImage *)image forState:(UIControlState)state; (void)setImage:(UIImage *)image forState:(UIControlState)state;

and select UIControlState from 然后从中选择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;

Hook all your buttons up to one method or in the method that responds to each of the button clicks call this method as well. 将所有按钮挂接到一个方法上,或将其响应于每个按钮单击的方法中,也将调用此方法。

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

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

Do the following in your button pressed method: 在按下按钮的方法中执行以下操作:

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

}

PS its always good to also post some source code when you ask a question. PS在您提出问题时也发布一些源代码始终是一件好事。 This helps people to give you a better answer. 这可以帮助人们给您更好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM