Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6135429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:28:44+00:00 2026-05-23T17:28:44+00:00

I was using the drawing code from this tutorial to let someone draw in

  • 0

I was using the drawing code from this tutorial to let someone draw in a small box: http://www.ifans.com/forums/showthread.php?t=132024

The default code draws on the entire screen, how do you limit the drawing to a small box on the screen?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:28:44+00:00Added an answer on May 23, 2026 at 5:28 pm

    In this examples the touches and the drawing happen in the main view of the view controller:

    UIGraphicsBeginImageContext(self.view.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    

    etc…

    So if you change the frame of that view to a smaller size, the drawing should be limited to the smaller size automatically.

    Or you could “move” the touch recognition and the drawing to a another smaller subview:

    1. Create a new smaller subview.
    2. Add it to the main view.
    3. Add a UITapGestureRecognizer to the small view
    4. Handle the touches in the UITapGestureRecognizer action and do the drawing on the subview.

    ===== EDIT =====

    Here is how to do it:

    - (void)viewDidLoad{
       [super viewDidLoad];
       self.view.backgroundColor = [UIColor grayColor];
    
       canvas = [[UIView alloc] initWithFrame:CGRectMake(200.0, 200.0, 300.0, 300.0)];
       canvas.backgroundColor = [UIColor whiteColor];
       [self.view addSubview:canvas];
    
       UIImageView *iv = [[UIImageView alloc] initWithImage:nil];
       iv.frame = canvas.frame;
       [self.view addSubview:iv];
       self.drawImage = iv;
       [iv release];
    
       UIPanGestureRecognizer *panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(userPanned:)];
       [canvas addGestureRecognizer:panRec];    
       [panRec release];
    }
    
    
    - (void)userPanned:(UIPanGestureRecognizer *)recognizer {
       CGPoint touchPoint = [recognizer locationInView:canvas];
    
       if (recognizer.state == UIGestureRecognizerStateBegan) lastTouchPoint = touchPoint;
    
          if (CGRectContainsPoint(CGRectMake(0.0, 0.0, canvas.frame.size.width, canvas.frame.size.height), touchPoint)) {
             UIGraphicsBeginImageContext(canvas.frame.size);
             [drawImage.image drawInRect:CGRectMake(0.0, 0.0, canvas.frame.size.width, canvas.frame.size.height)];
             CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
             CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
             CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
             CGContextBeginPath(UIGraphicsGetCurrentContext());
             CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastTouchPoint.x, lastTouchPoint.y);
             CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), touchPoint.x, touchPoint.y);
             CGContextStrokePath(UIGraphicsGetCurrentContext());
             drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
             UIGraphicsEndImageContext();
    
             lastTouchPoint = touchPoint;
         }
    }
    

    canvas, lastTouchPoint and drawImage are iVars.

    I did not implement the doubleTap and Tap routines to clear the image and to draw a point, but this should be enough to get you started.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw this C# using statement in a code example: using StringFormat=System.Drawing.StringFormat; What's that
In VB.NET I'm drawing an ellipse using some code like this. aPen = New
I'm trying to draw a textured quad in XNA using the sample code from
I've been using this method (I think I got it from one of Apple's
Ive taken over some prize drawing code. I can see the person is using
I am using a library called EXIFextractor to extract metadata information from images. This
I created a new c# project and follwed the steps from this tutorial to
This code is for transforming from colored to black and white. I tried to
I'm using this code to delete a row in a table, but it's not
I'm using the System.Drawing classes to generate thumbnails and watermarked images from user-uploaded photos.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.