how to change position of button with Animation - ios

change position when it is pressed.
UIButton * button = sender;
CGPoint position = button.frame.origin;
[UIButton setAnimationDuration:1.0];
CGSize size = button.frame.size;
button.frame = CGRectMake(position.x + 80,position.y,size.width,size.height);

Try this:
[UIView animateWithDuration:1.0 animations:^{
btn.frame = CGRectOffset(btn.frame, 0, 20);
}];

For animating the position of UIButton with duration 1.0 seconds
CGPoint position = button.frame.origin;
CGSize size = button.frame.size;
-(IBAction)btnClick:(id)sender {
[UIView animateWithDuration:1.0 animations:^{
button.frame = CGRectMake(position.x+80,position.y,size.width,size.height);
}];
}

Try this :
button.frame = CGRectMake(position.x + 80,position.y,size.width,size.height); // initial frame
[UIView animateWithDuration:2 animations:^{
button.frame = CGRectMake(position.x + 80,position.y,size.width,size.height); // new frame frame
} completion:^(BOOL finished) {
}];

you can use Animation block of UIView class ...
whatever you change the frame inside this block , the canvas fill the frame in between
Set the Start frame
button.frame= yourInitialFrame;
[UIView animateWithDuration:1.0 animations:^{
// set the End Frame here
button.frame = CGRectMake(position.x +
80,position.y,size.width,size.height); // new frame frame
} completion:^(BOOL finished) {
}];
also see this see this for more help

[UIView animateWithDuration:1.0 animations:^{
// set the End Frame here
button.frame = CGRectMake(position.x +
80,position.y,size.width,size.height); // new frame frame
} completion:^(BOOL finished) {
}];

Try this:
-(IBAction)ButtonClick:(id)sender
{
UIButton *aBtnRef = sender;
[UIView animateWithDuration:0.5 animations:^{
aBtnRef.frame = CGRectMake(aBtnRef.frame.origin.x + 50,aBtnRef.frame.origin.y, aBtnRef.frame.size.width, aBtnRef.frame.size.height);
} completion:^(BOOL finished) {
}];
}
For More Info :
Animation : How to make UIbutton move from right to left when view is loaded?
Moving UIButton position, animated

[UIView animateWithDuration:0.2 animations:^{
CGRect rect = button.frame;
rect.origin.x = rect.origin.x + 80;
button.frame = rect;
} completion:^(BOOL finished) {
}];

Related

I want to animate UITableView on Button Click

I have UITableView and i want to animate when user click on Button.
For Selected State --> BOTTOM TO TOP
For UnSelected State --> TOP TO BOTTOM
For that I have tried Below Code.
My TableView frame (0,39, width, height)
- (IBAction)onInviteFriendsButtonClick:(UIButton *)sender {
sender.selected =! sender.selected;
if (sender.selected) {
CGRect napkinBottomFrame = self.tblVWInviteFriend.frame;
napkinBottomFrame.origin.y = [UIScreen mainScreen].bounds.size.height;
self.tblVWInviteFriend.frame = napkinBottomFrame;
[UIView animateWithDuration:0.5 delay:0.0 options: UIViewAnimationOptionTransitionCurlUp animations:^{
self.tblVWInviteFriend.frame = CGRectMake(0, 39, self.tblVWInviteFriend.frame.size.width, self.tblVWInviteFriend.frame.size.height);
} completion:^(BOOL finished){/*done*/}];
}
else {
CGRect napkinBottomFrame = self.tblVWInviteFriend.frame;
napkinBottomFrame.origin.y = 39;
self.tblVWInviteFriend.frame = napkinBottomFrame;
[UIView animateWithDuration:0.5 delay:0.0 options: UIViewAnimationOptionTransitionCurlDown animations:^{
self.tblVWInviteFriend.frame = CGRectMake(0, -[UIScreen mainScreen].bounds.size.height, self.tblVWInviteFriend.frame.size.width, self.tblVWInviteFriend.frame.size.height);
} completion:^(BOOL finished){/*done*/}];
}
}
When i click button it will animate from BOTTOM TO TOP properly.
but when i click button again it will again gone from BOTTOM TO TOP.
Because you have set frame.origin.y of tableview is -y or XY coordinate.
self.tblVWInviteFriend.frame = CGRectMake(0, -[UIScreen mainScreen].bounds.size.height, self.tblVWInviteFriend.frame.size.width, self.tblVWInviteFriend.frame.size.height);
replace with this
self.tblVWInviteFriend.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, self.tblVWInviteFriend.frame.size.width, self.tblVWInviteFriend.frame.size.height);
Try this :
- (IBAction)onInviteFriendsButtonClick:(UIButton *)sender {
sender.selected =! sender.selected;
if (sender.selected) {
CGRect napkinBottomFrame = txtAOP.frame;
napkinBottomFrame.origin.y = [UIScreen mainScreen].bounds.size.height;
txtAOP.frame = napkinBottomFrame;
[UIView animateWithDuration:0.5 delay:0.0 options: UIViewAnimationOptionTransitionCurlUp animations:^{
txtAOP.frame = CGRectMake(0, 39, txtAOP.frame.size.width, txtAOP.frame.size.height);
} completion:^(BOOL finished){/*done*/}];
}
else {
CGRect napkinBottomFrame = txtAOP.frame;
napkinBottomFrame.origin.y = -[UIScreen mainScreen].bounds.size.height;
txtAOP.frame = napkinBottomFrame;
[UIView animateWithDuration:0.5 delay:0.0 options: UIViewAnimationOptionTransitionCurlDown animations:^{
txtAOP.frame = CGRectMake(0,39 , txtAOP.frame.size.width, txtAOP.frame.size.height);
} completion:^(BOOL finished){/*done*/}];
}
}

