【问题标题】:Clear UIView of drawing?清除绘图的UIView?
【发布时间】:2013-11-20 05:13:18
【问题描述】:

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

子类.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

查看控制器.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];
}

谁能告诉我为什么这不起作用,我应该怎么做?

【问题讨论】:

    标签: ios objective-c cocoa-touch uiview drawrect


    【解决方案1】:

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

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

    在您的子类的.h 文件中声明此方法。从您的视图控制器类中,您需要在需要擦除视图时调用此方法,

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

    试试看。

    希望有帮助!

    【讨论】:

    • @JosueEspinosa 此代码来自我的一个工作项目。你能告诉我你的情况出了什么问题吗?
    • 我实际上只是在有效的擦除方法下将我的增量图像设置为 nil!不知道为什么它不适用于您的上述示例。
    【解决方案2】:

    它肯定会工作......保证...... 除了@Amar 的回答和@Josue 的评论...谢谢你们俩

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多