Ios UIView图像-从一个图像淡入另一个图像
我正在尝试使用单个UIView从一个图像淡入另一个图像(是的!已经在stackoverflow上看到了Q&a,它使用两个UIView-彼此重叠) 我有一个当前的UIView图像,希望淡出并替换为另一个图像,然后淡入新图像。 我试过以下几点:第一个的褪色,独自一人,有效 尝试#1) 这会在新的图像中消失。。。旧图像不会褪色。还尝试在两组代码之间“暂停” 尝试#2) 这会立即显示新图像,不会褪色或“交叉溶解” 尝试#3) 这与#2的结果相同Ios UIView图像-从一个图像淡入另一个图像,ios,objective-c,Ios,Objective C,我正在尝试使用单个UIView从一个图像淡入另一个图像(是的!已经在stackoverflow上看到了Q&a,它使用两个UIView-彼此重叠) 我有一个当前的UIView图像,希望淡出并替换为另一个图像,然后淡入新图像。 我试过以下几点:第一个的褪色,独自一人,有效 尝试#1) 这会在新的图像中消失。。。旧图像不会褪色。还尝试在两组代码之间“暂停” 尝试#2) 这会立即显示新图像,不会褪色或“交叉溶解” 尝试#3) 这与#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];
}];
}];
试试这个。它淡出图像,交换没有动画的图像,然后淡入另一个动画
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];
}];
}];
通过设置图像视图的.image
属性,您无法从一个UIImage“交叉淡入”到另一个UIImage
不过,您可以使用“中间”图像视图来执行此操作
假设您有一个通过IBOutlet
连接的UIImageView
,以及一个通过iAction连接的按钮:
//
// 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
通过设置图像视图的.image
属性,您无法从一个UIImage“交叉淡入”到另一个UIImage
不过,您可以使用“中间”图像视图来执行此操作
假设您有一个通过IBOutlet
连接的UIImageView
,以及一个通过iAction连接的按钮:
//
// 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
//
// 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