【问题标题】:Mixing NSThreads and NSTimer to update a view混合 NSThreads 和 NSTimer 来更新视图
【发布时间】:2011-06-29 17:41:56
【问题描述】:

我想制作一个应用程序以在屏幕上显示和消失圆圈。圆圈在 TouchesBegan 上放大,在 touchesEnd 上变小。 我可以在一个圈子上做到这一点,但我想在用户触摸屏幕的任何地方都这样做。 我知道我必须使用 NSThreads,但我的示例不起作用。

这是我的一段代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    lastPoint = [touch locationInView:self.view];
    zoomIn = TRUE;
    rayonCercle = 25;

    //Creation d'un thread
    timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(cycle)         object:nil];
    [timerThread start];

    if ([touch tapCount] == 1) {
            NSLog(@"une touche");
    }

    if ([touch tapCount] == 2) {
            drawImage.image = nil;
            return;
    }
}

    - (void) cycle {

        NSLog(@"cycle");

        NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
        NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
        time = [NSTimer scheduledTimerWithTimeInterval: 0.1
        target: self
        selector: @selector(drawCircle:)
        userInfo: nil
        repeats: YES];

    [runLoop run];
    [pool release];
}

    - (void) drawCircle:(NSTimer *)timer {

            NSLog(@"drawCircle");

            UIGraphicsBeginImageContext(self.view.frame.size);
            [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width,         self.view.frame.size.height)];


            CGGradientRef myGradient;
            CGColorSpaceRef myColorSpace;
            size_t locationCount = 3;
            CGFloat locationList[] = {0.0, 0.5, 1.0};
            CGFloat colorList[] = {
            1.0, 0.0, 0.5, 1.0, //red, green, blue, alpha
            1.0, 0.0, 1.0, 1.0,
            0.3, 0.5, 1.0, 1.0
            };
            myColorSpace = CGColorSpaceCreateDeviceRGB();
            myGradient = CGGradientCreateWithColorComponents(myColorSpace, colorList,
            locationList, locationCount);

            CGContextDrawRadialGradient(UIGraphicsGetCurrentContext(), myGradient, lastPoint, 0,                 lastPoint,rayonCercle, 0);   

            drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();

            if (rayonCercle < 200 && zoomIn == TRUE) {
            _hue += 0.5;
            _brightness += 0.005;
            _saturation += 0.05;
            rayonCercle += 15;
            }
            if (zoomIn == FALSE) {
            if (rayonCercle < 0) {
            [time invalidate];
            [timerThread release];
            } else {
            _hue -= 0.5;
            _brightness -= 0.005;
            _saturation -= 0.05;
            rayonCercle -= 1;
            }
    }
}


    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
            UITouch *touch = [touches anyObject];   
            CGPoint currentPoint = [touch locationInView:self.view];
            lastPoint = currentPoint;

            _hue = 0.0;
            _saturation = 0.0;
            _brightness = 0.0;

            rayonCercle = 25;
            zoomIn = TRUE;
    }

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

            _hue = 0.0;
            _saturation = 0.0;
            _brightness = 0.0;

            zoomIn = FALSE;

            UITouch *touch = [touches anyObject];

            if ([touch tapCount] == 2) {
                    drawImage.image = nil;
                    return;
            }
    }

【问题讨论】:

    标签: iphone cocoa-touch nstimer nsthread


    【解决方案1】:

    您不需要辅助线程来更新视图。只需使用延迟回调(NSTimer,针对主线程)来使您必须绘制的区域无效,并跟踪最近触摸的位置,以便您知道将绘图居中的位置。 rect 失效回调将使用延迟回调合并,这是一个奖励。

    【讨论】:

    • 但是没有其他线程,怎么可能同时变小几个圆圈呢?
    • 您可以随着时间的推移管理事件序列。当 drawRect 被调用时,你根据绘制调用的时间(例如,当前时间)绘制什么。所以你需要跟踪多个事件的事件类型、时间、触摸位置和......(就是这样?)。使用此信息,您将知道要绘制什么、绘制什么尺寸以及事件发生多长时间(例如,如果您想淡出)。我希望这会有所帮助。
    • 对不起,我做不到它的工作原理:(我试图建立一个我想显示的所有形状的数组(在一个计时器中),在另一个计时器中,我浏览我的数组来绘制每个形状。但它没有工作......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多