2012-11-20 59 views
1

這裏是我的UIPanGestureRecognizerIOS畫出簡單的線條

- (void)SwipeHandle:(UIPanGestureRecognizer*)gestureRecognizer 
{ 
UIView *theSuperview = self.numberview; 
CGPoint touchPointInSuperview = [gestureRecognizer locationInView:theSuperview]; 

float gapX = image1.frame.size.width/8; 
float gapY = image1.frame.size.height/8.48; 

if(gestureRecognizer.state == UIGestureRecognizerStateBegan) 
{ 
    if(!ispause && [time.text intValue] > 0){ 
     if(!isbegan && !isended){ 
      for(int i = 1; i <= 16; i++) 
      { 
       UIView *imageview = [self.numberview viewWithTag:i]; 
       if (CGRectContainsPoint(imageview.frame, touchPointInSuperview)) 
       { 
        isbegan = YES; 
        isreverse = NO; 
        if([[ischose objectAtIndex:i-1] boolValue] == 0) 
        { 
         currentposition = imageview.tag; 
         positionvalue += pow(i, 3); 
         currentanswer += [self converter:[NSString stringWithFormat:@"%@", [allimagenumbers substringWithRange:NSMakeRange(i-1, 1)]]]; 
         [ischose replaceObjectAtIndex:i-1 withObject:[NSNumber numberWithBool:YES]]; 
         [self changeimage:@"selected"]; 
        } 

        previouspoint = imageview.frame.origin; 
        break; 
       } 
      } 
     } 
    } 
} 
else if(gestureRecognizer.state == UIGestureRecognizerStateChanged) 
{ 
    if(isbegan && !isended) 
    { 
     if(touchPointInSuperview.x >= 0 && touchPointInSuperview.x <= self.numberview.frame.size.width && touchPointInSuperview.y >= 0 && touchPointInSuperview.y <= self.numberview.frame.size.height) 
     { 
      for(int i = 1; i <= 16; i++) 
      { 
       UIImageView *imageview = (UIImageView*)[self.numberview viewWithTag:i]; 
       if (CGRectContainsPoint(imageview.frame, touchPointInSuperview)) 
       { 
        if((touchPointInSuperview.x >= imageview.frame.origin.x + gapX && touchPointInSuperview.x < imageview.frame.origin.x + imageview.frame.size.width - gapX) && (touchPointInSuperview.y >= imageview.frame.origin.y + gapY && touchPointInSuperview.y < imageview.frame.origin.y + imageview.frame.size.height - gapY)) 
        { 
         if([[ischose objectAtIndex:i-1] boolValue] == 0 && !isreverse) 
         { 
          currentposition = imageview.tag; 
          positionvalue += pow(i, 3); 
          currentanswer += [self converter:[NSString stringWithFormat:@"%@", [allimagenumbers substringWithRange:NSMakeRange(i-1, 1)]]]; 
          [ischose replaceObjectAtIndex:i-1 withObject:[NSNumber numberWithBool:YES]]; 
          [self changeimage:@"selected"]; 

          currentpoint = imageview.frame.origin; 

          [self.numberview drawRect:CGRectMake(self.numberview.frame.origin.x, self.numberview.frame.origin.y, self.numberview.frame.size.width, self.numberview.frame.size.height)]; 
          UIGraphicsBeginImageContext(imageview.frame.size); 
          CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f}; 
          CGContextSetStrokeColor(UIGraphicsGetCurrentContext(), red); 
          CGContextBeginPath(UIGraphicsGetCurrentContext()); 
          CGContextMoveToPoint(UIGraphicsGetCurrentContext(), previouspoint.x, previouspoint.y); 
          CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0f); 
          CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentpoint.x,currentpoint.y); 
          CGContextClosePath(UIGraphicsGetCurrentContext()); 

          previouspoint = currentpoint; 


         } 
         else 
         { 
          if(currentposition != imageview.tag) 
          { 
           isreverse = YES; 
          } 
          else 
          { 
           isreverse = NO; 
          } 
         } 
         break; 
        } 
       } 
      } 
     } 
     else 
     { 
      isended = YES; 
      isoutofbound = YES; 
      if(isbegan && isoutofbound) 
       [self countinganswer]; 
     } 
    } 
} 
else if(gestureRecognizer.state == UIGestureRecognizerStateEnded) 
{ 
    if(!isoutofbound) 
    { 
     isended = YES; 
     [self countinganswer]; 
    } 
    else 
     isoutofbound = NO; 
} 
} 

我試着畫一條線,但該線不能被延伸。

什麼問題?並可以給我一個示例項目?

回答

0

問題是你正在畫一個上下文,然後什麼也不做,只是漏。在進行繪圖的中間,您需要通過UIGraphicsGetImageFromCurrentContext()從上下文中獲取圖像。然後,您需要將該圖像分配給圖像視圖。

或者,您可以存儲繪製的點,然後在您的視圖的drawRect:函數中繪製它們。

不,我沒有一個示例項目給你。許多其他人已經完成了這個目標(包括我自己),我相信你也會這樣。如果你仍然遇到麻煩,你應該問一個更具體的問題,而不是僅僅在這裏傾銷所有的代碼,並問「爲什麼這不工作?」

一個無關的提示:如果你不想讓你的同事對你激怒,不要使用像你這樣做的百萬嵌套if語句。這是醜陋的,難以閱讀...