首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在翻转动画中途更改UIImageView图像

在翻转动画中途更改UIImageView图像
EN

Stack Overflow用户
提问于 2013-08-28 18:43:37
回答 3查看 9.4K关注 0票数 6

如何在翻转动画中途更改UIImageView的图像。我的代码似乎确实翻转了UIImageView并成功地更改了图像,但它在瞬间就发生了变化。我想要实现的是,只有当180度翻转达到90度翻转时,才能更改图像。下面是我的代码:

在视图上创建UIImageView:

代码语言:javascript
复制
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动画:

代码语言:javascript
复制
[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上点击手势来做这个动画。

EN

回答 3

Stack Overflow用户

发布于 2013-08-28 18:51:41

您可以轻松地使用UIView类方法+ (void)transitionWithView:duration:options:animations:completion:制作此动画。

在动画中使用如下代码:

代码语言:javascript
复制
[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替换为以下任一选项来设置翻转方向:

代码语言:javascript
复制
UIViewAnimationOptionTransitionFlipFromLeft
UIViewAnimationOptionTransitionFlipFromRight
UIViewAnimationOptionTransitionFlipFromTop
UIViewAnimationOptionTransitionFlipFromBottom
票数 22
EN

Stack Overflow用户

发布于 2020-03-24 07:46:47

对于Swift

代码语言:javascript
复制
UIView.transition(with: imageView,
                  duration: 0.4,
                  options: UIView.AnimationOptions.transitionFlipFromLeft,
                  animations: {
                      imageView.image = newImage
        }, completion: nil)
票数 2
EN

Stack Overflow用户

发布于 2013-08-28 22:35:50

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

代码语言:javascript
复制
[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) {

                                          }];
                     }
                 }];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18485711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档