Cannot make a smooth animation in UIView - iphone

I have add a view animation to my view on button click, the view will slide to right side on first click and will come back on second click..(just like in Facebook iPhone app)
But the animation is not so smooth, there are some jerkings and all. But when it reaches the final (x, y) they are in correct position, when the view is moving the animation doesn't look so professional.Help me to correct this
and one question,is there any way to animate the below tabbar with the same event.
(You can see in images that the tabbar is not moved with animation)
Adding the code here!
- (IBAction)ShowView:(id)sender
{
if (n==0)
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
self.view.center = CGPointMake(410, 205);
[[self.view layer] addAnimation:animation forKey:#"SwitchToNoti"];
n=1;
}else
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
self.view.center = CGPointMake(160,205);
[[self.view layer] addAnimation:animation forKey:#"SwitchTohome"];
n=0;
}

try this
- (IBAction)ShowView:(id)sender {
if (n==0)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[self.view setCenter:CGPointMake(self.view.center.x + 100, self.view.center.y)];
[UIView commitAnimations];
n=1;
}else
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[self.view setCenter:CGPointMake(self.view.center.x - 100, self.view.center.y)];
[UIView commitAnimations];
n=0;
}
}

You can use the following code to show the animation :-
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.40];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.view.frame = TestRect;
[UIView commitAnimations];
You need to make a CGRect named TestRect here and see if this solves your problem.

Related

How to add a subview with a presentmodalview type animation?

I have an application in which I need to
add a subview to a view with presentmodalview type animation
But in
CATransition
I didn't find any type of transition like that. Also in
UItransitionstyle
Can anybody guide me on this?
If you only want to use CATransition
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionMoveIn];
[animation setSubtype:kCATransitionFromTop];
[animation setDuration:0.6f];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:
kCAMediaTimingFunctionEaseInEaseOut]];
[subview.layer addAnimation:animation forKey:#"someTransition"];
Change the frame of the subview insite the UIView Animation, like as follows,
initially set the
subView.frame=CGRectMake(0,self.view.frame.size.height,your_width,your_height);
then set the required frame position inside the animation
[UIView animateWithDuration:0.25 delay:0 options: UIViewAnimationCurveEaseOut animations:^ {
// set subView's frame
} completion:^(BOOL finished) {
}
];
Try this code
[self.view bringSubviewToFront:yoursubview];
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionFade];
[animation setDuration:1.00];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:
kCAMediaTimingFunctionEaseIn]];
[yoursubview.layer addAnimation:animation forKey:#"fadeTransition"];
For more info check these links
Code on GitHub
Implement Changes Between Two Views In iPhone
add subview with animation
Try to use UIView animation block:
yourView.frame = CGRectMake(0, yourSuperView.frame.size.height, yourView.frame.size.width, yourView.frame.size.height);
[yourSuperView addSubview:yourView];
[UIView animateWithDuration:1.0 animations:^{
yourView.frame = CGRectMake(0, 0, yourView.frame.size.width, yourView.frame.size.height);
} completion:^(BOOL finished) {
//Task to do on Animation completion.
}];
You change the animation duration according to your convince. Also you can use following:
[UIView animateWithDuration:1.0 animations: {
//your Implemntation
}];
or
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationCurveEaseIn animations:^{
} completion:^(BOOL finished) {
}];
Please check the UIView class reference for animation option in UIViewAnimationOptions section and detail of methods.

Push-Pop kind Animation for UIViewController without using NavigationController

I want to implement Push/Pop kind of animation in my application. Currently in my application, I'm simply adding & removing viewcontroller using addsubview & removesubview without any animation.
Adding navigation controller will be a major change in application as it will change whole structure of application. Is there any way to implement such kind of animation with using navigation controller.
Try this
first give your subview a frame
secondview.frame=CGRectMake(330, 0, 320, 460);
Then when you are adding it
[self.view addSubView:secondview];
[UIView beginAnimations:#"bringViewDown" context:nil];
[UIView setAnimationDuration:0.2];
firstview.frame=CGRectMake(-330, 0, 320, 460);
secondview.frame=CGRectMake(0, 0, 320, 460);
[UIView commitAnimations];
Hope this helps.....
- (void)viewDidLoad
{
[super viewDidLoad];
viewSecond.frame = CGRectMake(330, 0, 320, 548);
}
- (IBAction)onBtnClick:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
viewFirst.frame = CGRectMake(-330, 0, 320, 548);
viewSecond.frame = CGRectMake(0, 0, 320, 548);
[UIView commitAnimations];
}
- (IBAction)onBtnClick2:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
viewFirst.frame = CGRectMake(0, 0, 320, 548);
viewSecond.frame = CGRectMake(330, 0, 320, 548);
[UIView commitAnimations];
}
Implement custom container controller refer Session 102 - Implementing UIViewController Containment in WWDC 2011
Add subview with push animation
//push animation
CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.view.layer addAnimation:transition forKey:nil];
[self addSubview:subView];
and remove subview with pop animation
//pop animation
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[self.superview layer] addAnimation:animation forKey:#"SlideView"];
[self removeFromSuperview];

