【问题标题】:Keep Animation Playing As ViewController is Dismissed?关闭 ViewController 时保持动画播放?
【发布时间】:2014-05-14 13:14:36
【问题描述】:

我有一个CAKeyframeAnimation,我想在我从屏幕左侧滑动手指时继续播放它(iOS 7 中的向后滑动手势)。目前,当我向后滑动时,它会跳到它的 startPosition 并保持静止,并且看起来相当草率。

ViewController 是我从另一个ViewController 推送的detailViewController。当然,我不希望在后台关闭detailViewController 后播放动画。就在被解雇的时候。

这是我的动画:

CGPoint startPoint = (CGPoint){160.f, 160.f};
CGPoint endPoint = (CGPoint){160.f, 15.f};

CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, startPoint.x, startPoint.y);
CGPathAddLineToPoint(thePath, NULL, endPoint.x, endPoint.y);

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.duration = 20.f;
animation.path = thePath;
animation.autoreverses = YES;
animation.repeatCount = INFINITY;
[self.myButton.layer addAnimation:animation forKey:@"position"];

任何帮助将不胜感激,谢谢!

使用模式详细信息进行编辑:我在 TableView 顶部有一个 UIView(顺便说一下,我没有使用 Storyboard)。在这个 UIView 里面是一个可以上下移动的 UIButton。因此,UIView 充当了查看 UIButton 上下移动的一种“窗口”。当我向后滑动时,并且 detailViewController 向右移动时,我希望 UIButton 继续按原样制作动画。 (在 tableView 之上,我的意思是它位于 tableView 之上。它不是分层的)。

【问题讨论】:

  • 你能描述一下你想要达到的效果吗?如果您只是想让 UIView 在屏幕上滑动,那么您这样做是最困难的。
  • 我在 TableView 之上有一个 UIView(顺便说一下,我没有使用 Storyboards)。在这个 UIView 里面是一个可以上下移动的 UIButton。因此,UIView 充当了查看 UIButton 上下移动的一种“窗口”。当我向后滑动,并且 detailViewController 向右移动时,我希望 UIButton 继续保持动画效果。
  • (在tableView之上,我的意思是它位于tableView之上。它不是分层的)。
  • 您是否正在寻找“动画”以与关闭过渡同步移动和/或过渡是否具有交互性?
  • 它与解雇完全不同步——事实上,在大多数 iOS 7 应用程序中,“解雇”只是一个常规的向后滑动手势。我只想让动画继续播放,直到 detailViewController 完全看不见 :) 希望这不会太难做?

标签: ios caanimation


【解决方案1】:

CAAnimations 仅更新按钮的表示层,而不是实际位置。在导航控制器的弹出过渡期间,我不知道有什么方法可以要求他们继续。

相反,我会使用NSTimerCADisplayLink 为实际按钮位置设置动画。一个简单的例子:

// in MyViewController.m

static CGFloat kButtonSpeed = 0.2f;
static CGFloat kButtonMinY = 15.f;
static CGFloat kButtonMaxY = 160.f;

- (void)initiateButtonAnimation
{
    if (_displayLink) {
        return;
    }

    _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(moveButton)];
    [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)moveButton
{
    if (_frameTimestamp == 0) {
        _frameTimestamp = [_displayLink timestamp];
        return;
    }

    if (!_renderFrame) {
        _renderFrame = !_renderFrame;
        return;
    }

    double currentTime = [_displayLink timestamp];
    double renderTime = currentTime - _frameTimestamp;
    _frameTimestamp = currentTime;
    CGFloat amount = renderTime * 60.f * kButtonSpeed;

    CGFloat y = self.myButton.center.y;

    if (!_moveButtonDown && y > kButtonMinY) {
        y = y - amount;
    }
    else if (_moveButtonDown && y < kButtonMaxY)
        y = y + amount;
    else
        _moveButtonDown = !_moveButtonDown;

    self.myButton.center = CGPointMake(self.myButton.center.x, y);
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    // don't forget to invalidate & release
    [_displayLink invalidate];
    _displayLink = nil;
}

【讨论】:

  • 在我的 .h 中,我输入:“@interface albumDetailViewController : UIViewController { CADisplayLink *displayLink; BOOL moveButtonDown; }” 然后我复制了您的代码(并删除了 displayLink 的 _ ) 但它不起作用。我做错什么了?谢谢
  • 我不确定 - 此代码适用于我在类扩展中声明的那些 ivars。你从哪里打电话给-initiateButtonAnimation?在这样做之前你是否正确设置了 myButton 的初始位置?
  • 哦,不,我修好了,我犯了一个愚蠢的错误。最后一件事 - 我如何改变它的速度?现在它移动得相当快。非常感谢您的帮助!
  • 我已经修改了代码来展示如何调整移动速度。如果它适合您,请将答案标记为已接受。我还建议您熟悉使用 CADisplayLink 和 NSRunLoop 为对象设置动画的一般概念。任何关于 iOS 游戏开发或核心动画的好书都应该涵盖这一点。
  • 不确定 - 代码示例对我来说足够好用:dropbox.com/sh/r9csgx8u4wlq0ed/AAAmerf5MUuoaxlMyK3Hs6HJa
【解决方案2】:
UIButton* button = [[UIButton alloc]initWithFrame:CGRectMake(160, 0, 320, 540)];
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction animations:^{
    button.frame = CGRectMake(160, 10, 320, 540);
} completion:^(BOOL finished) {
    // nothing to see here
}];

这可能是完成动画的最简单方法。您可能不得不摆弄不同的动画选项,但这是您想要的要点。

【讨论】:

  • 这使按钮向左移动,然后向右移动,然后跳转到一个位置。另外,当我在它的短动画中向后滑动时,它就像以前一样停止了。当我向后滑动时,如何让它循环并继续运行?
猜你喜欢
  • 2018-05-23
  • 1970-01-01
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 2022-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多