【问题标题】:How to undo multitouch drawing in iOS如何在 iOS 中撤消多点触控绘图
【发布时间】:2018-09-01 20:35:02
【问题描述】:

请帮助我撤消并转发我的抽奖。我将所有路径存储到 pathArray 中,现在我想撤消并转发路径(如 app Notes (Apple))。 请帮我解决这个问题!

这是我的 PathArray 类

//  PathArray.h

#import <Foundation/Foundation.h>

@interface PathArray : NSObject
@property (nonatomic) CGPoint start;
@property (nonatomic) CGPoint end;
@property (nonatomic) UIColor* color;
@property (nonatomic) CGFloat pathWidth;
- (instancetype) initWithStartPoint: (CGPoint)start andEnd: (CGPoint)end andColor: (UIColor *) color andPathWidth: (CGFloat) pathWidth;
@end

这是我的绘图课

- (void)drawRect:(CGRect)rect
{
    [strokecolor setStroke];
    for (int i=0; i<pathArray.count; i++  ) {
        CGContextRef contex = UIGraphicsGetCurrentContext();
        CGContextSetLineCap(contex, kCGLineCapRound);
        CGContextBeginPath(contex);
        CGContextMoveToPoint(contex, pathArray[i].start.x, pathArray[i].start.y);
        CGContextAddLineToPoint(contex, pathArray[i].end.x, pathArray[i].end.y);
        CGContextSetStrokeColorWithColor(contex, pathArray[i].color.CGColor);
        CGContextSetLineWidth(contex, pathArray[i].pathWidth);
        CGContextStrokePath(contex);
    }
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ 
    UITouch* touch = [touches anyObject];
    if ([touch type] == UITouchTypeStylus)
    {
        if(drawable){
            [self.delegate changeScrollViewInteraction:NO];
            UITouch *touch = [touches anyObject];
            lastPoint = [touch locationInView:self];
        }
    }
    else
    {
        [self.delegate changeScrollViewInteraction:YES];
        return;
    }
    [super touchesBegan: touches withEvent: event];
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([touch type] == UITouchTypeStylus) //pencil touch
    {
        if(drawable){
            CGPoint newpoint = [touch locationInView:self];
            PathArray *paths = [[PathArray alloc]initWithStartPoint:lastPoint andEnd:newpoint andColor:strokecolor andPathWidth:pathWidth];
            [pathArray addObject:paths];
            lastPoint = newpoint;
            [self setNeedsDisplay];
            NSLog(@"%@",pathArray[0]);
        }
    }
    else
    {
        return;
    }
    [super touchesMoved: touches withEvent: event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
     UITouch *touch = [touches anyObject];   
    if ([touch type] == UITouchTypeStylus && drawable) //pencil touch
    {
        [pathEndPointArray addObject:tempEndPointArray];
    }

    if ([touch type] == UITouchTypeStylus && !drawable)
    {
        [self.delegate CacheLIView_showPopup_message:@"Already cupped!"];
    }
    [self touchesMoved:touches withEvent:event];

}
-(void)undo{
}
-(void)foward{
}
-(void)re_draw_after_rotate{ }

我需要创建这些函数。

【问题讨论】:

  • 请向我们展示您自己为解决问题所做的努力。这可以包括但不限于实际代码、您自己进行的研究的结果或您考虑的潜在方法。

标签: ios objective-c drawing undo forward


【解决方案1】:

我会建议以下(这里没有代码):

  • 创建第二个数组pathUndoArray
  • 撤消时,从pathArray 中删除最后一个条目(如果存在)并将其(在末尾)添加到pathUndoArray,然后重绘。
  • 重做时,从pathUndoArray 中删除最后一个条目(如果存在)并将其(在末尾)添加到pathArray,然后重绘。
  • 重新触摸后,您必须删除 pathUndoArray

你也可以看看NSUndoManager

【讨论】:

  • 非常感谢!
猜你喜欢
  • 2013-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-02
  • 1970-01-01
  • 2013-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多