transitionWithView无法从UIImageView交换UIImage

时间:2014-05-15 06:41:18

标签: objective-c uiview uiimageview uiimage objective-c-blocks

根据我在回复this question时所读到的内容,以下内容应该有效:

UIImageView *snapshotView = [[UIImageView alloc]initWithFrame:self.window.frame];

[self.container.view addSubview:snapshotView];


for(int i = 1;i < 5; i = i +1){

        UIImage *snapshotImage = [self blurredInboxBgImageWithRadius: (i * 5)];
        [UIView transitionWithView:snapshotView
                      duration:2.0f
                       options:UIViewAnimationOptionCurveLinear
                    animations:^{
                        snapshotView.image = snapshotImage;
                    } completion:nil];
    }

但它没有。它根本不会为图像更改设置动画。我错过了什么?

1 个答案:

答案 0 :(得分:2)

两件事:

  1. 您需要在上一次转换的完成块中启动下一次转换,以使它们按顺序发生。
  2. 您需要使用UIViewAnimationOptionTransitionCrossDissolve选项,而不是UIViewAnimationOptionCurveLinear
  3. 这里有一些代码我模拟了标签上的一系列转换:

    @interface ViewController ()
    @property (nonatomic) NSInteger iterationCount;
    @property (strong, nonatomic) IBOutlet UILabel *label;
    @end
    
    @implementation ViewController
    
    // called on button tap
    - (IBAction)startTransitioning:(id)sender {
        self.iterationCount = 0;
        [self iterateTransition];
    }
    
    - (void)iterateTransition
    {
        if (self.iterationCount < 5) {
            self.iterationCount++;
            [UIView transitionWithView:self.label duration:2 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
                self.label.text = [NSString stringWithFormat:@"%d", self.iterationCount];
            } completion:^(BOOL finished) {
                [self iterateTransition];
            }];
        } else {
            self.iterationCount = 0;
        }
    }
    
    @end
    
相关问题