I want to move one view to right when I clicked a button. I wrote something but it's not working;
UIView* view = [self.view viewWithTag:100];
if (!view) {
NSLog(@"nil");
}
[UIView animateWithDuration:0.3f
animations:^{
[view setTransform:CGAffineTransformMakeTranslation(-100, 0)];
}
completion:^(BOOL finished){
}
];
try this code;
UIView* view = [self.view viewWithTag:100];
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = view.frame;
frame.origin.y = 0;
frame.origin.x = (-100);
view.frame = frame;
}
completion:^(BOOL finished)
{
NSLog(@"Completed");
}];
You can also move your view with smooth animation like this
- (void)moveViewPosition:(CGFloat)xPosition forView:(UIView *)view
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[view setFrame:CGRectMake(xPosition, view.frame.origin.y,view.frame.size.width, view.frame.size.height)];
[UIView commitAnimations];
}
and call it like this
[self moveViewPosition:120.0f forView:yourView];
for more you can also modify it to pass your require duration on function call like this.
- (void)moveViewPosition:(CGFloat)xPosition forView:(UIView *)view withDuration:(float)duration;
there are other options like
UIView* view = [self.view viewWithTag:99999];
[UIView animateWithDuration:0.9
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = view.frame;
frame.origin.y = 68;
frame.origin.x = 0;
view.frame = frame;
}
completion:^(BOOL finished)
{
NSLog(@"Completed");
}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With