我希望当我点击一个按钮来移动一个视图到右边。 我写的东西,但它不工作;
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");
}];