将动画添加到从A点移动到B点的uiview

时间:2016-06-25 11:07:43

标签: ios objective-c uiview

我有一个动作,动作将UIView从A点移动到B点。如何添加UIView从A点移动到B点的漂亮过渡。我正在寻找让它从A点慢慢移动到点B.我怎么做?在移动项目的那一刻,我将框架设置为B点。

4 个答案:

答案 0 :(得分:2)

检查此代码并设置您想要的框架

[UIView animateWithDuration:0.75 animations:^{
    button.frame = CGRectMake(10, 70, 100, 100);
}];

答案 1 :(得分:2)

如果您正在使用自动布局(而且几乎可以肯定 - 这是默认设置),那么@PinkeshGjr的解决方案可能会出现问题。使用AutoLayout,您无法直接操作视图的框架。在移动框架后的某个时刻,视图的约束可以将其抢回。

相反,您希望创建控制要设置动画的属性/属性的约束(在这种情况下为x和y位置),并控制 - 从约束拖动到视图控制器的标题中以创建出口。

然后,您想要更改约束上的常量值,并在动画代码中调用layoutIfNeeded。像这样:

buttonXConstraint.constant = 100;
buttonYConstraint.constant = 100;

[UIView animateWithDuration:0.75 animations:^{
  [self.button layoutIfNeeded];
}];

上面的代码假设您创建了2个约束出口并将其称为buttonXConstraintbuttonYConstraint

答案 2 :(得分:0)

您可以使用animateKeyframesWithDurationanimateWithDuration:animations来实现这一目标,但这只是一个线性插值。如果你想对动画有更多的控制权,我建议你看看MTAnimation它真的是一个功能强大且易于使用的Lib来实现漂亮的动画

干杯

答案 3 :(得分:0)

用于动画视图的非常好的辅助方法。

- (void) animateView:(UIView *)view ToFrame:(CGRect) frame withDuration:(float) duration withDelay:(float) startDelay
{
    [UIView animateWithDuration:duration delay:startDelay options:0 animations:^{

        view.frame = frame;
    } completion:^(BOOL finished)
    {
        // Completion actions
    }];
}
相关问题