2014-02-21 36 views
2
多點觸控繪圖

我與多點觸控的工作而寫,所以基本上我做什麼,我用手支撐寫作,因爲通常情況下,它是如何的用戶權限,我跟着這個鏈接How to ignore certain UITouch Points in multitouch sequence撤消與iOS的

一切工作正常,但是當我用手觸摸屏幕寫字時,它們是撤消的問題,否則它可以正常工作。

下面是我的代碼

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch* topmostTouch = self.trackingTouch; 
    for (UITouch *touch in touches) 
    { 
     ctr = 0; 

     touchStartPoint1 = [touch locationInView:self]; 


     [m_undoArray removeAllObjects]; 
     [m_redoArray removeAllObjects]; 
     [m_parentRedoArray removeAllObjects]; 


     if(!topmostTouch || [topmostTouch locationInView:self].y > touchStartPoint1.y) 
     { 
      topmostTouch = touch; 
      pts[0] = touchStartPoint1; 
     } 
    } 


    if (self.trackingTouch != nil && self.trackingTouch != topmostTouch) //    ![touches containsObject:self.trackingTouch]) 
    { 
     [self discardDrawing]; 

    } 

    self.trackingTouch = topmostTouch; 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if(self.trackingTouch== nil) 
    { 
     return; 
    } 

    CGPoint p = [self.trackingTouch locationInView:self]; 
    ctr++; 
    pts[ctr] = p; 

    if (ctr == 4) 
    { 
     pts[3] = midPoint(pts[2], pts[4]); 

     self.currentPath = [[DrawingPath alloc] init]; 

     [self.currentPath setPathColor:self.lineColor]; 
     self.currentPath.pathWidth = [NSString stringWithFormat:@"%f",self.lineWidth]; 


     [self.currentPath.path moveToPoint:pts[0]]; 
     [self.currentPath.path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; 

     CGPathRef cgPath = self.currentPath.path.CGPath; 
     mutablePath = CGPathCreateMutableCopy(cgPath); 

     [m_undoArray addObject:self.currentPath]; 
     [self setNeedsDisplay]; 


     pts[0] = pts[3]; 
     pts[1] = pts[4]; 
     ctr = 1; 
    } 
} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{  
    for (UITouch *touch in touches) 
    { 
     if(touch == self.trackingTouch) 
     { 
      [m_parentUndoArray addObject:[NSArray arrayWithArray:m_undoArray]];     
     }   
    } 
} 


-(void)undoButtonClicked 
{  
    NSMutableArray *undoArray = [m_parentUndoArray lastObject]; 

    NSLog(@"%@",undoArray); 

    [m_parentUndoArray removeLastObject]; 
    [m_parentRedoArray addObject:undoArray]; 
    m_drawStep = UNDO; 

    [self setNeedsDisplay];  

} 


- (void)drawRect 
{ 
    I have different cases here, I am showing Of Undo 

    for(int i = 0; i<[m_parentUndoArray count];i++) 
    { 
     NSMutableArray *undoArray = [m_parentUndoArray objectAtIndex:i]; 
     NSLog(@"%@",undoArray); 

     for(int i =0; i<[undoArray count];i++) 
     { 
     DrawingPath *drawPath = [undoArray objectAtIndex:i]; 
     GPathRef path = drawPath.path.CGPath; 
     mutablePath = CGPathCreateMutableCopy(path); 

     //Draw into CgLayer    
    } 
    } 
} 

這裏是圖像理解我的問題好,我第一次寫這個

enter image description here

After clicking on undo Once

點擊撤消一次後,你上面可以看到,其他部分已經被撤消,而不是最後一部分。所以我需要你在這方面的幫助。

+0

你以同樣的方式繪製之前和之後的'Undo'(你總是使用'm_parentUndoArray'來畫 ?)? –

+0

在觸摸移動,你可以看到,我將路徑轉換成CGMutablePath和繪圖..但爲了撤消,我使用ParentUndoArray .. @NicolasBonnet – Ranjit

+0

你測試重繪沒有刪除最後一個對象嗎? (用'm_parentUndoArray'測試繪製方法')@Ranjit –

回答

1

m_redoArray看起來是大爸爸,你從中吸取的。我不明白你爲什麼要在'touchesBegan'中清空這個問題,當然這些數組中的其中一個必須通過touchesBegan保持不變,否則你會從繪圖開始一直拖下去,不是嗎?

在我看來,這是你如何在這裏你的榜樣放棄了「地獄」 ..

+0

你好@Jef,我沒有在任何地方使用m_redoArray。我只使用m_undoArray並將其添加到m_parentUndoArray – Ranjit

+0

我的意思是我已經寫在上面顯示的代碼中,但我沒有在任何地方使用 – Ranjit

+0

啊,我想你在touchesBegan中清空的數組之一...不應該在這一點上需要清空,需要查看drawRect的其餘部分:....也不要忘記爲每個CGPathCreate或CGPathRelease(..)複製,那裏沒有ARC,這是核心基礎的東西 – Jef

0

那麼在我的情況下,我用bezierPath來觸摸併成功實現了撤消功能。這裏是代碼:

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
    // Initialization code 

    self.backgroundColor = [UIColor clearColor]; 
    myPath = [[UIBezierPath alloc] init]; 
    myPath.lineCapStyle = kCGLineCapRound; 
    myPath.miterLimit = 0; 
    bSize=5; 
    myPath.lineWidth = bSize; 
    brushPattern = [UIColor whiteColor]; 

    // Arrays for saving undo-redo steps in arrays 
    pathArray = [[NSMutableArray alloc] init]; 
    bufferArray = [[NSMutableArray alloc] init]; 


    } 
return self; 
} 

// Only override drawRect: if you perform custom drawing. 

// An empty implementation adversely affects performance during animation. 

- (void)drawRect:(CGRect)rect 
{ 
    [brushPattern setStroke]; 
    for (id path in pathArray){ 
     if ([path isKindOfClass:[UIBezierPath class]]) { 
      UIBezierPath *_path=(UIBezierPath *)path; 
      [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0]; 
     } 
    } 
} 

#pragma mark - Touch Methods 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

      UITouch *mytouch = [[touches allObjects] objectAtIndex:0]; 
      myPath = [[UIBezierPath alloc] init]; 
      myPath.lineWidth = bSize; 
      [myPath moveToPoint:[mytouch locationInView:self]]; 
      [pathArray addObject:myPath]; 

} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
     [myPath addLineToPoint:[[touches anyObject] locationInView:self]]; 
     [self setNeedsDisplay]; 
} 


#pragma mark - Undo Method 
-(void)undoButtonClicked 
{ 
    if([pathArray count]>0) 
    { 
    UIBezierPath *_path = [pathArray lastObject]; 
    [bufferArray addObject:_path]; 
     [pathArray removeLastObject]; 
     [self setNeedsDisplay]; 
    } 

} 
-(void)setBrushSize: (CGFloat)brushSize 
{ 
    bSize=brushSize; 
} 

-(void)redoButtonClicked 
{ 
    if([bufferArray count]>0){ 
    UIBezierPath *_path = [bufferArray lastObject]; 
    [pathArray addObject:_path]; 
    [bufferArray removeLastObject]; 
    [self setNeedsDisplay]; 
    } 
} 

希望它能幫助你。