Note that there are some explanatory texts on larger screens.

plurals
  1. PODrawing on a zoomable view
    primarykey
    data
    text
    <p>I'm working on a small drawing application, which has a basic requirement of supporting zoom-in/out. I have two main issues:</p> <ol> <li><p>Drawing doesn't appear crisp and clear, when view is zoomed/transformed. Is there a better approach, or is there a way to improve the drawing when the view is zoomed?</p></li> <li><p>The drawing performance is slow, when drawing on 1200 x 1200 pts canvas (on iPhone). Any chance I can improve it for large canvas sizes?</p></li> </ol> <p><strong>Zooming Code:</strong></p> <pre><code>- (void)scale:(UIPinchGestureRecognizer *)gestureRecognizer { [self adjustAnchorPointForGestureRecognizer:gestureRecognizer]; UIView *canvas = [gestureRecognizer view]; if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) { // Calculate the drawing view's size CGSize drawingViewSize = ...; // Calculate the minimum allowed tranform size // Developer's Note: I actually wanted to use the size 1/4th of the view // but self.view.frame.size doesn't return the correct (actual) width and height // It returns these values inverted i.e. width as height, and vice verse. // The reason is that the view appears to be transformed (90 degrees). // Since there's no work-around this, so for now, I'm just using fixed values. CGSize minTranformSize = CGSizeMake(100.0f, 100.0f); if ((minTranformSize.width &gt; drawingViewSize.width) &amp;&amp; (minTranformSize.height &gt; drawingViewSize.height)) { minTranformSize = drawingViewSize; } // Transform the view, provided // 1. It won't scale more than the original size of the background image // 2. It won't scale less than the minimum possible transform CGSize transformedSize = CGSizeMake(canvas.frame.size.width * [gestureRecognizer scale], canvas.frame.size.height * [gestureRecognizer scale]); if ((transformedSize.width &lt;= drawingViewSize.width) &amp;&amp; (transformedSize.height &lt;= drawingViewSize.height) &amp;&amp; (transformedSize.width &gt;= minTranformSize.width) &amp;&amp; (transformedSize.height &gt;= minTranformSize.height)) { canvas.transform = CGAffineTransformScale([canvas transform], [gestureRecognizer scale], [gestureRecognizer scale]); } [gestureRecognizer setScale:1.0]; } else if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) { // Recenter the container view, if required (piece is smaller than the view and it's not aligned) CGSize viewSize = self.view.bounds.size; if ((canvas.frame.size.width &lt; viewSize.width) || (canvas.frame.size.height &lt; viewSize.height)) { canvas.center = CGPointMake(viewSize.width/2, viewSize.height/2); } // Adjust the x/y coordinates, if required (piece is larger than the view and it's moving outwards from the view) if (((canvas.frame.origin.x &gt; 0) || (canvas.frame.origin.y &gt; 0)) &amp;&amp; ((canvas.frame.size.width &gt;= viewSize.width) &amp;&amp; (canvas.frame.size.height &gt;= viewSize.height))) { canvas.frame = CGRectMake(0.0, 0.0, canvas.frame.size.width, canvas.frame.size.height); } canvas.frame = CGRectIntegral(canvas.frame); } } </code></pre> <p><strong>Drawing Code</strong></p> <pre><code>- (void)draw { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); if (self.fillColor) { [self.fillColor setFill]; [self.path fill]; } if ([self.strokeColor isEqual:[UIColor clearColor]]) { [self.path strokeWithBlendMode:kCGBlendModeClear alpha:1.0]; } else if (self.strokeColor) { [self.strokeColor setStroke]; [self.path stroke]; } CGContextRestoreGState(context); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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