我希望为视图构建一个漂亮的背景,图像淡出&但是,我也希望动画不要停止。
我已经在使用它了:
backgroundUIImageView.animationImages =
[UIImage(named:"testimage1"), UIImage(named:"testimage2")]
backgroundUIImageView.animationDuration = 10
backgroundUIImageView.animationRepeatCount = 0
backgroundUIImageView.startAnimating()
问题:转换没有褪色
我也试过这个:
var UIImage1=UIImage(named:"testimage1")
var UIImage2=UIImage(named:"testimage2")
var crossFade = CABasicAnimation(keyPath:"contents")
crossFade.duration = 5.0
crossFade.fromValue = UIImage1.CGImage
crossFade.toValue = UIImage2.CGImage
self.backgroundUIImageView.layer.addAnimation(crossFade, forKey:"animateContents")
self.backgroundUIImageView.image = UIImage2
问题:动画没有重复,代码真的很难看。在我看来不安全..
有人会有关于如何做到这一点的任何想法/代码示例吗?
答案 0 :(得分:3)
我不知道如何在SWIFT中编写它,但我使用此代码段。
要重复,您应该添加选项UIViewAnimationOptionAutoreverse
和UIViewAnimationOptionRepeat
UIImage * toImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
[UIView transitionWithView:self.imageView
duration:0.8f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.imageView.image = toImage;
} completion:nil];
答案 1 :(得分:1)
我只需要自己做这件事。我使用了NSTimer和CA crossfade的组合。每次你的计时器开火,你都会得到一个很好的淡入淡出过渡。调整动画的长度和计时器间隔以设置动画的速度。
func startAnimatingLoadingImage() {
self.timer = NSTimer.scheduledTimerWithTimeInterval(0.3, target:self, selector: Selector("flipImage"), userInfo: nil, repeats: true)
}
和这个
func flipImage() {
let cell1 = UIImage(named: "dots1")!
let cell2 = UIImage(named: "dots2")!
let cell3 = UIImage(named: "dots3")!
let cell4 = UIImage(named: "dots4")!
let cells = [cell1, cell2, cell3, cell4]
let numCells = cells.count - 1
let currentImage = cells[currentCell]
var nextImage: UIImage
if currentCell == numCells {
nextImage = cells[0]
self.currentCell = 0
} else {
let x = currentCell + 1
nextImage = cells[x]
self.currentCell = x
}
let crossFade = CABasicAnimation(keyPath:"contents")
crossFade.duration = 0.25
crossFade.fromValue = currentImage.CGImage
crossFade.toValue = nextImage.CGImage
self.loadingView.layer.addAnimation(crossFade, forKey:"animateContents")
self.loadingView.image = nextImage
}