Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 暂停UIImageView动画_Ios_Image_Animation - Fatal编程技术网

Ios 暂停UIImageView动画

Ios 暂停UIImageView动画,ios,image,animation,Ios,Image,Animation,我正在制作UIImageView动画,如下所示: //////progressbar start progressbar.animationImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"1.png"], [UIImage imageNamed:@"2.png"],

我正在制作UIImageView动画,如下所示:

//////progressbar start
progressbar.animationImages = [NSArray arrayWithObjects:
                               [UIImage imageNamed:@"1.png"],
                               [UIImage imageNamed:@"2.png"],
                               [UIImage imageNamed:@"3.png"],
                               [UIImage imageNamed:@"4.png"],
                               [UIImage imageNamed:@"5.png"],
                               [UIImage imageNamed:@"6.png"],
                               [UIImage imageNamed:@"7.png"],
                               [UIImage imageNamed:@"8.png"],
                               [UIImage imageNamed:@"9.png"],
                               [UIImage imageNamed:@"10.png"],
                               [UIImage imageNamed:@"11.png"],
                               [UIImage imageNamed:@"12.png"],
                               [UIImage imageNamed:@"13.png"],
                               [UIImage imageNamed:@"14.png"],
                               [UIImage imageNamed:@"15.png"],
                               [UIImage imageNamed:@"16.png"],
                               [UIImage imageNamed:@"17.png"],
                               [UIImage imageNamed:@"18.png"],
                               [UIImage imageNamed:@"19.png"],
                               [UIImage imageNamed:@"20.png"],
                               [UIImage imageNamed:@"21.png"],
                               [UIImage imageNamed:@"22.png"],
                               [UIImage imageNamed:@"23.png"],
                               [UIImage imageNamed:@"24.png"],
                               [UIImage imageNamed:@"25.png"],
                               [UIImage imageNamed:@"26.png"],
                               [UIImage imageNamed:@"27.png"],
                               [UIImage imageNamed:@"28.png"],
                               [UIImage imageNamed:@"29.png"],
                               [UIImage imageNamed:@"30.png"],
                               [UIImage imageNamed:@"31.png"],
                               [UIImage imageNamed:@"32.png"],
                               [UIImage imageNamed:@"33.png"],
                               [UIImage imageNamed:@"34.png"],
                               [UIImage imageNamed:@"35.png"],
                               [UIImage imageNamed:@"36.png"],
                               [UIImage imageNamed:@"37.png"],
                               [UIImage imageNamed:@"38.png"],
                               [UIImage imageNamed:@"39.png"],
                               [UIImage imageNamed:@"40.png"],
                               [UIImage imageNamed:@"41.png"],
                               [UIImage imageNamed:@"42.png"], nil];
progressbar.animationDuration = 5.00;
progressbar.animationRepeatCount = 0;
[progressbar startAnimating];
现在我需要暂停它,这样一旦用户单击按钮,图像将不会动画,并将停留在当前图像上

我从网上下载了这个代码来暂停它

  UIView *viewBeingAnimated = //your view that is being animated
viewBeingAnimated.frame = [[viewBeingAnimated.layer presentationLayer] frame];
[viewBeingAnimated.layer removeAllAnimations];
//when user unpauses, create new animation from current position.

那代码行吗?还有,我该如何从当前图像中取消暂停呢?

我也尝试过使用此代码,但没有成功

相反,您可以使用apple在以下链接中提供的代码暂停并恢复动画

要使CALayer传递给pauseLayer函数,可以使用以下代码

CALayer *player = progressbar.layer;
[self pauseLayer:player];
我的做法如下:

- (IBAction)play:(id)sender
{
pauseAnimation = FALSE;
[self performSelector:@selector(changeImages) withObject:nil afterDelay:0];
[self.pauseButton setHidden:NO];
[self.playButton setHidden:YES];

}

- (IBAction)pause:(id)sender
{
pauseAnimation = TRUE;
[self.pauseButton setHidden:YES];
[self.playButton setHidden:NO];
}
- (void)changeImages
{
if (currentFrame == self.selectedAttachment.noOfFrames) 
{

    currentFrame = -1;
    return; //Don't use return, if a looped playing is required.
}
if (currentFrame == -1) {
    currentFrame = 0;
}
if (pauseAnimation) 
{
    return;
}

[self performSelector:@selector(changeImages) withObject:nil afterDelay:.5]; 

imageView.image = [frameArray objectAtIndex:currentFrame];
currentFrame ++;    
}
甚至你可以在两者之间更改图像

- (IBAction)replaceImage:(id)sender
{
UIImage *replaceImage = [[UIImage alloc]init];
replaceImage = [UIImage imageNamed:@"Bluehound.gif"];
int i = arc4random()%25;
[frameArray replaceObjectAtIndex:i withObject:replaceImage];
}
谢谢大家。您现在可以播放、暂停、替换图像等。
*有一个错误:再次点击播放按钮会增加速度…:P

请尝试

停止动画制作

[progressBar stopAnimating];