Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-23-2011, 10:11 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 11
kpearce2 is on a distinguished road
Exclamation UITouch Follow x or y

Hi,

I may just been getting tired and completely missing the obvious but I have created an app where the user can draw on the screen. I wanted the user to be able to draw in straight lines. so I implemented this code:

Code:
     if (currentPoint.y < (firstPoint.y + 20) || currentPoint.y > (firstPoint.y - 20)) {        
        currentPoint.y = firstPoint.y;
    } 
    else {
    currentPoint.x = firstPoint.x;
    } 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x,currentPoint.y);
Which allow me to draw a straight line across the screen but not down. I want it to know when the user is make a stroke down or across and forces the line to be straight accordingly.

I also tried this

Code:
if (currentPoint.y < (firstPoint.y + 20) || currentPoint.y > (firstPoint.y - 20)) {        
        currentPoint.y = firstPoint.y;
    } 
    else if (currentPoint.x < (firstPoint.x + 20) || currentPoint.x > (firstPoint.x - 20)) {
    currentPoint.x = firstPoint.x;
    }
Thanks for any help.

Last edited by kpearce2; 09-23-2011 at 10:13 AM.
kpearce2 is offline   Reply With Quote
Old 09-24-2011, 08:51 AM   #2 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 11
kpearce2 is on a distinguished road
Default

bump
kpearce2 is offline   Reply With Quote
Old 09-25-2011, 01:27 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 280
dapis is on a distinguished road
Default

Quote:
Originally Posted by kpearce2 View Post
Hi,

I may just been getting tired and completely missing the obvious but I have created an app where the user can draw on the screen. I wanted the user to be able to draw in straight lines. so I implemented this code:

Code:
     if (currentPoint.y < (firstPoint.y + 20) || currentPoint.y > (firstPoint.y - 20)) {        
        currentPoint.y = firstPoint.y;
    } 
    else {
    currentPoint.x = firstPoint.x;
    } 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x,currentPoint.y);
Which allow me to draw a straight line across the screen but not down. I want it to know when the user is make a stroke down or across and forces the line to be straight accordingly.

I also tried this

Code:
if (currentPoint.y < (firstPoint.y + 20) || currentPoint.y > (firstPoint.y - 20)) {        
        currentPoint.y = firstPoint.y;
    } 
    else if (currentPoint.x < (firstPoint.x + 20) || currentPoint.x > (firstPoint.x - 20)) {
    currentPoint.x = firstPoint.x;
    }
Thanks for any help.
I'm not sure exactly what you're up to but I will take a stab at it.

As it stands now, you'll be getting some most bizarre behavior. Think about what happens in your current code when the user starts dragging within a 40-pixel radius vertical strip and then suddenly moves outside of that. How to fix it depends on what you're trying to do. The gist of your code is OK, but the implementation just won't work.

I am assuming that this code resides inside touchesMoved.

You should not be testing inside touchesMoved but test once somewhere else and set a global flag e.g., userDraggingHorizontally to check inside touchesMoved.

Or you could check for horizontal dragging inside a strip for say 5-20 iterations (by incrementing a counter each time touchesMoved gets entered) and if the behavior is consistent for that period of time, then set your flag and check it for the duration rather than the vertical distance from the firstPoint.

Or you could assume horizontal dragging up until the first discrepancy appears and then abort the drawing at that point.

Essentially you want the code above to look like this inside touchesMoved:

Code:
if (draggingHorizontally){        
        currentPoint.y = firstPoint.y;
    } 
     else if (draggingVertically) {
    currentPoint.x = firstPoint.x;
dapis is offline   Reply With Quote
Reply

Bookmarks

Tags
draw, paint, straight line, touch

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 390
15 members and 375 guests
13dario13, 7twenty7, eski, EvilElf, glenn_sayers, HemiMG, iOS.Lover, jarv, n00b, pbart, Pudding, sacha1996, Sami Gh, UMAD, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,905
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:17 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0