배경 : 사용자가 어딘가에 손을 대면 블록을 그립니다. 블록이 있으면 지우고 싶습니다. 블록을 관리해야하는 지점을 추적하기 위해 NSMutableArray
을 사용하여 블록을 관리합니다. 사용자가 터치 할 때마다 터치 위치에 이미 블록이 포함되어 있는지 여부가 결정되고 그에 따라 어레이를 관리합니다.iOS : CGContextRef drawRect가 입력과 일치하지 않습니다.
문제 :이 문제에 대한 매우 이상한 의견이 있습니다. 우선 배열의 모든 것이 내가 원하는대로 작동합니다. 문제는 사용자가 블록을 지우고 싶을 때 발생합니다. 배열이 올바르게 유지되는 동안 도면은 배열의 변경 사항을 무시하는 것 같습니다. 마지막 점 이외의 것은 제거되지 않습니다. 사용자가 다른 곳을 클릭 할 때 깜박임조차 켜고 끕니다.
- (void)drawRect:(CGRect)rect
{
NSLog(@"drawrect current array %@",pointArray);
for (NSValue *pointValue in pointArray){
CGPoint point = [pointValue CGPointValue];
[self drawSquareAt:point];
}
}
- (void) drawSquareAt:(CGPoint) point{
float x = point.x * scale;
float y = point.y * scale;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, x, y);
CGContextAddLineToPoint(context, x+scale, y);
CGContextAddLineToPoint(context, x+scale, y+scale);
CGContextAddLineToPoint(context, x, y+scale);
CGContextAddLineToPoint(context, x, y);
CGContextSetFillColorWithColor(context, [UIColor darkGrayColor].CGColor);
CGContextFillPath(context);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *aTouch = [touches anyObject];
CGPoint point = [aTouch locationInView:self];
point = CGPointMake((int) (point.x/scale), (int) (point.y/scale));
NSLog(@"Touched at %@", [NSArray arrayWithObject: [NSValue valueWithCGPoint:point]]);
NSValue *pointValue = [NSValue valueWithCGPoint:point];
int i = [pointArray indexOfObject:pointValue];
NSLog(@"Index at %i",i);
if (i < [pointArray count]){
[pointArray removeObjectAtIndex:i];
NSLog(@"remove");
}else {
[pointArray addObject:pointValue];
NSLog(@"add");
}
NSLog(@"Current array : %@", pointArray);
[self setNeedsDisplay];
}
scale
가 16 pointArray
뷰의 멤버 변수로서 정의된다
여기서 코드이다.
테스트하려면 : 이것을 UIView에 드롭하고 뷰 컨트롤러에 추가하여 효과를 확인할 수 있습니다.
질문 : 그림을 배열과 일치 시키려면 어떻게합니까?
업데이트 + 설명 :이 방법의 비용을 알고 있지만 나 빠른 수치를 얻을 수 있도록 만 생성됩니다. 실제 응용 프로그램에서는 사용되지 않으므로 비용이 많이 들지 않도록하십시오. 이 기능을 사용하여 그림을 그리는 NSString
(@"1,3,5,1,2,6,2,5,5,..."
)의 값만 가져 왔습니다. 실제로 다시 그릴 필요가 없을 때 더 효율적으로 사용할 수 있습니다. 묻는 질문에 충실하십시오. 고맙습니다.
'setFill'은 완벽하게 작동합니다. 그러나'clearsContextBeforeDrawing : YES'가 작동하지 않았습니다. 그것은 매우 비효율적이지만 제 목적을 위해서는 완벽합니다. 고맙습니다. – Byte