2012-07-24 28 views
0

長按是否可以在長時間按比例繪畫?因爲當我做長按手勢時,我想讓自己的線條變得更大,並使用該點來觸摸移動線。我希望這是有道理的,這是我的代碼。客觀C:製作點長按

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event//upon moving 
{ 

      UITouch *touch = [touches anyObject]; 
      previousPoint2 = previousPoint1; 
      previousPoint1 = currentTouch; 
      currentTouch = [touch locationInView:self.view]; 


      CGPoint mid1 = midPoint(previousPoint2, previousPoint1); 
      CGPoint mid2 = midPoint(currentTouch, previousPoint1); 


      UIGraphicsBeginImageContext(CGSizeMake(1024, 768)); 
      [imgDraw.image drawInRect:CGRectMake(0, 0, 1024, 768)]; 
      CGContextRef context = UIGraphicsGetCurrentContext(); 
      CGContextSetLineCap(context,kCGLineCapRound); 
      CGContextSetLineWidth(context, slider.value); 
      CGContextSetBlendMode(context, blendMode); 
      CGContextSetRGBStrokeColor(context,red, green, blue, 1); 
      CGContextBeginPath(context); 
      CGContextMoveToPoint(context, mid1.x, mid1.y);//Computation 
      CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y); 
      CGContextStrokePath(context); 

      imgDraw.image = UIGraphicsGetImageFromCurrentImageContext(); 
      UIGraphicsGetCurrentContext(); 
} 

現在我該如何插入我的長按?

回答

0

一種可能的解決方案:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

  • 保存從在一個實例變量的touches一個UITouch

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

  • 檢查實例 變量。如果是,則計算保存的觸摸和新觸摸的時間戳差異。使用此差異來確定線寬,然後取消設置實例變量。
1

您應該添加一個UILongPressGestureRecognizer到您的視圖。然後該識別器應該有一個與之關聯的方法,增加點的半徑,然後繪製它,當手勢結束時,將半徑重置爲某個默認的起始值。

嘗試這樣:

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(drawAndExpandPoint:)]; 
    [self addGestureRecognizer:recognizer]; 
} 

然後,在drawAndExpandPoint方法,你可以這樣做(與具有一些默認值伊娃稱爲半徑):

-(void)drawAndExpandPoint:(UILongPressGestureRecognizer *)recognizer 
{ 
    //Reset radius, if gesture ended 
    if (recognizer.state == UIGestureRecognizerStateEnded) { 
     radius = DEFAULT_RADIUS; 
     return; 
    } 

    else if (radius <= MAX_RADIUS) { 
     radius += RADIUS_INCREMENT; 
     //You will have to write this method to draw the point 
     [self drawAtPoint:[recognizer locationInView:self.view] withRadius:radius]; 
    } 
} 

此代碼可能不是你所描述的100%,但我認爲它概述了一般策略,即使用手勢識別器 - 它使事情變得更容易。

+0

是你在UIView中聲明的代碼嗎?如果沒有使用drawinRec方法在「[self drawAtPoint:[recognitionizer locationInView:self.view] withRadius:radius]」中更改self,我應該怎麼做;「 – jrf 2012-07-25 08:31:28

+0

不,我會在處理手勢的視圖控制器中聲明這一點。 drawAtPoint:withRadius方法只是我編寫的一個任意方法。你怎麼做你的繪畫? – 2012-07-25 08:34:53

+0

我有一個「自我」的錯誤爲您的任意方法..我不知道我應該放在那裏。 – jrf 2012-07-25 09:28:53