如何在导航右侧栏按钮上使用动画?

时间:2015-08-29 09:05:20

标签: objective-c animation ios8 uinavigationbar

我使用以下代码导航右侧栏按钮: -

UIButton *settingsBtn =  [UIButton buttonWithType:UIButtonTypeCustom];
    [settingsBtn setImage:[UIImage imageNamed:@"logo_soft.png"] forState:UIControlStateNormal];
    //[settingsBtn addTarget:self action:@selector(settings) forControlEvents:UIControlEventTouchUpInside];
    [settingsBtn setFrame:CGRectMake(-16, -6, 95, 43)];

    UIView *rightBarButtonItems = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 76, 32)];
    //[rightBarButtonItems addSubview:homeBtn];
    [rightBarButtonItems addSubview:settingsBtn];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButtonItems];

现在我想在相同的导航右侧栏按钮上使用下面的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

 // Animation

    UIImageView*animationView = [[UIImageView alloc] initWithFrame:self.view.frame];
    animationView.animationImages = [NSArray arrayWithObjects:

                                     [UIImage imageNamed:@"1.gif"],
                                     [UIImage imageNamed:@"2.gif"],
                                     [UIImage imageNamed:@"3.gif"],
                                     [UIImage imageNamed:@"4.gif"],
                                     [UIImage imageNamed:@"5.gif"],
                                     [UIImage imageNamed:@"6.gif"],nil];
    animationView.animationDuration = 1.25;
    animationView.animationRepeatCount = 0;
    [animationView startAnimating];
    [self.view addSubview:animationView];
}

如何使用我的导航栏按钮,如下图所示:---

enter image description here

请给我合适的答案或提供示例代码。

谢谢。

1 个答案:

答案 0 :(得分:0)

我尝试过你的代码,我的一个小技巧就像魔法一样。

NSArray *imageArray = [NSArray arrayWithObjects:
                       [UIImage imageNamed:@"tmp-0"],
                       [UIImage imageNamed:@"tmp-1"],
                       [UIImage imageNamed:@"tmp-2"],
                       [UIImage imageNamed:@"tmp-3"],
                       [UIImage imageNamed:@"tmp-4"],
                       [UIImage imageNamed:@"tmp-5"],
                       [UIImage imageNamed:@"tmp-6"],
                       [UIImage imageNamed:@"tmp-7"],nil];

UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barButton setImage:[UIImage imageNamed:@"tmp-0"] forState:UIControlStateNormal]; // mine trick
[barButton.imageView setAnimationImages:imageArray];
[barButton.imageView setAnimationDuration:1.0f];

[barButton.imageView startAnimating];
[barButton sizeToFit];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:barButton];

您首先需要向barButton提供图片,因此您的按钮可以显示其框架

Screenshot of my test