简体   繁体   English

不起作用NSQueue

[英]Doesn't work NSQueue

I have a problem which I find it difficult to solve itself therefore I ask for your help. 我有一个很难解决的问题,因此请您帮忙。

in FirstViewController.m (FVC) 在FirstViewController.m(FVC)中

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

in SVC.h 在SVC.h中

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

in SVC.m 在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];
  }
 }

if i more than 1 that has to be created some threads and for everyone to be caused the blinkPic method, where the pictures(Pic) with specified number has to change 18 times the image - to blink. 如果我必须创建一个以上的线程,并且每个人都需要使用bickerPic方法,则具有指定编号的picture(Pic)必须更改图像的18倍-才能闪烁。

The problem - if the picture one (i=0), performance of the blinkPic method passes perfectly and the picture "blinks" 问题-如果图片一(i = 0),blinkPic方法的性能通过完美,图片“闪烁”

If pictures there is more (i=1 or 2 or 3 etc), the blinkPic method is executed always for the last picture, all the others remain without changes. 如果有更多图片(i = 1或2或3等),则始终对最后一张图片执行blinkPic方法,所有其他图片保持不变。 Why the blinkPic method isn't executed for all pictures when their more than one? 为什么当所有图片都不止一个时不对所有图片都执行blinkPic方法? But only for the last (( 但仅适用于最后((

 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];
  }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM