Search code examples
ioscore-animationuibarbuttonitem

Refresh animation on UIBarButtonItem


I am trying to make a "refresh" animation using a UIBarButtonItem, the problem is that is not only rotating but moving the image up and down a bit.

enter image description here

-(void)startRefreshButtonRotationAnimation {
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0];
    rotationAnimation.duration = 1;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 1000;
    UIView *view = self.navigationItem.rightBarButtonItem.customView;
    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

- (void)stopRefreshButtonRotationAnimation {
    UIView *view = self.navigationItem.rightBarButtonItem.customView;
    [view.layer removeAllAnimations];
}

Solution

  • Check the image, make sure that the image's center is in the center of the spinner (because it looks like it most likely isn't). Go into your favourite image editing software and center it, making sure that when you export, the transparent areas aren't clipped off.