[Tutorial] Drawing to the screen.

Discussion in 'iOS Development' started by SkylarEC, Jan 16, 2009.

  1. rrichar New Member

    Member Since:
    Sep 1, 2010
    Message Count:
    3
    can someone please help me make a clear button ?
  2. nghine_rbc New Member

    Member Since:
    Aug 5, 2010
    Message Count:
    3
    Hello, i have a question.
    I do everything follow the tutorial then build and run very good. But when i zoom in the "drawImage" object by multi touch on screen then i draw very slow and my app have CRASH.

    anyone know that reason? Help me!
    this is my TouchBegan method
    Code:
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {	
    		touches = [event allTouches];
    		switch ([touches count]) {
    			case 1: { //Single touch	
    			} break;
    			case 2: { //Multi Touch
    				//Track the initial distance between two fingers.
    				UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
    				UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
    				initialDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:[self drawImage]]
    														 toPoint:[touch2 locationInView:[self drawImage]]];
    			} break;
    		}
    }
    and touchMoved Method
    Code:
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    		touches = [event allTouches];
    		switch ([touches count])
    		{
    		
    			case 2: {
    				//The image is being zoomed in or out.
    				UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
    				UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
    				//Calculate the distance between the two fingers.
    				finalDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:[self drawImage]]
    													   toPoint:[touch2 locationInView:[self drawImage]]];
    
    				//Check if zoom in or zoom out.
    				if(initialDistance > finalDistance&& drawImage.frame.size.width >160 && drawImage.frame.size.height >300) {		
    					//here decreasing the rect width,height				
    					drawImage.frame = CGRectMake(0,0, drawImage.frame.size.width - (initialDistance-finalDistance), drawImage.frame.size.height - (initialDistance-finalDistance)); 
    					drawImage.center = CGPointMake(160,230);
    					initialDistance = finalDistance;	
    				}
    				else if(initialDistance < finalDistance && drawImage.frame.size.width <740 && drawImage.frame.size.height < 880 ){
    					//here increasing the rect width,height
    					drawImage.frame = CGRectMake(0,0, drawImage.frame.size.width + (finalDistance-initialDistance), drawImage.frame.size.height + (finalDistance-initialDistance)) ; 
    					drawImage.center = CGPointMake(160,230);
    					initialDistance = finalDistance;
    
    				}
    			} break;
    		}
    }
    Best Regards!
    ------------------double post merged------------------
    may be this code can help you
    Code:
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
  3. rrichar New Member

    Member Since:
    Sep 1, 2010
    Message Count:
    3
    If I put the following line of code in my Clear button the screen be cleared of all he stuff the draw on the screen ?
    Code:
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
    How about allowing drawing on a certain section of the screen ?
  4. nghine_rbc New Member

    Member Since:
    Aug 5, 2010
    Message Count:
    3
    if you put this code before you are drawing line on the screen(an UIImageView), then instead you draw a line on screen, you will erase screen under the line you have just draw.
    Code:
    [COLOR="Red"]CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);[/COLOR]
    //CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    .
    .
    .
    This code like a trick.:eek:
  5. nghine_rbc New Member

    Member Since:
    Aug 5, 2010
    Message Count:
    3
    seems no one wants to help me solve this problem. OK. I will ignore that. not allowed zoom in or zoom out. But how can i undo the picture after draw in it...?
  6. jjboyle New Member

    Member Since:
    Sep 28, 2010
    Message Count:
    1
    this is a great post and works perfectly...i've been wrestling with it however for drawing in a rotated view. when i rotate to landscape the drawing is offset from the touch point by a significant factor, but the factor changes as i move my cursor around the screen. i suspect a scaling or content mode issue, but after countless hours and open ticket with apple developer support, i'm not getting anywhere. any one get this or a variant working in landscpape?
    ------------------double post merged------------------
    drawImage.image = nil;
  7. cubase23 Guest

    Member Since:
    Message Count:
    0
    go back to older state

    Hi, it's possible to go back to older state or undo? I've found CGContextSaveGState and CGContextRestoreGState but I don't know if is the way to do that and how to implement it

    Thanks!
  8. SkylarEC Super Moderator Emeritus

    Member Since:
    Sep 19, 2007
    Message Count:
    6,640
  9. Unregistered Guest

    Member Since:
    Message Count:
    0
    Build Errors

    Hi Skylar

    Thanks for the tutorial - this is exactly what I have been looking for.

    When I ran it the first time in the simulator, I got 157 errors. I got hat fixed and it worked good.

    I connected my iPad to it to run on the device, but I get two build errors I am not sure how to fix.

    They are:

    CFBundleIdentifier contains an illegal character

    and

    invalid bundle identifier

    I am not sure if this is your code or me at my end?

    Any ideas ?

    Iain
  10. SkylarEC Super Moderator Emeritus

    Member Since:
    Sep 19, 2007
    Message Count:
    6,640
    The CFBundleIdentifier is in your Info.plist. That error's on you.

    It's the com.yourapp.name. Go in and change that to all valid characters.