混合NSThreads和NSTimer来更新视图

时间:2011-02-18 10:29:41

标签: iphone cocoa-touch nstimer nsthread

我想创建一个应用程序,以便在屏幕上显示和消失圆圈。在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;
            }
    }

1 个答案:

答案 0 :(得分:0)

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