Animation lag at the first run - iphone

The search bar is animated using the following code:
- (IBAction)toggleSearchBar:(UIBarButtonItem *)sender{
CATransition *applicationLoadViewIn = [CATransition animation];
[applicationLoadViewIn setDuration:0.2];
[applicationLoadViewIn setType:kCATransitionPush];
[applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
if (self.searchBar.hidden) {
self.searchBar.hidden = NO;
[self.searchBar becomeFirstResponder];
[applicationLoadViewIn setSubtype:kCATransitionFromBottom];
[[searchBar layer] addAnimation:applicationLoadViewIn forKey:kCATransitionPush];
}else{
self.searchBar.hidden = YES;
[self.searchBar resignFirstResponder];
[applicationLoadViewIn setSubtype:kCATransitionFromTop];
[[searchBar layer] addAnimation:applicationLoadViewIn forKey:kCATransitionPush];
}
}
It works fine except the lag at the first run of the animation. Is there anyway to fix it? Any help will be appreciated:)
Edit 1
he leftBarButton is wired to toggleSearchBar: action. The app is loaded. The first animation is the first time the leftBarButton clicked.

Why don't you show it off screen on loading the view controller... it's a bit hacky but it should do the trick.

Related

Partial Curl get dissapears when home button clicks

I am doing a partial curl using CATransition, It works fine but After home button click while the page is curved and trying to resume then the curved page gets disappear.
Here is my code
self.animation = [CATransition animation];
[self.animation setDelegate:self]; [animation setDuration:1.00];
[self.animation setTimingFunction:UIViewAnimationCurveEaseInOut];
if (!curled){
self.animation.type = #"pageCurl";
self.animation.fillMode = kCAFillModeForwards;
self.animation.endProgress = 0.50;
}
[self.animation setRemovedOnCompletion:NO];
[[self view] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[[[self view] layer] addAnimation:self.animation forKey:#"pageCurlAnimation"];
This may help you
-(void)viewWillDisappear:(BOOL)animated{
[self.view.layer removeAllAnimations];
[super viewWillDisappear:YES];
}

iPhone: How to hide tab bar to show fullscreen view

I have gallery view in my iphone app. On tap gesture, I am hiding the Navigation bar by:
[self.navigationController setNavigationBarHidden:activated animated:YES];
I have tab bar also, how to hide it and display the image as full screen?
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionFade];
[[self.view.window layer] addAnimation:animation forKey:#"layerAnimation"];
[self.tabBarController.tabBar setHidden:YES];
// Display tab bar animated
CATransition *animation = [CATransition animation];
animation setType:kCATransitionFade];
[[self.view.window layer] addAnimation:animation forKey:#"layerAnimation"];
[self.tabBarController.tabBar setHidden:NO];
I have not checked this code but I think it will work
[[self navigationController] setHidesBottomBarWhenPushed:YES];
i hope this will help you.
self.hidesBottomBarWhenPushed = YES;
this will hide the tabbar
If it a UITabBarController then it has a property tabBar.
tabBarController.tabBar.hidden = YES
should work for you.
Also if your image is not a full size then change the frame of UIImageView to CGRectMake(0, 0, 320, 460); if you are showing a status bar.

Animating transitions of two subviews simultaneously with CATransition

I have my main view, and when a user taps a 'Show' button I want two things to happen:
Fade in a black UIView with alpha 0.5 covering the entire parent
view. I'll refer to this as darkenBackground
Slide the view of a second view controller
(self.secondViewController.view) on top of darkenBackground
When a user is finished, they tap 'Done' and the following happens:
darkenBackground fades out and is removed from the superview.
self.secondViewController.view slides out of the screen in the
bottom direction and is removed from the superview.
All animations working simultaneously.
So far, I've been able to achieve the first part (sort-of), transition-in. This works fine. But when the user taps 'Done' the transition-out does not work as required. The problems I'm faced with are:
darkenBackground and self.secondViewController.view fade out and are removed, but
self.secondViewController.view wasn't supposed to fade out! I tried animating only the second view but it simply disappears without animation.
When a user taps 'Show' for a second time
(transition-in), the view from the previous transition
appears before the animation takes place. It doesn't look like it
was removed from the superview previously.
Here is my code:
When a user taps 'Show':
- (IBAction)showSecondView {
darkenBackground = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
[darkenBackground setBackgroundColor:[UIColor blackColor]];
[darkenBackground setAlpha:0.5];
self.secondViewController.view.frame = CGRectMake(0, 0, 320, 460);
self.secondViewController.view.backgroundColor = [UIColor clearColor];
[CATransaction begin];
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionFade];
CATransition *animation2 = [CATransition animation];
[animation2 setDuration:0.5];
[animation2 setType:kCATransitionMoveIn];
[animation2 setSubtype:kCATransitionFromTop];
[animation2 setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.view addSubview:darkenBackground];
[self.view addSubview:self.secondViewController.view];
[[self.view layer] addAnimation:animation forKey:#"darkenBkgFadeIn"];
[[self.secondViewController.view layer] addAnimation:animation2 forKey:#"ShowSecondView"];
[CATransaction commit];
}
When a user taps 'Done' (A function defined in the secondViewController.m class where it refers to the main view controller variables through delegate):
- (IBAction)hideSecondView {
[CATransaction begin];
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionFade];
CATransition *animation2 = [CATransition animation];
[animation2 setDuration:0.5];
[animation2 setType:kCATransitionPush];
[animation2 setSubtype:kCATransitionFromBottom];
[animation2 setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.myParentController.darkenBackground removeFromSuperview];
[self.myParentController.secondViewController.view removeFromSuperview];
[[self.myParentController.view layer] addAnimation:animation forKey:#"darkenBkgFadeOut"];
[[self.myParentController.secondViewController.view layer] addAnimation:animation2 forKey:#"RemoveSecondView"];
[CATransaction commit];
}
Ultimately I want to show the view in the same way that the TWTweetComposeViewController modal view appears, where the background darkens and the controller appears. I could just use a modal view controller, but the issue I have with that is that when presenting the view controller it only 'appears' and does not slide from the bottom (since I require a semi-transparent background, I cannot use the default context for the modal view).
Cannot figure this one out, would appreciate some guidance.
I assume that self.myParentController.view used in -hideSecondView is the same view as self.view in -showSecondView. If this is the case, then you're applying a transition to the whole view here:
[[self.myParentController.view layer] addAnimation:animation forKey:#"darkenBkgFadeOut"];
You added self.secondViewController.view as a subview of self.view in -showSecondView, so when you later perform a transition on self.myParentController.view, it's going to apply the transition to all its subviews (i.e., self.secondViewController.view) as well.
Instead of applying the transition to self.myParentController.view in -hideSecondView, apply it directly to darkenBackground:
[[self.myParentController.darkenBackground layer] addAnimation:animation forKey:#"darkenBkgFadeOut"];
If I recall correctly, you can apply CATransitions when layers are added/removed to/from a view. If not, then you'll have to wrap darkenBackground in a separate container view and perform the transitions on that so it doesn't include self.secondViewController.view when you do the fade out.

switching two views causes crash

XYZAppDelegate *appdelegate=[[UIApplication sharedApplication] delegate];
CSelectorViewController *cPrice=[[CSelectorViewController alloc] initWithNibName:#"CSelectorViewController" bundle:nil];
[cPrice.view setFrame:CGRectMake(0, 20, 320, 480)];
[appdelegate.window addSubview:cPrice.view];
[self applyAnimation];
[cPrice release];
i am adding a view like this,not using navigationcontroller,since i have to switch two views top-bottom also.but when i am giving the [cPrice release]; the app crashes. but when i am not giving it in code... the static analyser is showing a warning..what is the right way to do this?
-(void)applyAnimation
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
XYZAppDelegate *appdelegate=[[UIApplication sharedApplication] delegate];
[[appdelegate.window layer] addAnimation:animation forKey:#"slideLeft"];
}
You're adding the view of cPrice to the window as a subview (which will implicitly retain the view), but you do nothing to preserve cPrice itself. You should make it a retained property or some such.

Why are my CATransitions acting up?

I am using the following code to switch between views with CATransition.
CATransition *applicationLoadViewIn = [CATransition animation];
[applicationLoadViewIn setDuration:20];
[applicationLoadViewIn setType:kCATransitionPush];
[applicationLoadViewIn setSubtype:kCATransitionFromTop];
[applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
ViewToSwitchTo *myviewcontroller = [[ViewToSwitchTo alloc] init];
[self.view.layer addAnimation:applicationLoadViewIn forKey:kCATransitionPush];
[self.view addSubview:myviewcontroller.view];
It functions mostly how I want it to. It pushes from the top like it should, however it for some reason acts strangely. First, the view I am switching to starts coming in from the bottom like it should, but for some reason, the view that I am switching FROM appears over the top of it with low opacity, so you see both of them. However, you also see the view that is coming in, shifted maybe 100 pixels upwards, on top of itself and the other view, once again with low opacity. Just before the halfway point of the the transition, everything works fine, you only see the view that is coming in and the view going out, doing what they should be doing. But slightly after the halfway point, the view I am switching to appears in its final destination, under the view I am switching from, and the view I am switching from has been reduced in opacity.
What is going on here?
I really not understand what you want but hope this code will help to implement ,if not then tell more about you CATransition
-(void) startAnimation {
fullImageView.alpha = 0.50;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(pulseAnimationDidStop:finished:context:)];
fullImageView.alpha = 1.0;
[UIView commitAnimations];
}
- (void)pulseAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
if(hasFocus) {
[self startAnimation];
} else {
fullImageView.alpha = 1.0;
}
}
-(void) setHasFocus:(BOOL)_hasFocus {
hasFocus = _hasFocus;
if(hasFocus) {
[self startAnimation];
}
}
You can use this way as well
CATransition *applicationLoadViewIn = [CATransition animation];
[applicationLoadViewIn setDuration:1.5];
[applicationLoadViewIn setType:kCATransitionPush];
[applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
//[applicationLoadViewIn setRepeatCount:114];
productVC=[[PoductViewController alloc]initWithNibName:#"PoductViewController" bundle:[NSBundle mainBundle]];
[[productVC.view layer] addAnimation:applicationLoadViewIn forKey:kCATransitionPush];
[self.view addSubview:productVC.view];
But I suggest you to use Navigation Controller in place of
[self.view addSubview:productVC.view];
just use
[self.navigationController pushViewController:productVC animated:YES];
If you use this, there will be no background view, you will see white screen of the present view. But if you add subview, then you need to manually remove subview.