Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS: CGContextRef drawRect does not agree with input
    primarykey
    data
    text
    <p><strong>Background</strong> : I would like to draw blocks when the user touch up somewhere. If the block is there, I want to erase it. I manage the blocks by using <code>NSMutableArray</code>to keep track of points where the block should go. Every time user touches, it will determine if the touch place already contained a block or not and manage the array accordingly. </p> <p><strong>Problem</strong> : I got a very weird feedback from this. First of all, everything in the array works as I wanted. The problem comes when the user wanted to erase a block. While the array is maintained correctly, the drawing seems to ignore the change in the array. It will not remove anything but the last dot. And even that flashes toggles on and off when the user clicked elsewhere.</p> <p><strong>Here is the code</strong> :</p> <pre><code>- (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 &lt; [pointArray count]){ [pointArray removeObjectAtIndex:i]; NSLog(@"remove"); }else { [pointArray addObject:pointValue]; NSLog(@"add"); } NSLog(@"Current array : %@", pointArray); [self setNeedsDisplay]; } </code></pre> <p><code>scale</code> is defined as 16. <code>pointArray</code> is a member variable of the view.</p> <p><strong>To Test</strong> : You can drop this into any UIView and add that to the viewController to see the effect. </p> <p><strong>Question</strong> : How do I get the drawing to agree with the array?</p> <hr> <p><strong>Update + Explanation</strong>: I am aware of the cost of this approach but it is only created for me to get a quick figure. It will not be used in the real application, thus, please do not get hung up about how expensive it is. I only created this capability to get a value in <code>NSString</code> (<code>@"1,3,5,1,2,6,2,5,5,..."</code>) of a figure I draw. This will become more efficient when I am actually using it with no redrawing. please stick to the question asked. Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload