ios – 清晰的绘图?

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

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

查看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];
}

任何人都可以告诉我为什么这不起作用,我应该做什么呢?

解决方法

重新初始化路径变量并强制视图使用此新创建的空路径进行绘制.这给人以擦除先前绘制路径的印象.在你的子类中包含这个函数,
- (void)erase {
    path   = nil;  //Set current path nil
    path   = [UIBezierPath bezierPath]; //Create new path
    [self setNeedsdisplay]; 
}

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

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

试试看.

希望有所帮助!

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...