将UIImageView图像更改为翻转动画的一半

Lit*_*Dev 4 animation objective-c uiview cgaffinetransform ios

如何在翻转动画的一半时更改UIImageView的图像.我的代码似乎确实翻转了UIImageView并成功更改了图像,但它瞬间发生了变化.我想要实现的是仅在180度翻转达到90度翻转时才改变图像.这是我的代码:

在视图上创建UIImageView:

UIImage *image = [UIImage imageNamed:@"fb1.jpg"];
    imageView = [[UIImageView alloc] initWithImage:image];
    vw = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)];
    vw.layer.cornerRadius = vw.frame.size.width / 2;
    imageView.frame = CGRectMake(20, 20, 80, 80);
    imageView.layer.cornerRadius = imageView.frame.size.width / 2;
    imageView.layer.masksToBounds = YES;
    imageView.layer.borderColor = [[UIColor blackColor] CGColor];
    imageView.layer.borderWidth = 1.0;
    [self.view addSubview: imageView];
    //[self.view addSubview:vw];
    [imageView setUserInteractionEnabled:YES];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(taptap:)];
    [imageView addGestureRecognizer:tap];
Run Code Online (Sandbox Code Playgroud)

在点击动画UIImageView:

[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
                     animations:^(void) {
                         imageView.transform = CGAffineTransformMakeScale(-1, 1);
                         [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionTransitionCrossDissolve
                                          animations:^(void) {
                                              imageView.image = [UIImage imageNamed:@"fb2.jpg"];
                                          } completion:^(BOOL finished) {

                                          }];
                     }
                     completion:^(BOOL finished) {
                     }];
Run Code Online (Sandbox Code Playgroud)

此外,我正在特定UIImageView上的轻击手势上执行此动画.

Luk*_*cka 20

您可以轻松地使用UIView类方法+ (void)transitionWithView:duration:options:animations:completion:来实现此动画.

像这样的代码用于动画:

[UIView transitionWithView:imageView
                  duration:0.4
                   options:UIViewAnimationOptionTransitionFlipFromRight
                animations:^{
                    //  Set the new image
                    //  Since its done in animation block, the change will be animated
                    imageView.image = newImage;
                } completion:^(BOOL finished) {
                    //  Do whatever when the animation is finished
                }];
Run Code Online (Sandbox Code Playgroud)

您可以通过替换UIViewAnimationOptionTransitionFlipFromRight以下任何选项来设置翻转方向:

UIViewAnimationOptionTransitionFlipFromLeft
UIViewAnimationOptionTransitionFlipFromRight
UIViewAnimationOptionTransitionFlipFromTop
UIViewAnimationOptionTransitionFlipFromBottom
Run Code Online (Sandbox Code Playgroud)