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

时间:2013-08-28 10:43:37

标签: ios objective-c animation uiview cgaffinetransform

如何在翻转动画的一半时更改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];

点击动画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) {
                     }];

另外,我正在特定UIImageView上的点击手势上做这个动画。

3 个答案:

答案 0 :(得分: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
                }];

您可以使用以下任一选项替换UIViewAnimationOptionTransitionFlipFromRight来设置翻转方向:

UIViewAnimationOptionTransitionFlipFromLeft
UIViewAnimationOptionTransitionFlipFromRight
UIViewAnimationOptionTransitionFlipFromTop
UIViewAnimationOptionTransitionFlipFromBottom

答案 1 :(得分:0)

尝试两步动画,如果你不喜欢这样,你可以查找连接选项的文档。

[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn
                 animations:^(void) {
                     //do your half rotation here
                     imageView.transform = CGAffineTransformMakeScale(0, 1);
                 }
                 completion:^(BOOL finished) {
                     if(finished){
                         imageView.image = [UIImage imageNamed:@"fb2.jpg"];
                         [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut
                                          animations:^(void) {
                                              //do your second half rotation here
                                              imageView.transform = CGAffineTransformMakeScale(-1, 1);
                                          } completion:^(BOOL finished) {

                                          }];
                     }
                 }];

答案 2 :(得分:0)

对于Swift

UIView.transition(with: imageView,
                  duration: 0.4,
                  options: UIView.AnimationOptions.transitionFlipFromLeft,
                  animations: {
                      imageView.image = newImage
        }, completion: nil)