How to implement nice animation for rectangle UIButton to circle shape in iOS?

When i clicked on "Login" button i need to make my rectangle login button
Login Screenshot
to animate and change it shape to circle like below link
Login Button after i click
I have seen this code in stackoverflow but its not working the way i want
CGFloat width = self.login.frame.size.width;
CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:#"cornerRadius"];
morph.fromValue = #0;
morph.toValue = #(width/2);
morph.duration = 0.5;
[self.login.layer addAnimation:morph forKey:#"morph"];
self.login.layer.cornerRadius = width/2;
Please help
Thanks for answering this.
This is the final code
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationCurveLinear
animations:^{
self.layoutWidth.constant = 70;
[self.view layoutIfNeeded];
self.btnLogin.clipsToBounds = YES;
self.btnLogin.layer.cornerRadius =self.btnLogin.bounds.size.height/2.0f;
}
completion:^(BOOL finished){
}];
Try this,
Demo Animation
Code:
- (IBAction)clicklogin:(id)sender {
[UIView animateWithDuration:0.1
delay:1.0
options:UIViewAnimationCurveLinear
animations:^{
self.layoutWidth.constant = 70;
[self.view layoutIfNeeded];
}
completion:^(BOOL finished){
NSLog(#"Done!");
[UIView animateWithDuration:0.1
delay:0.0
options:UIViewAnimationCurveLinear
animations:^{
self.btnLogin.clipsToBounds = YES;
self.btnLogin.layer.cornerRadius =self.btnLogin.bounds.size.height/2.0f; //or use ( self.login.bounds.size.height/2);
self.btnLogin.layer.borderColor=[UIColor redColor].CGColor;
self.btnLogin.layer.borderWidth=2.0f;
[self.view layoutIfNeeded];
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
}];
}
Output :
How about this?
[UIView animateWithDuration:1.0 animations:^{
CGRect frame = btn.frame;
CGPoint center = btn.center;
frame.size.width = btn.frame.size.height;
frame.size.height = btn.frame.size.height;
btn.frame = frame;
btn.center = center;
btn.layer.cornerRadius = frame.size.width/2;
btn.clipsToBounds = YES;
}];
This is what i get
And after a 1 sec animation i get this
If you want to get circle,just follow simple way
-(IBAction)actionClick:(id)sender
{
[UIView animateWithDuration:0.7f
animations:^
{
[sender setAlpha:0.5f];
}
completion:^(BOOL finished)
{
[sender setAlpha:1.0f];
button.layer.cornerRadius = button.frame.size.width / 2;
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;
];
}
}

Moving UIView OFF the screen from left to right and bring it IN again from left

How can I Move an UIView OFF the screen from left to right and then bring it IN the screen again from left side ?!!
Thanks in advance,
All you have to do is animate the x origin position of the view and use the devices screen with to determine how much. So for off screen to the right:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_yourView.frame = CGRectMake(screenRect.size.width, _yourView.frame.origin.y, _yourView.frame.size.width, _yourView.frame.size.height);
}
completion:^(BOOL finished) {
//position screen left after animation
_yourView.frame = CGRectMake(-screenRect.size.width, _yourView.frame.origin.y, _yourView.frame.size.width, _yourView.frame.size.height);
}];
And for on screen go back to 0.0 x-position or whatever your starting point was:
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_yourView.frame = CGRectMake(0.0, _yourView.frame.origin.y, _yourView.frame.size.width, _yourView.frame.size.height);
}
completion:^(BOOL finished) {
//do something after animation
}];
To move OFF the screen:
CGRect frame = viewToMove.frame;
frame.origin.x = self.view.frame.size.width;
viewToMove.frame = frame;
To move the view back into the screen:
CGRect frame = viewToMove.frame;
frame.origin.x = 0;
viewToMove.frame = frame;
//Store your view's original rect
UIRect originRect = yourView.frame;
[UIView animateWithDuration:0.25 animations:^{
//create new rect and set its X coord to phone's width
CGrect rect = originRect;
rect.origin.x = self.view.frame.size.width;
yourView.frame = rect;
} completion:^(BOOL finished){
// when animation finished animating start new animation
//to bring your view to its original position
[UIView animateWithDuration:0.25 animation:^{
yourView.frame = originRect;
}];
}];
This code will move it to right in 0.3 seconds and then back from left to it's current position again in the next 0.3 seconds.
CGRect backToOriginal = Yourview.frame;
CGRect frameOnRight = CGRectMake ( screenWidth,backToOriginal.origin.y,backToOriginal.frame.size.width,backToOriginal.frame.size.height);
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ Yourview.frame = frameOnRight; } completion:^(BOOL finished)[UIView setFrame: screenLeft;] [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ Yourview.frame = backToOriginal; }{ }];
There must be errors in this code in terms of syntax since I am not sitting on a MAC right now.
//To move view to left
[UIView animateWithDuration:0.5
delay:1.0
options: UIViewAnimationCurveEaseOut
animations:^{
self.yourView.frame = CGRectMake(distanceToLeft, distanceFromTop, yourView.width, yourView.height);
}
completion:^(BOOL finished){
NSLog(#"Moved to left!");
}];
// To move back to original position
[UIView animateWithDuration:0.5
delay:1.0
options: UIViewAnimationCurveEaseOut
animations:^{
self.yourView.frame = CGRectMake(originalX, OriginalY, yourView.Width, yourView.height);
}
completion:^(BOOL finished){
NSLog(#"Back to normal!");
}];

how to use array value in UIView animation

In UIViewAnimation, so far i used a single image to animate from one place to another like this:
CGRect frame = button.frame;
CGRect frame1 = button1.frame;
frame.origin.x = frame1.origin.x;
frame.origin.y = frame1.origin.y;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 3.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView animateWithDuration:3.0 animations:^{
[button setTransform:CGAffineTransformIdentity];
} completion:^(BOOL finished) {
}];
button.frame = frame;
[UIView commitAnimations];
Here I have nine images in an array:
redAppleArray = #[redApple1,redApple2,redApple3,redApple4,redApple5,redApple6,redApple7,redApple8,redApple9,redApple10];
I have text display random numbers.
My question is how to get the above text value and animate the image using UIViewAnimation? i.e., if text =3, 3 redApple to animate .
To get the value from a label for example do:
int i = [[myLabel text] intValue];
And then use it to get the element in your array:
id *redApple = [redAppleArray objectAtIndex:i];
Try do something lilt this:
-(void)yourMethodToAnimateWithText:(NSString*)numberAsAText {
int = [numberAsAText intValue];
CGRect frame = button.frame;
CGRect frame1 = button1.frame;
frame.origin.x = frame1.origin.x;
frame.origin.y = frame1.origin.y;
// If you want to add some option use [UIView animateWithDuration:delay:option:animation:completion:]
[UIView animateWithDuration:3.0 animations:^{
//[button setTransform:CGAffineTransformIdentity];
button.frame = frame;
//Animate your images, text, etc.
} completion:^(BOOL finished) {
}];
}
Hope this is what you after.

UIView animation: simulating velocity

I am trying to use a UIView animation to simply move a view onto the screen:
[UIView animateWithDuration:.10 delay:0 options: nil animations:^
{
self.menusView.frame = endingMenuViewFrame;;
}
completion:^(BOOL finished)
{
}];
I'm wanting to add an animation so that UIView floats a little when it reaches the top before it comes down, i.e. akin to if someone jumps in the air - when they first jump, they shoot up quickly, but then gravity gradually slows them down as they reach the top of their jump, and eventually it pushes them back to earth. Does anyone know how to accomplish this?
Try with this code. This will make your view bounce three times with each time your height reduced by half. You may add more bounces.
CGFloat offset = 200.0;
CGRect originalFrame = self.menusView.frame;
[UIView animateWithDuration:1.0 animations:^{
CGRect frame = self.runnerAlertView.frame;
frame.origin.y = frame.origin.y - offset;
self.menusView.frame = frame;
} completion:^(BOOL finished){
[UIView animateWithDuration:1.0 animations:^{
self.menusView.frame = originalFrame;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.0 animations:^{
CGRect frame = self.menusView.frame;
frame.origin.y = frame.origin.y - 0.5 *offset;
self.menusView.frame = frame;
} completion:^(BOOL finished){
[UIView animateWithDuration:1.0 animations:^{
self.menusView.frame = originalFrame;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.0 animations:^{
CGRect frame = self.runnerAlertView.frame;
frame.origin.y = frame.origin.y - 0.25 * offset;
self.menusView.frame = frame;
} completion:^(BOOL finished){
[UIView animateWithDuration:1.0 animations:^{
self.menusView.frame = originalFrame;
}];
}];
}];
}];
}];
}];

Resources