NSQueue不起作用

时间:2014-02-26 11:04:47

标签: iphone objective-c

我遇到了一个问题,我觉得很难解决,所以我请求你的帮助。

在FirstViewController.m(FVC)中

if (..............) {
   [self performSegueWithIdentifier:@"SVC" sender:self];//SecondViewController
}

在SVC.h中

......
- (void)blinkPic;
......

在SVC.m

- (void)viewDidLoad
{
[super viewDidLoad];

 NSOperationQueue *queue = [[NSOperationQueue alloc] init];

 .......
 for (int i = 0; i < _picForDisplay.length; i++) { //picForDisplay can be from 0 to 10

 _forBlink = ......; // number of picture

   NSOperation *operation = [[NSInvocationOperation alloc]
                            initWithTarget:self
                            selector:@selector(blinkPic)
                            object:nil];

   [queue addOperation:operation];
 }
 .......
 }
 - (void)blinkPic
 {
   for (int i = 0; i < 18; i++) {

    if ( (i % 2 ) == 0 ){
        UIImageView *Pic = [self valueForKey:[NSString stringWithFormat:@"Pic%i",         _forBlink]];
        [Pic performSelectorOnMainThread:@selector(setImage:) withObject:[UIImage imageNamed:@"1.png"] waitUntilDone:YES];

    } else {
        UIImageView *Pic = [self valueForKey:[NSString stringWithFormat:@"Pic%i", _forBlink]];
        [Pic performSelectorOnMainThread:@selector(setImage:) withObject:[UIImage imageNamed:@"2.png"] waitUntilDone:YES];
    }

    [NSThread sleepForTimeInterval:0.1f];
  }
 }

如果超过1必须创建一些线程并且每个人都要引起blinkPic方法,其中指定数字的图片(Pic)必须改变图像的18倍 - 闪烁。

问题 - 如果图片一(i = 0),blinkPic方法的性能完美通过,图片“闪烁”

如果图片有更多(i = 1或2或3等),则始终为最后一张图片执行blinkPic方法,所有其他图片保持不变。 为什么不对所有图片执行blinkPic方法? 但仅限于最后一次((

1 个答案:

答案 0 :(得分:0)

 for (int i = 0; i < _picForDisplay.length; ++i) { //picForDisplay can be from 0 to 10
   NSOperation *operation = [[NSInvocationOperation alloc]
                            initWithTarget:self
                            selector:@selector(blinkPic:)
                            object:@(i)];

   [queue addOperation:operation];
 }
 .......
 }
 - (void)blinkPic:(NSNumber*) forBlink
 {
   for (int i = 0; i < 18; i++) {

    if ( (i % 2 ) == 0 ){
        UIImageView *Pic = [self valueForKey:[NSString stringWithFormat:@"Pic%@", forBlink]];
        [Pic performSelectorOnMainThread:@selector(setImage:) withObject:[UIImage imageNamed:@"1.png"] waitUntilDone:YES];

    } else {
        UIImageView *Pic = [self valueForKey:[NSString stringWithFormat:@"Pic%@", forBlink]];
        [Pic performSelectorOnMainThread:@selector(setImage:) withObject:[UIImage imageNamed:@"2.png"] waitUntilDone:YES];
    }

    [NSThread sleepForTimeInterval:0.1f];
  }
 }