在一个UIScrollView上的多个画布上绘制线条

时间:2011-11-20 15:15:10

标签: objective-c ios quartz-2d

我试图让用户在UIScrollView上并排放置的多个UIView画布上绘制线条(启用分页)。

滚动视图是让用户在画布之间交换的,因此他们可以一次维护多个图纸。

问题在于,无论哪个画布被触摸,都会画出相同的画布。如果我在滚动视图上有白色和黑色画布,触摸白色画布总是在黑色画布上绘制线条。

我认为这与使用的图形上下文有关,我已经仔细检查过正确的画布是否正在接收触摸事件。

我执行与绘图相关的最后一个画布似乎接收了所有事件。当我在父视图控制器中设置两个画布的背景时,我设置的最后一个背景是接收行的那个。 因此,设置黑色画布的背景意味着将绘制黑色画布,反之亦然。

这是canvas对象的代码:

#import "Canvas.h"


@implementation Canvas

@synthesize canvasColour;

UIImageView* drawImage;
int mouseMoved;
BOOL mouseSwiped;
CGPoint lastPoint;

CGContextRef context;

- (void)viewDidLoad {
    [super viewDidLoad];
    drawImage = [[UIImageView alloc] initWithImage:nil];
    drawImage.frame = self.view.frame;
    [self.view addSubview:drawImage];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    NSLog([NSString stringWithFormat:@"Touches began on %@", self.canvasColour]);


    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        drawImage.image = nil;
        return;
    }

    lastPoint = [touch locationInView:self.view];
    lastPoint.y -= 20;

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.view];
    currentPoint.y -= 20;

    UIGraphicsBeginImageContext(self.view.frame.size);
    context = UIGraphicsGetCurrentContext();
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

    mouseMoved++;

    if (mouseMoved == 10) {
        mouseMoved = 0;
    }


}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        drawImage.image = nil;
        return;
    }


    if(!mouseSwiped) {
        UIGraphicsBeginImageContext(self.view.frame.size);
        [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }

}

- (void)dealloc
{
    [super dealloc];
}

@end

有人可以帮忙解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您的drawImage变量看起来不正确。它应该是在头文件的接口部分或.m文件的专用接口部分中声明的实例变量或属性。

我不确定如果您只有一个自由浮动变量声明会发生什么,看起来编译器将其视为一个静态变量,可以解释您的症状,但我不确定这是什么正在这里发生。