gpt4 book ai didi

objective-c - 在一个 UIScrollView 的多个 Canvas 上画线

转载 作者:行者123 更新时间:2023-11-28 17:40:48 26 4
gpt4 key购买 nike

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

ScrollView 可以让用户在 Canvas 之间切换,因此他们可以同时维护多个绘图。

问题是无论触摸哪个 Canvas ,总是绘制到同一个 Canvas 。如果 ScrollView 上有白色和黑色 Canvas ,触摸白色 Canvas 总是会在黑色 Canvas 上画线。

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

我做一些与绘图相关的最后一个 Canvas 似乎接收了所有事件。当我在父 View Controller 中设置两个 Canvas 的背景时,我设置最后一个背景的那个是接收线条的那个。因此,最后设置黑色 Canvas 的背景意味着将在黑色 Canvas 上绘制,反之亦然。

这是 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

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

最佳答案

您的 drawImage 变量看起来没有正确声明。它应该是在头文件的接口(interface)部分或 .m 文件中的私有(private)接口(interface)部分中声明的实例变量或属性。

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

关于objective-c - 在一个 UIScrollView 的多个 Canvas 上画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8202454/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com