iPhone视图移动动画
我想点击一个按钮时将一个视图移动到右边。 我写了一些东西,但它不起作用;
UIView* view = [self.view viewWithTag:100]; if (!view) { NSLog(@"nil"); } [UIView animateWithDuration:0.3f animations:^{ [view setTransform:CGAffineTransformMakeTranslation(-100, 0)]; } completion:^(BOOL finished){ } ];
试试这段代码;
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"); }];
像这样做 。
leftFrame是您可以开始的框架,右框架是您要移动到的框架
UIView* view = [self.view viewWithTag:100]; if (!view) { NSLog(@"nil"); } [vw setFrame:leftFrame]; [UIView animateWithDuration:0.3f animations:^{ [vw setFrame:rightFrame]; } completion:^(BOOL finished){ } ];
快乐编码:)
您也可以使用这样的平滑动画移动视图
- (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]; }
并称之为这样
[self moveViewPosition:120.0f forView:yourView];
对于更多,你也可以修改它以在这样的函数调用上传递你的需要持续时间。
- (void)moveViewPosition:(CGFloat)xPosition forView:(UIView *)view withDuration:(float)duration;
还有其他选择
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"); }];