UIButton move animated problem

how to make a UIButton when moving (animated) without shadow? i tried the code but cannot make it can someone modifify or point out the problem
-(IBAction)move{
point=CGPointMake(0,1);
for(int i=0;i<50;i++){
NSLog(#"fdfafa");
CATransition *animation = [CATransition animation];
[animation setType:#"push"];
[animation setSubtype:#"fromBottom"];
[animation setDuration:0.5];
//[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[testButton.layer addAnimation:animation forKey:kAnimationKey];
testButton.center = CGPointMake(testButton.center.x, testButton.center.y + point.y);
}
NSLog(#"%f",testButton.center.y);
}
or have other better method to make a object move animated?
I'd use something like this:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
button.center = CGPointMake(x,y);
[UIView commitAnimations];

Animation : How to make UIbutton move from right to left when view is loaded?

I have not done any animation stuff with iphone development so far.
Can anybody help me how to make UIbutton move from right to left when view is loaded?
I want to generate effect like that : when view is loaded, the button appears moving from right end to left & stops at its place. Is it possible?
Any help regarding this animation stuff is appreciated. Please help with some sample if available.
Thanks in advance.
Just to make it even slicker, I would suggest not changing the UIView's frame but its center;
CGPoint newLeftCenter = CGPointMake( 20.0f + myButton.frame.size.width / 2.0f, myButton.center.y);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
myButton.center = newLeftCenter;
[UIView commitAnimations];
in your viewDidAppear:(BOOL)animated method you can change frame of your button inside animation block. so you'll see what you want. Check documentation about animations in UIView class reference. The simpliest animation block will be like that(sorry, i have no mac nearby right now, so code may not be working just after copy and paste)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
[myButton setFrame:newFrame];
[UIView commitAnimations];
From top to bottom animation
CATransition *animation = [CATransition animation];
[animation setDuration:0.4];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromBottom];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[TopButton layer] addAnimation:animation forKey:#"SwitchToDown"];
/* Common transition subtypes. */
kCATransitionFromRight
kCATransitionFromLeft
kCATransitionFromTop
kCATransitionFromBottom
Swift:
let transition1: CATransition = CATransition()
let timeFunc1 : CAMediaTimingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition1.duration = 1.0
transition1.timingFunction = timeFunc1
transition1.type = kCATransitionPush
transition1.subtype = kCATransitionFromRight
self.yourBtnORViewRef.layer.addAnimation(transition1, forKey: kCATransition)
Objective-C
CATransition *animation = [CATransition animation];
[animation setDuration:1.0];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[self.yourBtnORViewRef layer] addAnimation:animation forKey:#"SwitchToRight"];
Swift 5:
//Animate Your Button From Left to Right
let transition1: CATransition = CATransition()
let timeFunc1 : CAMediaTimingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
transition1.duration = 1.5
transition1.timingFunction = timeFunc1
transition1.type = CATransitionType.push
transition1.subtype = CATransitionSubtype.fromLeft
self.yourBtnRef.layer.add(transition1, forKey: kCATransition)

PageTurn animation in iPhone

How to make page turn animation like the Stanza app ?
Earlier i was using , but its not the animation i am looking for.
[CATransaction begin];
CATransition *animation = [CATransition animation];
[animation setType:(prevPage ? #"pageUnCurl" : #"pageCurl")];
[animation setDuration:0.7f];
[animation setFillMode: ( prevPage ? kCAFillModeBackwards : kCAFillModeForwards )];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[[self layer] addAnimation:animation forKey:#"transitionViewAnimation"];
[CATransaction commit];
Thanks
[UIView beginAnimations:#"yourAnim" context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:yourView cache:cacheFlag];
...
[UIView commitAnimations];
This does the page curl animation. For the flip, use the UIViewAnimationTransitionFlipFromLeft or UIViewAnimationTransitionFlipFromRight constant.