IOS / Objective-C: анимация изменения изображения в UIBarButtonItem - PullRequest
0 голосов
/ 22 мая 2018

Я пытаюсь создать эффект, когда пользовательское изображение в UIBarButtonItem меняется на другое изображение.До сих пор я смог заставить первое изображение раствориться с помощью следующего кода.Кто-нибудь может подсказать, как я мог заставить второе изображение одновременно исчезать?

//Create barbuttonitem in viewwillappear
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
    [b setFrame:CGRectMake(12, 0, 22, 22)];
    [b addTarget:self action:@selector(menuButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [b setImage:[UIImage imageNamed:@"firstimage.png"] forState:UIControlStateNormal];
    UIBarButtonItem * myBarButton = [[UIBarButtonItem alloc] initWithCustomView:b];

//Animate in viewDidAppaer

 [UIView animateWithDuration:1.0
                          delay:1.0
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         self.myBarButton.customView.alpha = 0;
                     }
                     completion:^(BOOL finished) {
UIImage *secondImage = [UIImage imageNamed:@"menu2.png"];
    [self.myBarButton setImage:secondImage];//THis does not change image
                                 self.myBarButton.customView.alpha = 1;//no animation
                             }];

Ответы [ 2 ]

0 голосов
/ 22 мая 2018
Try this code    

    @interface ViewController ()
        {
            UIBarButtonItem * myBarButton;
            NSTimer *time;
            UIButton *b;
        }
        @end

        @implementation ViewController

        - (void)viewDidLoad {
            [super viewDidLoad];
            b = [UIButton buttonWithType:UIButtonTypeCustom];
            [b setFrame:CGRectMake(12, 0, 22, 22)];
            [b addTarget:self action:@selector(menuButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
            [b setImage:[UIImage imageNamed:@"AddPlus_Yellow.png"] forState:UIControlStateNormal];
            myBarButton = [[UIBarButtonItem alloc] initWithCustomView:b];
            self.navigationItem.rightBarButtonItem=myBarButton;
            time = [NSTimer scheduledTimerWithTimeInterval: 2.0 target: self selector:@selector(onTick:) userInfo: nil repeats:NO];
            // Do any additional setup after loading the view, typically from a nib.
        }

        -(void)onTick:(NSTimer *)timer {

            [b setImage:[UIImage imageNamed:@"filter.png"] forState:UIControlStateNormal];
[time invalidate];

        }
0 голосов
/ 22 мая 2018

Вы можете использовать что-то вроде этого

[UIView transitionWithView:self.myBarButton
                      duration:0.3
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{ [self.myBarButton setImage:secondImage];}
                    completion:nil];
...