我想在单击按钮时将一个视图移动到右侧。 我写了一些东西,但它不起作用;
UIView* view = [self.view viewWithTag:100];
if (!view) {
NSLog(@"nil");
}
[UIView animateWithDuration:0.3f
animations:^{
[view setTransform:CGAffineTransformMakeTranslation(-100, 0)];
}
completion:^(BOOL finished){
}
];
答案 0 :(得分:21)
试试这段代码;
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");
}];
答案 1 :(得分:2)
这样做
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){
}
];
快乐编码:)
答案 2 :(得分:2)
您还可以使用像这样的平滑动画移动视图
- (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");
}];