I am trying to fade one view in while fading the other out using animateWithDuration and changing the alpha values of each. The problem I'm having is that the views seem to fade through a black background. I would like to keep the transitioning background a white color being that both of my views have white backgrounds.
How can I fade between my two views without transitioning through a black background?
Here's my code:
[self.view1 setAlpha:0.0];
[self.view addSubview:self.view1];
[UIView animateWithDuration:0.5
animations:^{self.view1.alpha = 1.0;self.view2.alpha = 0.0;}
completion:^(BOOL finished){[self.view2 removeFromSuperview];}];
How about something like this:
[UIView transitionWithView:self.view
duration:0.4
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self.view addSubview:self.view1];
}
completion:^(BOOL finished) {
[self.view2 removeFromSuperview];
}];
Use: animateWithDuration:animations:completion:
Set the new image view alpha to 0.
Place the new image view in front of the current image view.
In animations: set alpha of the new image view to 1.0.
In completion: handler cleanup how ever you want.
Related
I'm switching between two container views in my program with UIViewAnimationOptionTransitionFlipFromLeft as such:
[UIView transitionWithView:self.view
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
self.timerPage.hidden = true;
self.countdownPage.hidden = false;
} completion:nil
];
It works fine but what bugs me is the fact that my views are faded in and out to black, i would like the colour instead to be white. I've searched various threads but i couldn't find a solution to this exact problem.
I can't upload an image yet but the image in this question shows exactly what I'm talking about. The view fades to black during transition: UIView transitionFromView: how can I do black background during transition?
You can get the behavior you want by adding a subview or sublayer to your view with a clear background color and then setting the background color of that to white in your animation block.
UIView *whiteView = [[UIView alloc] init];
whiteView.frame = self.button.bounds;
[self.button addSubview:whiteView];
[UIView animateWithDuration:0.45 animations:^{
whiteView.backgroundColor = [UIColor whiteColor];
}];
I have custom action sheet that I create as VC. I present it with 'overCurrentContex' with the code
[self presentViewContoller:myVC animated:YES completion:nil];
I need to add dark background to it.
I added it with code
self.view.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
But as I present it modally it slides from the bottom with my dark backgroundColor.
I need that this color appears like Fade.
How can I do it? I need to add custom transition to it?
As I understand it, you're wanting to have the ViewController slide up from the bottom then have the background color change (transition) to a darker color of your choice?
I'd probably use the alpha value of the background and animate it to become more opaque to achieve that effect.
Here's a possible animation of the alpha value using an animation block:
self.view.alpha = 0.0f;
float alphaValue = 1.0f;
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
animations:^(void) {
//The stuff you want to animate
self.view.alpha = alphaValue;
}
completion:^(BOOL finished){
//anything you want to do right after the animation is done
}];
I have a view and on that there is a AVPlayer on that I need a dissolve animation when the player is
moving from the first video to second video.
I don't know your UIView structure, but you can load the new UIView / AVVideo and use UIView animations to transform the current view to the second one.
// load new UIView
// make it invisible
[UIView animateWithDuration:1
delay:1.5
options:UIViewAnimationCurveEaseOut
animations:^{
// fade in, or change x/y to slide in
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
I have a horizontally-scrolling paging UIScrollView in an iPad app, containing lots of pages. On the last page, I tap on a button on the screen to reset back to page 1. I would like to be able to cross-dissolve this transition, but it doesn't seem to work:
[UIView transitionWithView:self.view duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationOptionAllowAnimatedContent animations:^{
pagingScrollView.contentOffset = CGPointZero;
} completion:^(BOOL finished) {
[self refreshPages];
}];
I read that adding UIViewAnimationOptionAllowAnimatedContent will allow all content to transition, but it doesn't work. Instead, the screen cross-dissolves to the background colour, and when the transition is complete, the first page just appears.
you cannot fade-out a UIView (the scroller) AND simultaneously fade-in the same view...
you could just using different UIViews...
what you can do is:
1) fadeOut the scroller in the current position (to the backGround)
2) while the scroller is invisible, move it to the right position (with no animation)
3) fadeIn the scroller from the backGround
something like:
// START FIRST PART OF ANIMATION
[UIView animateWithDuration:0.5 delay:0.0 options: options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationOptionAllowAnimatedContent animations:^{
pagingScrollView.alpha = 0;
} completion:^(BOOL finished) {
// FIRST PART ENDED
// MOVE SCROLLER (no animation)
pagingScrollView.contentOffset = CGPointZero;
// START SECOND PART OF ANIMATION
[UIView animateWithDuration:0.5 delay:0.0 options: options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationOptionAllowAnimatedContent animations:^{
// fadeIn - animated
pagingScrollView.alpha = 1;
} completion:^(BOOL finished) {
// ANIMATION ENDED
[self refreshPages];
}];
}];
NEW EDIT:
thanks to amadour, who taught me something with his comments,
i hope he could add an answer of his own, i would vote for him
anyway, to answer to jowie original question:
i got the right animation just moving the contentOffset setting out of the animation block,
and removing UIViewAnimationOptionAllowAnimatedContent (not really needed), and passing pagingScrollView as parameter for transitionWithView
this worked for me:
pagingScrollView.contentOffset = CGPointZero;
[UIView transitionWithView:pagingScrollView duration:3.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
// pagingScrollView.contentOffset = CGPointZero; // move up, outside of animation block
} completion:^(BOOL finished) {
NSLog(#"-->> END amimation");
[self refreshPages];
}];
I am brand new to Core Animation, and I need to know how to do 2 animations:
I need to switch XIBs by fading through black (fully releasing the the first view controller)
I need to mimic the UINavigationController's pushViewController animation (switching XIBs and releasing the first view controller)
How can you achieve these animated view transitions?
I've done both of these animations, but maybe not in the exact way you are looking for.
Fade View to black, I took this the other way an instead added a new
subview that covered the entire window that was Black and animated
the Alpha from 0.0 to 1.0. Made for a nice effect.
[UIView animateWithDuration:0.5
animations:^{ _easterEgg.alpha = 1.0; }
completion:^(BOOL finished) { [self animateIndex:0]; }];
Slide in a view like UINavigationController. I didn't do this exactly like UINavigationController since it does multiple animations, but I did have a new view slide the previous view off screen. This code sets the frame of the new view off screen to the right of the current view, builds a frame location that is off the screen to the left, and grabs the current visible frame. Finally it just animates the new view from off screen right into the visible frame, and the old view from the visible frame to off left. Then removes the old view.
CGRect offRight = CGRectMake(_contentView.frame.size.width,
0,
_contentView.frame.size.width,
_contentView.frame.size.height);
CGRect offLeft = CGRectMake(-_contentView.frame.size.width,
0,
_contentView.frame.size.width,
_contentView.frame.size.height);
CGRect visibleFrame = CGRectMake(0, 0, _contentView.frame.size.width, _contentView.frame.size.height);
[view setFrame:offRight];
UIView *currentView = [[_contentView subviews] lastObject];
[_contentView addSubview:view];
[UIView animateWithDuration:0.5
animations:^{
[currentView setFrame:offLeft];
[view setFrame:visibleFrame];
}
completion:^(BOOL finished) {
[currentView removeFromSuperview];
}];