在更改图像时翻转 UIButton?

Flipping UIButton while changing image?

我需要更改一个 UIButton 的图像,用户点击按钮时,它会被翻转,然后更改为一个新图像,这个不会翻转,而只会改变图像。

CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
  crossFade.duration = 1.0;
  crossFade.fromValue = (id)cell.imageView.image.CGImage;
  crossFade.toValue = (id)Cellimg.CGImage;
  crossFade.removedOnCompletion = NO;
  crossFade.fillMode = kCAFillModeForwards;
  [cell.imageView.layer addAnimation:crossFade forKey:@"rotationY"];

  [cell setImage:Cellimg forState:UIControlStateNormal];

我做错了什么?

为什么不创建带有图像视图的自定义按钮?像这样-

UIImage *img = [UIImage imageNamed:@"path/to-image.png"];

UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.imageView.transform = CGAffineTransformMakeRotation(M_PI); // flip the image view
[button setImage:img forState:UIControlStateNormal]

用这个解决了:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 0.50f];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:cell cache:NO];

// [baseBtn setTitle:newTitle forState:UIControlStateNormal];
[cell setImage:Cellimg forState:UIControlStateNormal];

[UIView commitAnimations];