Animation 在终止之前,获取至少一个周期的UIImageView动画

Animation 在终止之前,获取至少一个周期的UIImageView动画,animation,uiimageview,nsoperation,uiactivityindicatorview,nsinvocationoperation,Animation,Uiimageview,Nsoperation,Uiactivityindicatorview,Nsinvocationoperation,我不熟悉Objective C和X-code,我很难让我的UIImageView动画在屏幕上停留至少一个完整的周期。我使用它作为一个自定义等待动画,当一个按钮被点击,一堆保存和处理的东西发生。我使用NSOpearationQueue在单独的线程中执行这些操作。以下是我代码的第一部分: 我的动画在ViewDidLoad中定义,称为“我的动画”。这是一个UIImageView动画,由一系列UIImages制作而成。我立即将其隐藏起来,并在需要时显示 -(void)viewDidLoad {

我不熟悉Objective C和X-code,我很难让我的UIImageView动画在屏幕上停留至少一个完整的周期。我使用它作为一个自定义等待动画,当一个按钮被点击,一堆保存和处理的东西发生。我使用NSOpearationQueue在单独的线程中执行这些操作。以下是我代码的第一部分:

我的动画在ViewDidLoad中定义,称为“我的动画”。这是一个UIImageView动画,由一系列UIImages制作而成。我立即将其隐藏起来,并在需要时显示

-(void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    NSArray *myAnimationArray  = [[NSArray alloc] initWithObjects:
                        [UIImage imageNamed:@"arisprite_0000.png"],
                        [UIImage imageNamed:@"arisprite_0001.png"],
                        [UIImage imageNamed:@"arisprite_0002.png"],
                        [UIImage imageNamed:@"arisprite_0003.png"],
                        [UIImage imageNamed:@"arisprite_0004.png"],
                        [UIImage imageNamed:@"arisprite_0005.png"],
                        [UIImage imageNamed:@"arisprite_0006.png"],
                        [UIImage imageNamed:@"arisprite_0007.png"],
                        [UIImage imageNamed:@"arisprite_0008.png"],
                        [UIImage imageNamed:@"arisprite_0009.png"],
                        [UIImage imageNamed:@"arisprite_0010.png"],
                        [UIImage imageNamed:@"arisprite_0011.png"],
                        [UIImage imageNamed:@"arisprite_0012.png"],
                        [UIImage imageNamed:@"arisprite_0013.png"],
                        [UIImage imageNamed:@"arisprite_0014.png"],
                        [UIImage imageNamed:@"arisprite_0015.png"],
                        [UIImage imageNamed:@"arisprite_0016.png"],
                        [UIImage imageNamed:@"arisprite_0017.png"],
                        [UIImage imageNamed:@"arisprite_0018.png"],
                        [UIImage imageNamed:@"arisprite_0019.png"],
                        [UIImage imageNamed:@"arisprite_0020.png"],
                        [UIImage imageNamed:@"arisprite_0021.png"],
                        [UIImage imageNamed:@"arisprite_0022.png"],
                        [UIImage imageNamed:@"arisprite_0023.png"],
                        [UIImage imageNamed:@"arisprite_0024.png"],
                        [UIImage imageNamed:@"arisprite_0025.png"],
                        [UIImage imageNamed:@"arisprite_0026.png"],
                        [UIImage imageNamed:@"arisprite_0027.png"],
                        [UIImage imageNamed:@"arisprite_0028.png"],
                        [UIImage imageNamed:@"arisprite_0029.png"],
                        [UIImage imageNamed:@"arisprite_0030.png"],
                        [UIImage imageNamed:@"arisprite_0031.png"],
                        [UIImage imageNamed:@"arisprite_0032.png"],
                        [UIImage imageNamed:@"arisprite_0033.png"],
                        [UIImage imageNamed:@"arisprite_0034.png"],
                        [UIImage imageNamed:@"arisprite_0035.png"],
                        [UIImage imageNamed:@"arisprite_0036.png"],
                        [UIImage imageNamed:@"arisprite_0037.png"],
                               nil];


myAnimation.animationImages = myAnimationArray;
myAnimation.animationDuration = 2.5;
    [myAnimation.layer setBorderColor:[[UIColor blackColor] CGColor]];
    [myAnimation.layer setBorderWidth:2.0];

[self.view addSubview:myAnimation];
    [myAnimation setHidden:YES];
...
...
...
}

-(IBAction)saveToYYY:(id)sender
{
// hide current view and show save screen
[self showAnimatedSaveScreen];

// show the saving screen
[self.view bringSubviewToFront:savingBackgroundView];
[self.view bringSubviewToFront:savingText];
[self.view bringSubviewToFront:myAnimation];


/* Operation Queue init */
NSOperationQueue *queue = [NSOperationQueue new];

/* Create our NSInvocationOperation to call save code, passing in nil */
NSInvocationOperation *saveToYYYOperation = [[NSInvocationOperation alloc]
                                    initWithTarget:self
                                    selector:@selector(saveToYYYWithOperation)
                                    object:nil];

NSInvocationOperation *saveToXXXOperation = [[NSInvocationOperation alloc]
                                                  initWithTarget:self
                                                       selector:@selector(saveToXXXWithOperation)
                                                  object:nil];

/* Add the operation to the queue */
[saveToXXXOperation addDependency:saveToYYYOperation];
[queue addOperation:saveToYYYOperation];
[queue addOperation:saveToXXXOperation];    
}
然后,在我的最后一个队列操作中

-(void)saveToXXXWithOperation
{
... 
a bunch of processing
...

[self performSelectorOnMainThread:@selector(hideAnimatedSaveScreen) withObject:nil     waitUntilDone:YES];
}
一切似乎都很正常,只是当这些保存操作太快时,尤其是在iPad上,myAnimation无法完成其2.5的持续时间。我想在隐藏动画之前至少播放一次。有没有办法实现这一点?

如果问题是“在完成保存后如何等待2.5秒”,只需让您的
hideAnimatedSaveScreen
实现使用延迟性能(
性能选择器:withObject:afterDelay:
)。您可以使用时间戳(
[NSDate date]
)更改该数量,了解动画开始后实际经过的时间,并根据需要进行减法


如果问题是“我如何知道我的动画何时完成”,这很难说,因为您的代码中实际上没有显示任何动画,所以当您说您有UIImageView动画时,无法猜测您的意思。

您实际上没有在代码中显示任何UIImageView或任何动画,所以很难回答。添加到定义myAnimation的位置(viewDidLoad)。现在我的问题更清楚了吗?如果这是你要做的动画,那么我认为我上面的答案是好的。当我们到达图像数组的末尾时,简单的UIImageView图像动画不会发出任何信号。当然,如果你做的是真正的动画,你会有更多的灵活性和力量。太好了,谢谢。我是新来的,正在学习和探索,所以在我的原型应用程序中,这种简单的动画就足够了。对于我的应用程序的最终版本,我想探讨一下你的建议。你是在暗示我应该研究“核心动画”吗?不是特别的,只是一般的真实动画。我写了一本书可能会对你有所帮助:(重点是当动画结束时,真正的动画确实会给你一个信息,这才是你真正想要的。)