Ios 制作一个UIView画布,可用于用手指画线

Ios 制作一个UIView画布,可用于用手指画线,ios,canvas,touch,draw,Ios,Canvas,Touch,Draw,我正在尝试写一个UIView,它的工作原理就像画布,我们可以用手指在上面画画。我实现了一个画布视图,当手指移动时,它首先绘制已经存在的图像,然后绘制一条连接前一点和当前点的细线。当我移动手指时,它确实会绘制图像,但是,当我绘制时,图像会逐渐消失。我不明白为什么会出现这种情况。我知道有其他一些方法可以用UIView实现画布,但我想知道为什么我的实现是错误的。下面是我的完整代码。我希望任何人都能复制这段代码并运行它,这样我的问题就更直观了 #import <UIKit/UIKit.h>

我正在尝试写一个UIView,它的工作原理就像画布,我们可以用手指在上面画画。我实现了一个画布视图,当手指移动时,它首先绘制已经存在的图像,然后绘制一条连接前一点和当前点的细线。当我移动手指时,它确实会绘制图像,但是,当我绘制时,图像会逐渐消失。我不明白为什么会出现这种情况。我知道有其他一些方法可以用UIView实现画布,但我想知道为什么我的实现是错误的。下面是我的完整代码。我希望任何人都能复制这段代码并运行它,这样我的问题就更直观了

#import <UIKit/UIKit.h>

@interface CanvasView : UIView

@end

#import "CanvasView.h"
#import <QuartzCore/QuartzCore.h>

@interface CanvasView()
@property (nonatomic) CGPoint startPoint;
@property (nonatomic) CGPoint endPoint;
@property (nonatomic, strong) UIImage* oldImage;
@end

@implementation CanvasView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch* touch = [touches anyObject];
    self.startPoint = [touch locationInView:self];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch* touch = [touches anyObject];
    self.endPoint = [touch locationInView:self];

    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    self.oldImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch* touch = [touches anyObject];
    self.endPoint = [touch locationInView:self];

    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    self.oldImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [self setNeedsDisplay];
}

-(void)drawRect:(CGRect)rect{
    CGContextRef context = UIGraphicsGetCurrentContext();

    //draw image already exists
    [self.oldImage drawInRect:self.bounds];

    //draw new tiny line
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, self.startPoint.x, self.startPoint.y);
    CGContextAddLineToPoint(context, self.endPoint.x, self.endPoint.y);
    CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
    CGContextSetLineWidth(context, 5.);
    CGContextStrokePath(context);

    self.startPoint = self.endPoint;
}

@end
#导入
@界面画布视图:UIView
@结束
#导入“CanvasView.h”
#进口
@界面画布视图()
@属性(非原子)CGPoint startPoint;
@属性(非原子)CGPoint端点;
@属性(非原子,强)UIImage*oldImage;
@结束
@实现画布视图
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
如果(自我){
//初始化代码
self.backgroundColor=[UIColor whiteColor];
}
回归自我;
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
UITouch*touch=[触摸任何对象];
self.startPoint=[触摸位置查看:self];
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件{
UITouch*touch=[触摸任何对象];
self.endPoint=[touch locationInView:self];
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderContext:UIGraphicsGetCurrentContext()];
self.oldImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
[自我设置需要显示];
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event{
UITouch*touch=[触摸任何对象];
self.endPoint=[touch locationInView:self];
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderContext:UIGraphicsGetCurrentContext()];
self.oldImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
[自我设置需要显示];
}
-(void)drawRect:(CGRect)rect{
CGContextRef context=UIGraphicsGetCurrentContext();
//绘制图像已存在
[self.oldImage drawInRect:self.bounds];
//画一条新的细线
CGContextBeginPath(上下文);
CGContextMoveToPoint(上下文,self.startPoint.x,self.startPoint.y);
CGContextAddLineToPoint(上下文,self.endPoint.x,self.endPoint.y);
CGContextSetStrokeColorWithColor(上下文[UIColor greenColor].CGColor);
CGContextSetLineWidth(上下文,5.);
CGContextStrokePath(上下文);
self.startPoint=self.endPoint;
}
@结束

您找到解决方案了吗?我也对衰落发生的原因感兴趣。