Have an object fade in and out of existence - cocoa-touch

I have a view that sets its hidden when the user taps the main view. I need the view to fade in and out of existence so it looks smoother than just disappearing.
Heres my code so far (It's inside a touches event):
if (!isShowing) {
isShowing = YES;
myView.hidden = YES;
//Needs to fade out here
}
else {
isShowing = NO;
myView.hidden = NO;
//Needs to fade in here
}

I've never had luck with animating hidden. Instead, animate alpha.

Just wrap your code like this:
[UIView beginAnimations:nil context:NULL];
if (!isShowing) {
isShowing = YES;
myView.hidden = NO
}
else {
isShowing = NO;
myView.hidden = YES
}
[UIView commitAnimations];
or simplify it to this:
[UIView beginAnimations:nil context:NULL];
isShowing = !isShowing;
myView.hidden = isShowing? NO : YES;
[UIView commitAnimations];
You might also want to use UIView's setAnimationDuration:, setAnimationCurve:, or setAnimationBeginsFromCurrentState: methods to customize how the view fades in and out.

Related

Keep the End Position of Animation

I tried everything, but everytime its the same problem:
i want to move a button for y = 276. when the animation ends, my buttons jumps back to the startposition, but i want that the endanimationposition is the new position, and the button has to stay there, until a new animation is called.
tried with
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
CGPoint p = _cell00.center;
p.x += 100;
_cell00.center = p;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
same issue like
[UIView animateWithDuration:1.0 animations:^{
_cell00.frame = CGRectOffset(_cell00.frame, 0, 20);
}];
or
[UIView animateWithDuration:0.5 animations:^{
_cell00.center = CGPointMake(0, 20.0);
}];
EDIT:
I call this Animation with my Button called statisticsBUttonPressed.
- (IBAction)statisticsButtonPressed:(id)sender {
if (statOrGame == 0) {
statOrGame = 1;
}else {
statOrGame = 0;
}
NSLog(#"statOrGame? %d", statOrGame);
[self animateView:(UIButton *)sender];
}
-(void) animateView:(UIButton *)sender {
sender.userInteractionEnabled = NO;
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{ // The iOS device = iPhone or iPod Touch
NSLog(#"IS IPHONE 5");
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if (iOSDeviceScreenSize.height == 568)
{
if (statOrGame) {
[UIView animateWithDuration:1.0 animations:^{
_cell00.frame = CGRectOffset(_cell00.frame, 0, 20);
}];
}
}
}
}
The problem is that you're using auto layout. You can't (just) change the frame of a view that is positioned by auto layout, because what positions the view is its constraints. You need to change the constraints. You can do this at the end of the animation, or you can simply animate the change of constraints.

uiview animateWithDuration cant hide in same toggle method

Trying to hide and show a view from a single method but it seems like it should work to show and hide but its instant.
- (void)toggleMyView:(BOOL)show
{
CGRect orig = self.originalMyViewFrame; // original frame of the view
CGRect offScreen = CGRectMake(orig.origin.x, self.view.frame.size.height, orig.size.width, orig.size.height);
if (show) {
self.myView.frame = offScreen; // set off screen
self.myView.hidden = NO; // unide the view
[UIView animateWithDuration:0.5 animations:^{
self.myView.frame = orig; // animate to original position
}];
} else {
[UIView animateWithDuration:0.3 animations:^{
self.myView.frame = offScreen; // animate off screen
} completion:^(BOOL finished) {
self.myView.hidden = YES; // hide when animation finished
}];
}
}
If I remove the hide logic it animates in nicely.
- (void)toggleMyView:(BOOL)show
{
CGRect orig = self.originalMyViewFrame; // original frame of the view
CGRect offScreen = CGRectMake(orig.origin.x, self.view.frame.size.height, orig.size.width, orig.size.height);
if (show) {
self.myView.frame = offScreen; // set off screen
self.myView.hidden = NO; // unide the view
[UIView animateWithDuration:0.5 animations:^{
self.myView.frame = orig; // animate to original position
}];
} else {
self.myView.hidden = YES; // hide when animation finished
}
}
I dont see why adding the hide logic breaks.
Could this be a memory retain cycle problem?

How to flip an individual UIView (without flipping the parent view)

This is an iPad project where I have a UIView with several subViews, and I am trying to animate one of this UIViews using [UIView transitionFromView:toView:duration:options:completion], but when I run this method the whole parent view gets flipped!
This is the code I'm using:
UIView *secondView = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 300, 300)];
[newView setBackgroundColor:[UIColor redColor]];
[UIView transitionFromView:self.firstView.view toView:secondView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
Any idea on how can I flip between this views without animating the whole parent view?
Thanks!
this code may helps you:
put the two views you want to flip inside an unnamed view with the same size
and link the IBOutlet UIView *newView,*oldView; to the views and put the new view on top
bool a = NO;
#implementation ViewController
- (IBAction)flip:(id)sender
{
if (a == NO) {
[UIView transitionFromView:oldView toView:newView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:NULL];
a = YES; // a = !a;
}
else {
[UIView transitionFromView:newView toView:oldView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:NULL];
a = NO; // a = !a;
}
}
I had problems getting this to work also. I used the code written by Floris, but although it worked for the first "flip" the next flip started to go weird where the buttons and labels on the frontside UI View lost there positions.
I put the code below into place and it is working fine.
Couple of things to note:
panelView is a UIView control on the ViewController that has two other UIViews nested inside it (subviews). The first one is called frontView, second one is called backView. (see picture below)
I have a bool property called displayingFront on the class
(in my .h)
#property BOOL displayingFront;
in my .m
#synthesize displayingFront;
in my viewDidLoad method
self.displayingFront = YES;
This is the code in the .m that i have rigged up to the two buttons if front and back UIViews...
- (IBAction)flip:(id)sender
{
[UIView transitionWithView:self.panelView
duration:1.0
options:(displayingFront ? UIViewAnimationOptionTransitionFlipFromRight :
UIViewAnimationOptionTransitionFlipFromLeft)
animations: ^{
if(displayingFront)
{
self.frontView.hidden = true;
self.backView.hidden = false;
}
else
{
self.frontView.hidden = false;
self.backView.hidden = true;
}
}
completion:^(BOOL finished) {
if (finished) {
displayingFront = !displayingFront;
}
}];
}
Use a container view to hold your subviews, and make the container view the same size as your subview that you're trying to animate.
So, you can setup your views like this.
self.view-> "a container view" -> subviews
**//flipping view continuously**
bool a = NO;
-(void)viewDidLoad
{
// THIS is the canvass holding the two views this view is flipping
UIView *v=[[UIView alloc]initWithFrame:CGRectMake(40, 40, 150, 150)];
v.backgroundColor=[UIColor clearColor];
[self.view addSubview:v];
[v addSubview:yourfirstview];
[v addSubview:yoursecondview];
// calling the method
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:#selector(flip)
userInfo:nil
repeats:YES];
}
//flipping view randomly
-(void)flip
{
if (a == NO)
{
[UIView transitionFromView:yourfirstview toView:yoursecondview duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
a = YES;
}
else if(a==YES)
{
[UIView transitionFromView:yoursecondview toView:yourfirstview duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
a = NO;
}
}
I fixed this problem by using old-school animations:
[UIView beginAnimations:#"Flip" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:firstView cache:YES];
[self addSubview:secondView];
[UIView commitAnimations];
Additionally you can remove the firstView:
[firstView removeFromSuperview];
I'd like to make an update of Steve Parker's answer in SWIFT 4:-
var displayBlankView:Bool = true
UIView.transition(with:flipView, duration:1.0, options:displayBlankView ? .transitionFlipFromLeft:.transitionFlipFromRight, animations:{
if(self.displayBlankView){
self.view1.isHidden=true
self.view2.isHidden=false
}else{
self.view1.isHidden=false
self.view2.isHidden=true
}
}, completion:{
(finished: Bool) in
self.displayBlankView = !self.displayBlankView;
})

Checking if the UIPickerView is shown or hidden? The *if* conditions?

I have a little problem with my UIPickerView animation.
I did set up the animation so when I click a button the UIPickerView will slide up and after another click it will slide down. But I hit the problem on if. I tried to set up a new settings bool and set it's value after each animation, so the if was just checking for the bool. I'm not sure if I did explained it well, but maybe you get the idea from the code.
But unfortunately it's not working... Any ideas?
- (void)viewDidLoad
{
[super viewDidLoad];
settings = [NSUserDefaults standardUserDefaults];
[settings setBool:NO forKey:#"pickerShown"];
[settings synchronize];
}
- (IBAction)showPicker:(id)sender {
CGRect rect = countryPicker.frame;
CGPoint origin = CGPointMake(0, 510); // Some off-screen y-offset here.
rect.origin = origin;
countryPicker.frame = rect;
if ([settings boolForKey:#"pickerShown"] == YES) {
// Perform transform to slide it off the screen.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
countryPicker.transform = CGAffineTransformMakeTranslation(0, -0); // Offset.
[UIView commitAnimations];
[settings setBool:NO forKey:#"pickerShown"];
[settings synchronize];
} else if ([settings boolForKey:#"pickerShown"] == NO) {
// Perform transform to slide it onto the screen.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
countryPicker.transform = CGAffineTransformMakeTranslation(0, -266); // Offset.
[UIView commitAnimations];
[settings setBool:YES forKey:#"pickerShown"];
[settings synchronize];
}
}
EDIT:
Just found the solution...
If anyone's interested - here it is:
The h. file:
.
.
.
{
BOOL pickerShown;
}
.
.
.
The .m file:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect rect = countryPicker.frame;
CGPoint origin = CGPointMake(0, 510); // Some off-screen y-offset here.
rect.origin = origin;
countryPicker.frame = rect;
pickerShown = NO;
}
- (IBAction)showPicker:(id)sender {
if (pickerShown == NO) {
// Perform transform to slide it onto the screen.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
//[UIView setAnimationDidStopSelector:#selector(hidePicker)];
countryPicker.transform = CGAffineTransformMakeTranslation(0, 0); // Offset.
[UIView commitAnimations];
[self performSelector:#selector(hidePicker) withObject:nil afterDelay:1];
} else if (pickerShown == YES) {
//countryPicker.hidden = NO;
// Perform transform to slide it onto the screen.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
countryPicker.transform = CGAffineTransformMakeTranslation(0, -266); // Offset.
[UIView commitAnimations];
countryPicker.hidden = NO;
}
}
If a view is visible on screen, it's window property will be non-nil.
Just found the solution... If anyone's interested - here it is:
The h. file:
.
.
.
{
BOOL pickerShown;
}
.
.
.
The .m file:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect rect = countryPicker.frame;
CGPoint origin = CGPointMake(0, 510); // Some off-screen y-offset here.
rect.origin = origin;
countryPicker.frame = rect;
pickerShown = NO;
}
- (IBAction)showPicker:(id)sender {
if (pickerShown == NO) {
// Perform transform to slide it onto the screen.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
//[UIView setAnimationDidStopSelector:#selector(hidePicker)];
countryPicker.transform = CGAffineTransformMakeTranslation(0, 0); // Offset.
[UIView commitAnimations];
[self performSelector:#selector(hidePicker) withObject:nil afterDelay:1];
} else if (pickerShown == YES) {
//countryPicker.hidden = NO;
// Perform transform to slide it onto the screen.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
countryPicker.transform = CGAffineTransformMakeTranslation(0, -266); // Offset.
[UIView commitAnimations];
countryPicker.hidden = NO;
}
}
You can check it with:
self.countryPicker.superview
If picker is hidden then superview will be nil. Otherwise it will be containing view.
If all you want is the basic effect of displaying the picker view, then removing it again there is an easier way.
pseudocode:
//do any picker initialization here
if(!pickerIsShowing)
[self.view addSubview:yourPickerView];
else
[self.yourPickerView removeFromSuperView];

Is it possible to hide a UIToolbar with an animation?

I know this code is available to UINavigationController only.
[self.navigationController setNavigationBarHidden:YES animated:YES];
An example using blocks. This will hide a toolbar at the top of an iPad screen.
[UIView animateWithDuration:.7
animations:^(void)
{
CGRect toolbarFrame = self.toolbar.frame;
toolbarFrame.origin.y = -44; // moves iPad Toolbar off screen
self.toolbar.frame = toolbarFrame;
}
completion:^(BOOL finished)
{
self.toolbar.hidden = YES;
}];
Here is the Code:
[UIView beginAnimations:#"hideView" context:nil];
[UIView setAnimationDuration:0.7];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:toolBar cache:YES];
toolbarFrame.origin.y = 380;
toolBar.frame = toolbarFrame;
[UIView commitAnimations];
you can modify the toolBar 'y' origin.