Undo with multitouch drawing in iOS

后端 未结 2 1698
鱼传尺愫
鱼传尺愫 2020-12-04 03:38

I am working with multitouch while writing, So basically what I am doing is, I am writing with hand support, because typically, its how user rights, I followed this link How

相关标签:
2条回答
  • 2020-12-04 03:45

    Well in my case i have used bezierPath to draw on touch and have successfully implemented undo functionality. Here is the code :

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

    Hope it will help you.

    0 讨论(0)
  • m_redoArray appears to be the big daddy, the one you draw from. I don't understand why you empty this out out in 'touchesBegan...', surely one of these arrays must carry through touchesBegan unaltered, or you'll be dropping stuff from the start of your drawing all the way through, no?

    It appears to me that this is how you dropped the 'Hell' in your example here..

    0 讨论(0)
提交回复
热议问题