iOS8:如何从UIViewController中清除UIView

时间:2014-12-08 20:17:37

标签: objective-c uiview uiviewcontroller ios8

我正在尝试更新我的应用以使用自动布局的故事板。使用XIB,我的工作正常。我的应用程序有一个视图控制器,在Interface Builder中定义了一个subView。我在subView中绘制。

有效。

但我无法清除(或撤消/重做)。我可以看到调用UIView方法,但我不能做我应该做的事情。

以下是一些代码剪辑,我很感激一些帮助:

VC只是调用视图:

- (IBAction)eraseButtonTapped:(id)sender {
     NSLog(@"%s", __FUNCTION__);
    savedImage.image = drawImage.image;
    [drawingView eraseButtonClicked];
}

的UIView:

- (void)eraseButtonClicked {
    NSLog(@"%s", __FUNCTION__);
    self.bufferArray = [self.currentArray mutableCopy];
    [self.currentArray removeAllObjects];
    [self setNeedsDisplay];
}

两个函数都记录..

这些是触控方式:

#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{   
    NSLog(@"%s", __FUNCTION__);
    self.currentColorPath = [[DrawingPath alloc] init];
    [self.currentColorPath setColor:self.currentColor];
    UITouch *touch= [touches anyObject];


    [self.currentColorPath.path moveToPoint:[touch locationInView:self]];
    [self.currentArray addObject:self.currentColorPath];
    [self.redoStack removeAllObjects];

    lastPoint = [touch locationInView:self];
    lastPoint.y -= 20;

    if ([touch tapCount] == 2) {
        [self alertOKCancelAction];

        return;
    }        
}



-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s", __FUNCTION__);
    UITouch *touch = [touches anyObject]; 
    [self.currentColorPath.path addLineToPoint:[touch locationInView:self]];

    [self setNeedsDisplay];

    CGPoint currentPoint = [touch locationInView:self];
    currentPoint.y -= 20;

    lastPoint = currentPoint;

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s", __FUNCTION__);
    self.currentColorPath = nil;
}

视图调用一个单独的对象来处理绘图。这是一个代码片段(删除不相关的位)。

- (id)init {
    //NSLog(@"%s", __FUNCTION__);
    if (!(self = [super init] ))
        return nil;

   path = [[UIBezierPath alloc] init];
    _path.lineCapStyle=kCGLineCapRound;
    _path.lineJoinStyle=kCGLineJoinRound;
    [_path setLineWidth:brush];

    return self;
}

- (void)draw {
    NSLog(@"%s", __FUNCTION__);

    [self.color setStroke];
    [self.path stroke];

}

0 个答案:

没有答案