2014-05-14 84 views
0

我有一個CAKeyframeAnimation,當我從屏幕左側滑動手指(iOS 7中的向後滑動手勢)時,我想繼續玩。目前,當我向後滑動時,它會跳轉到它的startPosition,並且它保持靜態,並且看起來相當sl。。保持動畫播放ViewController被解僱?

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(我沒有使用故事板的方式)。在這個UIView裏面是一個上下移動的UIButton。因此,UIView作爲一種「窗口」來看UIButton上下移動。當我滑回去,並且隨着detailViewController向右移動,我希望UIButton繼續進行動畫。 (在tableView之上,我的意思是它位於一個tableView之上,它不在它的頂層)。

+0

你能描述一下你試圖完成的效果嗎?如果你只是試圖讓UIView在屏幕上滑動,那麼你可以以最難的方式做到這一點。 – InkGolem

+0

我有一個桌面上的UIView(我沒有使用故事板的方式)。在這個UIView裏面是一個上下移動的UIButton。因此,UIView作爲一種「窗口」來看UIButton上下移動。當我滑回去,並且隨着detailViewController向右移動,我希望UIButton繼續進行動畫。 – user3127576

+0

(在tableView之上,我的意思是它位於tableView之上,它不在它的頂層)。 – user3127576

回答

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; 
} 
+0

在我的.h,我把: 「@interface albumDetailViewController:的UIViewController { CADisplayLink * DisplayLink的; BOOL moveButtonDown; }」 ,然後我複製你的代碼(並取消了對_ DisplayLink的)但它不起作用。我做錯了什麼?謝謝 – user3127576

+1

我不確定 - 這段代碼適用於那些在類擴展中聲明的ivars。你從哪裏調用'-initiateButtonAnimation'?在這樣做之前,您是否正確設置了myButton的初始位置? – followben

+0

哦,不,我修理它,這是我的一個愚蠢的錯誤。最後一件事 - 我如何改變它的速度?現在它移動得非常快。非常感謝你的幫助! – user3127576

1
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 
}]; 

這可能是完成動畫的最簡單方法。你可能不得不擺弄不同的動畫選項,但這是你想要的東西的要點。

+0

這使得按鈕向左移動,然後向右,然後跳轉到一個位置。另外,當我在短小的動畫中滑過時,它就像以前一樣停下來。當我滑回來時,如何使其循環並繼續運行? – user3127576