Fade/dissolve when changing UIImageView's image
Edit: there is a better solution from @algal below.
Another way to do this is by using predefined CAAnimation transitions:
CATransition *transition = [CATransition animation];
transition.duration = 0.25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;
transition.delegate = self;
[self.view.layer addAnimation:transition forKey:nil];
view1.hidden = YES;
view2.hidden = NO;
See the View Transitions example project from Apple: https://developer.apple.com/library/content/samplecode/ViewTransitions/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007411
Fade/dissolve when changing UIImageView's image with percentage
This Extension works for me:
extension UIImage {
static func mergeImage(imageA: UIImage, imageB:UIImage, percentage: CGFloat) -> UIImage{
UIGraphicsBeginImageContextWithOptions(imageA.size, false, 0.0)
imageA.draw(at: CGPoint(x: 0, y: 0), blendMode: .normal, alpha: 1-percentage)
imageB.draw(at: CGPoint(x: 0, y: 0), blendMode: .normal, alpha: percentage)
let mergedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return mergedImage!;
}
}
White screen when attempting to fade between images in UIImageView
AutoLayout not calculated yet when in viewDidLoad
You can set frame when layout changed in viewDidLayoutSubviews.
Try this
class ViewController: UIViewController {
var bckgImageView = UIImageView(image: UIImage(named: "one"))
override func viewDidLoad() {
super.viewDidLoad()
bckgImageView.frame = self.view.frame
self.view.addSubview(bckgImageView)
animateBackground()
}
func animateBackground() {
UIView.transition(with: self.view,
duration: 5,
options: .transitionCrossDissolve,
animations: { self.bckgImageView.image = UIImage(named: "two") },
completion: nil)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
bckgImageView.frame = self.view.frame
}
}
Fade in/out Image in UIIamgeView happens with no duration
Try to start the animation in the viewDidAppear
delegate. Think thats the problem.
Fade In UIImageView
The image needs to be visible all the time while you're fading it in. You are animating the alpha property on a hidden image, and then showing it at the end. Correct would be:
Image.hidden = NO;
[UIView animateWithDuration:0.5
delay:1.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{ Image.alpha = 1; }
completion:^(BOOL finished){}
];
How to animate the change of image in an UIImageView?
I am not sure if you can animate UIViews with fade effect as it seems all supported view transitions are defined in UIViewAnimationTransition
enumeration. Fading effect can be achieved using CoreAnimation. Sample example for this approach:
#import
...
imageView.image = [UIImage imageNamed:(i % 2) ? @"3.jpg" : @"4.jpg"];
CATransition *transition = [CATransition animation];
transition.duration = 1.0f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;
[imageView.layer addAnimation:transition forKey:nil];
UIImageView change picture with fade effect
You can do this by using UIView
's + transitionWithView:duration:options:animations:completion: method.
[UIView transitionWithView:yourImageView duration:7.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
yourImageView.image = newImage;
} completion:nil];
Related Topics
How to Hide the Status Bar in a Swift iOS App
How to Create Radio Buttons and Checkbox in Swift (Ios)
How to Make App Fully Working Correctly for Autorotation in iOS 6
How to Save Local Data in a Swift App
Xcode 9 Gm - Wkwebview Nscoding Support Was Broken in Previous Versions
How Add Separator to String at Every N Characters in Swift
Com.Apple.Webkit.Webcontent Drops 113 Error: Could Not Find Specified Service
Swift: Issue in Converting String to Double
Xcode, Where to Assign the Segue Identifier
How to Increase the Height of Navigation Bar in Xcode
Uisegmentedcontrol Below Uinavigationbar in iOS 7
Uitableview with Fixed Section Headers
How to Detect iPhone Is on Silent Mode