清除UIView的绘图? [英] Clear UIView of drawing?

查看:643
本文介绍了清除UIView的绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用UIView的子类来绘制,这个子类视图用于在视图控制器上获取签名。有一个清除按钮,它应该清除UIView,除了它不工作。这是我尝试过的。

I am using a subclass of UIView to draw, this subclassed view is used to get your signature on a view controller. There is a clear button which is supposed to clear the UIView except it doesn't work. Here is what I have tried.

subclass.h

subclass.h

@implementation subclassed uiview
{
    UIBezierPath *path;
    UIImage *incrementalImage; // (1)
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setMultipleTouchEnabled:NO];
        [self setBackgroundColor:[UIColor clearColor]];
        path = [UIBezierPath bezierPath];
        [path setLineWidth:2.0];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [incrementalImage drawInRect:rect]; // (3)
    [path stroke];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    [path moveToPoint:p];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    [path addLineToPoint:p];
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event // (2)
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    [path addLineToPoint:p];
    [self drawBitmap]; // (3)
    [self setNeedsDisplay];
    [path removeAllPoints]; //(4)
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesEnded:touches withEvent:event];
}

- (void)drawBitmap // (3)
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
    //[[UIColor blackColor] setStroke];
    if (!incrementalImage) { // first draw;
        UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
        [[UIColor clearColor] setFill];
        [rectpath fill]; // fill it
    }
    [incrementalImage drawAtPoint:CGPointZero];
    [path stroke];
    incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

@end

p>

view controller.m

- (IBAction)clearTapped:(id)sender {
    self.subclassedView.backgroundColor = [UIColor clearColor];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextFillRect(context,self.subclassedView.bounds);
    CGContextClearRect(context, self.subclassedView.bounds);
    CGContextFlush(context);
    [self.subclassedView setNeedsDisplay];
}

任何人都可以告诉我为什么这不工作,

Can anyone tell me why this doesn't work, and what I should do instead?

推荐答案

重新初始化路径变量并强制视图使用此新创建的空路径绘制。这给出擦除先前绘制路径的印象。在你的子类中包含这个函数,

Reinitialise the path variable and force the view to draw with this newly created empty path. This gives impression of erasing the previous drawn path. In your subclass include this function,

- (void)erase {
    path   = nil;  //Set current path nil
    path   = [UIBezierPath bezierPath]; //Create new path
    [self setNeedsDisplay]; 
}

.h 您的子类的文件。从您的视图控制器类,你需要调用此方法,每当你需要删除视图,

Declare this method in the .h file of your subclass. From your view controller class you need to call this method whenever you need to erase the view,

- (IBAction)clearTapped:(id)sender {
    [self.subclassedView erase];
}

尝试一下。

希望有所帮助!

这篇关于清除UIView的绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