UIView图像 - 从一个图像淡入另一个图像

时间:2018-03-07 16:53:10

标签: ios objective-c

我尝试使用 SINGLE UIView尝试从一张图片淡入另一张图片(是!已经在堆栈溢出中看到Q& As,使用了2个UIViews - 在彼此之上)。

我有一个当前的UIView图像,想淡出它并用另一个图像替换它,并淡化新图像。

我尝试过以下方法:第一个人的褪色,单独行动。

尝试#1)

  // Fade original image - image already set in Storyboard
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:5.0];
    [UIView setAnimationDelegate:self];
    [_imgMirror setAlpha:0.0];
    [UIView commitAnimations];


    // Fade in new image
    [_imgMirror setImage:[UIImage imageNamed:@"NewImage"]];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:5.0];
    [_imgMirror setAlpha:100.0];
    [UIView commitAnimations];

新图像渐渐消失......旧图像不会褪色。还尝试了暂停'在两组代码之间。

尝试#2)

UIImage * toImage = [UIImage imageNamed:@"NewImage"];
[UIView transitionWithView:self.imgMirror
                  duration:0.33f
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                    self.imgMirror.image = toImage;
                } completion:NULL];

这会立即显示新图像,永不淡化或“解决问题”。

尝试#3)

  // Fade original image - image already set in Storyboard
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:5.0];
        [UIView setAnimationDelegate:self];
        [_imgMirror setAlpha:0.0];
        [UIView commitAnimations];



UIImage * toImage = [UIImage imageNamed:@"NewImage"];
    [UIView transitionWithView:self.imgMirror
                      duration:0.33f
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        self.imgMirror.image = toImage;
                    } completion:NULL];

这与#2的结果相同。

知道出了什么问题吗?

2 个答案:

答案 0 :(得分:2)

试试这个。它将图像淡出,在没有动画的情况下交换图像,然后用另一个动画淡入。

CGFloat fadeDuration = 0.5f;
[UIView animateWithDuration:fadeDuration animations:^{
    [_imgMirror setAlpha:0.0];
} completion:^(BOOL finished) {
    UIImage * toImage = [UIImage imageNamed:@"NewImage"];
    _imgMirror.image = toImage;
    [UIView animateWithDuration:fadeDuration animations:^{
        [_imgMirror setAlpha:1.0];
    }];
}];

答案 1 :(得分:1)

你不能"交叉褪色"通过设置图像视图的.image属性,从一个UIImage到另一个UIImage。

可以使用"中介"虽然图像视图。

假设您通过UIImageView连接了IBOutlet,并通过IBAction连接了按钮:

//
//  CrossFadeViewController.m
//
//  Created by Don Mag on 3/7/18.
//

#import "CrossFadeViewController.h"

@interface CrossFadeViewController ()

@property (strong, nonatomic) IBOutlet UIImageView *imgMirror;

@end

@implementation CrossFadeViewController

- (IBAction)didTap:(id)sender {

    [self crossFadeTo:@"NewImage"];

}

- (void) crossFadeTo:(NSString *)newImageName {

    UIImageView *toImageView;

    // create a new UIImageView with the new Image
    toImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:newImageName]];

    // set the new image view's frame to match the existing one
    toImageView.frame = _imgMirror.frame;

    // cross-fade / dissolve from the existing image view to the new one
    [UIView transitionFromView:_imgMirror
                        toView:toImageView
                      duration:0.33
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    completion:^(BOOL finished) {
                        // when animation is finished
                        // remove the "old" image view from its superview
                        [_imgMirror removeFromSuperview];
                        // assign the new image view to the IBOutlet property
                        _imgMirror = toImageView;
                    }];

}

@end