在我的整个应用程序中

在我的整个应用程序中

我实现了以下代码,该代码允许用户在imageView上进行绘制。我想在我的整个应用程序中实施此操作,但不希望继续复制和粘贴。

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

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint pointNext = [touch locationInView:self.view];
UIGraphicsBeginImageContext(drawImage.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), pointCurrent.x, pointCurrent.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), pointNext.x, pointNext.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
pointCurrent = pointNext;
}


到目前为止,我已经尝试创建一个类别,但是我不确定这是否是正确的解决方法。我是否制作了一个自定义方法并尝试在其他类中调用它,或者我吠叫了错误的树?在此先感谢您抽出宝贵的时间阅读此问题。

最佳答案

我修改了您的代码,因为我遇到了您在注释中提到的相同错误。这段代码对我有用。

@interface RDImageView ()
@property (nonatomic) CGPoint pointCurrent;
@end

@implementation RDImageView

-(instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor blueColor];
    }
    return self;
}

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

}



- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint pointNext = [touch locationInView:self];
    UIGraphicsBeginImageContext(self.frame.size);
    [self.image drawInRect:self.bounds];
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1,1, 0.0, 1.0);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.pointCurrent.x, self.pointCurrent.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), pointNext.x, pointNext.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.pointCurrent = pointNext;
}

关于ios - 如何允许用户在我的整个应用程序中进行绘制?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23072301/

10-09 16:23