2011-02-18 60 views
0

我想創建一個應用程序,使屏幕上出現和消失的圈子。 TouchesBegan上的圓圈放大,touchesEnd上的圓圈更小。 我可以在一個圓上做到這一點,但我想在用戶觸摸屏幕的任何地方做到這一點。 我知道我必須使用NSThreads,但我的示例不起作用。混合NSThreads和NSTimer更新視圖

這一塊我的代碼:

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

回答

0

你並不需要一個輔助線程來更新視圖。只需使用延遲迴調(NSTimer,以主線程爲目標)使必須繪製的區域無效,並跟蹤最近觸摸的位置,以便知道將繪圖居中的位置。 rect失效回調將使用延遲迴調進行合併,這是一項獎勵。

+0

但是沒有其他線程,如何可能幾個圓可以同時變小? – mickcoco 2011-02-18 10:55:52