Fade/dissolve when changing UIImageView's imag

2019-01-03 04:19发布

Rather than creating two UIImageViews, it seems logical to simply change the image of one view. If I do that, is there anyway of having a fade/cross dissolve between the two images rather than an instant switch?

8条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-03 04:48

It can be much simpler using the new block-based, UIKit animation methods.

Suppose the following code is in the view controller, and the UIImageView you want to cross-dissolve is a subview of self.view addressable via the property self.imageView. Then all you need is:

UIImage * toImage = [UIImage imageNamed:@"myname.png"];
[UIView transitionWithView:self.imageView
                  duration:5.0f
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                  self.imageView.image = toImage;
                } completion:nil];

Done.

And to do it in Swift, it's like so:

let toImage = UIImage(named:"myname.png")
UIView.transitionWithView(self.imageView,
                          duration:5,
                          options: UIViewAnimationOptions.TransitionCrossDissolve,
                          animations: { self.imageView.image = toImage },
                          completion: nil)

Swift 3.0

 let toImage = UIImage(named:"myname.png")
 UIView.transition(with: self.imageView,
                   duration: 0.3,
                   options: .transitionCrossDissolve,
                   animations: {
                       self.imageView.image = toImage
                   },
                   completion: nil)
查看更多
Explosion°爆炸
3楼-- · 2019-01-03 04:48

For Swift 3.0.1 :

UIView.transition(with: self.imageView,
              duration:0.5,
              options: .transitionCrossDissolve,
              animations: { self.imageView.image = newImage },
              completion: nil)

Reference: https://gist.github.com/licvido/bc22343cacfa3a8ccf88

查看更多
登录 后发表回答