2012-02-01 110 views

回答

2

如何暫停和恢復的UIView動畫:

這將介紹如何做到以上無阻擋的動畫。 (人們仍然希望支持3.0以上)。

這隻允許在動畫達到設定位置後暫停。關於如何在動畫中的中間暫停的動畫,我使用CALayers建議這樣:

CALayer* myLayer = [self.myUIView.layer presentationLayer]; 
CGRect frameStop = myLayer.frame; 
double pausedX = frameStop.origin.x; 
double pausedY = frameStop.origin.y; 

現在的實際如何:

startAnimation = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    startAnimation.titleLabel.font = [UIFont systemFontOfSize:22]; 
    startAnimation.titleLabel.lineBreakMode = UILineBreakModeHeadTruncation; 
    [startAnimation setTitle:(@"pause") forState:UIControlStateNormal]; 
    [startAnimation addTarget:self action:@selector(doAnimation) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:startAnimation]; 



-(void)doAnimation{ 
    if (bicyclePad.animationPause == false){ 
     [restartAnimation setTitle:(@"Resume") forState:UIControlStateNormal]; 
     [bicyclePad pauseAnimation]; 
    } else { 
     [restartAnimation setTitle:(@"Pause") forState:UIControlStateNormal]; 
     [bicyclePad resumeAnimation]; 
    } 
} 

-(void)resumeAnimation{ 
    bicyclePad.animationPause = false; 
    [restartAnimation setTitle:(@"Resume") forState:UIControlStateNormal]; 
    objectMotion = [[yourUIView alloc]initWithFrame:CGRectMake((*yourPathPoints)[0].x, (*yourPathPoints)[0].y, 8, 8)]; 
    [self.view addSubview:objectMotion]; 
    [self animateBike:nil finished:YES context:nil]; 


} 

-(void)pauseAnimation{ 
    bicyclePad.animationPause = true; 

    [restartAnimation setTitle:(@"Pause") forState:UIControlStateNormal]; 
    [bicyclePad doAnimation]; 
} 

-(void)animateObject:(NSString*)animationID finished:(BOOL)finished context:(void*)context { 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    //below suggesting your animation loops 
    if (animationPause == true) 
     [UIView setAnimationDidStopSelector:@selector(animateStop:finished:context:)]; 
    else 
    [UIView setAnimationDidStopSelector:@selector(animateObject:finished:context:)]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationCurve: UIViewAnimationCurveLinear]; 
    [UIView setAnimationDuration:yourDelay]; 
    [UIView commitAnimations]; 
    animationPause == false; 

} 

-(void)animateStop:(NSString*)animationID finished:(BOOL)finished context:(void*)context{ 
} 
+0

'animationPause ==虛假;'和'animationPause == true;'比較,不做任何事情。您可能想要分配值。 – DarkDust 2014-05-16 14:46:00

+0

恐怕你沒有。你用另一個比較取代了一個比較。 'bicyclePad.animationPause == false;'仍然是一個比較,結果被扔掉。你想'bicyclePad.animationPause = false;'。注意'=='與'='。 – DarkDust 2014-05-17 12:38:13

+0

啊好的。我想知道你的第一條評論。是的,這個代碼大約2歲。感謝您指出了這一點。 @DarkDust – 2014-05-19 15:42:27