繁体   English   中英

不起作用NSQueue

[英]Doesn't work NSQueue

我有一个很难解决的问题,因此请您帮忙。

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

如果我必须创建一个以上的线程,并且每个人都需要使用bickerPic方法,则具有指定编号的picture(Pic)必须更改图像的18倍-才能闪烁。

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

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

 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