2014-05-07 65 views
1

我使用下面的代码创建动画暂停图像:我想从一个数组

NSMutableArray *dashBoy = [NSMutableArray array]; 
for (int i = 1; i<= 20; i++) { 
    butterfly = [NSString stringWithFormat:@"5c_%d.jpg", i]; 
    if ((image = [UIImage imageNamed:butterfly])) 
     [dashBoy addObject:image]; 
    } 

    [stgImageView setAnimationImages:dashBoy]; 
    [stgImageView setAnimationDuration:7.0f]; 
    [stgImageView setAnimationRepeatCount:-1]; 
    [stgImageView startAnimating]; 

我的要求是如果dashBoy是5c_10.jpg,然后暂停图像进行约5秒,并然后恢复动画,如果dashBoy是5c_20.jpg,则再次暂停图像约5秒钟并重新恢复。可能吗?

回答

2

您不能使用像这样的UIImageView动画变速。

虽然有几种方法。

1.自己动手。

为此,您可以使用类似NSTimer的东西,并让它反复激发一种方法来为您更改图像。使用此可以每个时间图像单独地(你甚至可以创建包含的图像和时间的长度来显示它,然后创建与这些阵列的数据对象。

2.操纵当前方法。

如果你有20幅图像,你要告诉他们所有的7秒钟,然后这就是......每幅图像0.35秒。所以5秒的暂停约14图像相当。

因此,而不是将每个图像一次您可以添加1-9一次,然后添加10-14次。11-19一次,然后20-14次。

然后它会像它正在做的那样交换,但是当它达到10时,它会将它交换为同一图像的另一个副本,以便它看起来像暂停。

然后,您必须将持续时间...增加到17秒,以获得每个图像的相似持续时间。

我该怎么办?

虽然它听起来像是一个黑客(因为它),我想我会给第二个方法先去。工作起来要容易得多,所以如果失败了,你还没有花很长时间来完成工作。

第一种方法是设置更多的工作,但会允许更好地控制动画。

快速的方法1

创建一个对象像脏例子...

MyTimedImage 
------------ 
UIImage *image 
CGFloat duration 

因此,例如...

// probably want this as a property 
NSMutableArray *timedImages = [NSMutableArray array]; 

MyTimedImage *timedImage = [MyTimedImage new]; 
timedImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"5c_%d.jpg", i]]; 
timedImage.duration = 0.4; 

[timedImages addObject:timedImage]; 

然后你想办法显示他们...

//set up properties something like this... 
@property (nonatomic, assign) NSInteger currentIndex; 
@property (nonatomic, assign) BOOL paused; 
@property (nonatomic, assign) NSTimer *imageTimer; 

- (void)displayNextImage 
{ 
    if (self.paused) { 
     return; 
    } 

    NSInteger nextIndex = self.currentIndex + 1; 

    if (nextIndex == [self.timedImages count]) { 
     nextIndex = 0; 
    } 

    MyTimedImage *nextImage = self.timedImages[nextIndex]; 

    self.currentIndex = nextIndex;  

    self.imageView.image = nextImage.image; 

    self.imageTimer = [NSTimer scheduledTimerWithInterval:nextImage.duration target:self selector:@selector(displayNextImage) userInfo:nil repeats:NO]; 
} 

使用您公开的属性可以暂停按钮按下(例如)当前图像上的图像视图。

要启动该过程,只需运行[self displayNextImage];,您也可以在图像循环中的任意位置启动。

+0

你可以举例说明第一种方法 – user3571918

+0

编辑我的答案。 – Fogmeister

1

使用UIImageView动画,这是不可能的,您可能需要创建自己的动画逻辑,如加载数组中的所有UIImages并通过计时器,切换到下一个动画帧,并且当您希望暂停时,使计时器失效。

相关问题