gpt4 book ai didi

ios - 在关闭 ViewController 时继续播放动画?

转载 作者:行者123 更新时间:2023-11-28 22:07:17 33 4
gpt4 key购买 nike

我有一个 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 之上。它不是在 tableView 之上分层)。

最佳答案

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

相反,我会使用 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;
}

关于ios - 在关闭 ViewController 时继续播放动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23655824/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com