我是 Objective C 和 X 代码的新手,我无法让我的 UIImageView 动画在屏幕上停留至少一个完整的周期。我将它用作自定义等待动画,用于点击按钮并发生一堆保存和处理的事情。我使用 NSOperationQueue 在单独的线程中执行这些操作。这是我的代码的第一部分:
我的动画是在 ViewDidLoad 中定义的,它被称为“myAnimation”。它是一个 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 的持续时间。在隐藏动画之前,我想至少播放一次。有没有办法实现这个?