简体   繁体   中英

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)

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

in SVC.h

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

in 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.

The problem - if the picture one (i=0), performance of the blinkPic method passes perfectly and the picture "blinks"

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. Why the blinkPic method isn't executed for all pictures when their more than one? 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];
  }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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