2011-11-20 6 views
1

사용자가 UIScrollView (페이징 사용 가능)에 나란히 배치 된 여러 UIView 캔버스에서 선을 그릴 수있게하려고합니다.하나의 UIScrollView에있는 여러 캔버스의 선 그리기

사용자가 캔버스를 서로 바꿀 수 있도록 스크롤보기가있어 한 번에 여러 그림을 유지 관리 할 수 ​​있습니다.

문제는 캔버스 중 어떤 것이 건드려도, 항상 같은 캔버스가 그려진다는 것입니다. 스크롤보기에 흰색과 검은 색 캔버스가있는 경우 흰색을 터치하면 항상 검정색 선이 그려집니다.

그래픽 컨텍스트가 사용되는 것과 관련이 있다고 생각합니다. 올바른 캔버스가 터치 이벤트를 수신하고 있음을 두 번 확인했습니다.

그림과 관련이있는 마지막 캔버스가 모든 이벤트를 수신하는 것으로 보입니다. 상위 뷰 컨트롤러에서 두 캔버스의 배경을 설정할 때 마지막 배경을 설정 한 것은 선을받는 배경입니다. 그래서 검은 색 캔버스의 배경을 마지막으로 설정하면 검은 색 캔버스가 그려지 며 그 반대의 경우도 마찬가지입니다.

#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 

누군가가이 문제를 도울 수 :

이 캔버스 객체의 코드?

답변

0

drawImage 변수가 올바르게 선언 된 것처럼 보입니다. 헤더 파일의 인터페이스 섹션이나 .m 파일의 개인 인터페이스 섹션에 선언 된 인스턴스 변수 또는 속성이어야합니다.

무료 부동 변수 선언이있는 경우 어떻게되는지 확실하지 않습니다. 컴파일러가 증상을 설명하는 정적 변수로 취급하고있는 것처럼 보입니다. 그러나 그것이 맞는지 확실하지 않습니다. 여기서 일어나는 일.

+0

이것은 정확히 문제였습니다. 감사! –