在更改图像时翻转UIButton?

时间:2015-01-14 10:36:27

标签: ios objective-c uibutton

我需要更改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];

我做错了什么?

2 个答案:

答案 0 :(得分:0)

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

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]

答案 1 :(得分:0)

解决了这个问题:

[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];