Animating only the image in UIBarButtonItem

后端 未结 4 675
暖寄归人
暖寄归人 2021-01-03 07:11

Ive seen this effect in 2 apps and I REALLY want to find how to do it.

The animation is in a UIBarButtonItem, and is only to the image. The image is a + symbol, and

4条回答
  •  太阳男子
    2021-01-03 07:21

    So the answer for this is you have to make a instance of the Image view, then set it up with no resizing and view mode is centered. Then add the image view to a UIButton with custom type, and then use the button as the custom view for the bar item.

    - (IBAction)animate {
        [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
            imageView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(45));
        } completion:^(BOOL finished) {
            imageView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(0));
            if ([imageView.image isEqual:[UIImage imageNamed:@"Add.png"]]) {
                imageView.image = [UIImage imageNamed:@"Close.png"];
            }
            else imageView.image = [UIImage imageNamed:@"Add.png"];
        }];
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Add.png"]];
        imageView.autoresizingMask = UIViewAutoresizingNone;
        imageView.contentMode = UIViewContentModeCenter;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 40, 40);
        [button addSubview:imageView];
        [button addTarget:self action:@selector(animate) forControlEvents:UIControlEventTouchUpInside];
        imageView.center = button.center;
        barItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        navItem.rightBarButtonItem = barItem;
    }
    

提交回复
热议问题