与创建两个UIImageViews
相比,简单地更改一个视图的image
似乎是合乎逻辑的。如果我这样做了,是否存在在两个图像之间进行淡出/交叉融合而不是即时切换?
发布于 2017-03-10 12:03:39
对于Swift 3.0.1:
UIView.transition(with: self.imageView,
duration:0.5,
options: .transitionCrossDissolve,
animations: { self.imageView.image = newImage },
completion: nil)
发布于 2011-10-04 02:12:02
是的,你所说的是绝对正确的,这就是你所做的。我写了这个方法&总是用它来淡出我的图像。为此,我与CALayer
打交道。为此,您需要导入核心动画。
+ (void)fadeInLayer:(CALayer *)l
{
CABasicAnimation *fadeInAnimate = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeInAnimate.duration = 0.5;
fadeInAnimate.repeatCount = 1;
fadeInAnimate.autoreverses = NO;
fadeInAnimate.fromValue = [NSNumber numberWithFloat:0.0];
fadeInAnimate.toValue = [NSNumber numberWithFloat:1.0];
fadeInAnimate.removedOnCompletion = YES;
[l addAnimation:fadeInAnimate forKey:@"animateOpacity"];
return;
}
您可以对淡出图像执行相反的操作。在它淡出之后。只需将其从superview (即UIImageView
)中删除即可。[imageView removeFromSuperview]
。
发布于 2013-03-11 04:44:46
我需要无限期地重复这个过渡。在这个过程中,我进行了大量的尝试和错误,但我最终得到了我想要的最终结果。这些是用于向UITableViewCell中的UIImageView添加图像动画的代码片段。
相关代码如下:
@interface SomeViewController ()
@property(nonatomic, strong) NSMutableArray *imagesArray;
@property(nonatomic, assign) NSInteger varietyImageAnimationIndex;
@property(nonatomic, assign) BOOL varietyImagesAnimated;
@end
@implementation SomeViewController
@synthesize imagesArray;
@synthesize varietyImageAnimationIndex;
@synthesize varietyImagesAnimated;
...
// NOTE: Initialize the array of images in perhaps viewDidLoad method.
-(void)animateImages
{
varietyImageAnimationIndex++;
[UIView transitionWithView:varietyImageView
duration:2.0f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
varietyImageView.image = [imagesArray objectAtIndex:varietyImageAnimationIndex % [imagesArray count]];
} completion:^(BOOL finished) {
[self animateImages];
}];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[cell.imageView setImage:[imagesArray objectAtIndex:0]];
[self setVarietyImageView:cell.imageView];
if (! varietyImagesAnimated)
{
varietyImagesAnimated = YES;
[self animateImages];
}
...
return cell;
}
https://stackoverflow.com/questions/7638831
复制相似问题