Little help please? UIImage moving...

Discussion in 'iOS Development' started by cocotutch, Nov 15, 2009.

  1. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Hoi There People! (Said with yobbo Aussie accent)

    Please Register or Log in to view images



    I know that you can move a UIImage with a touch by its ".center" position. Thats VERY easy. I just can't seem to be able to move an image by the area I tap. I want to tap on the bottom of an image, and drag to have the image move up staying pinned to my finger until I let go.

    Is there a way I can do this?

    I.e. "image.center = touchPos" -> "image.bottom = touchPos"??

    thanks,

    cocotutch
  2. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    [OBJC]

    // where 'startLocation' and 'currentLocation' are CGPoint structs.

    - (void)touchesBegan

    Please Register or Log in to view images

    NSSet *)touches withEvent

    Please Register or Log in to view images

    UIEvent *)event {

    startLocation = [[touches anyObject] locationInView:self.view];

    // ...
    }

    - (void)touchesMoved

    Please Register or Log in to view images

    NSSet *)touches withEvent

    Please Register or Log in to view images

    UIEvent *)event {

    currentLocation = [[touches anyObject] locationInView:self.view];

    CGRect newFrame = myView.frame;

    newFrame.origin.x = currentLocation.x - startLocation.x;
    newFrame.origin.y = currentLocation.y - startLocation.y;

    [myView setFrame: newFrame];
    }
    [/objc]

    Simple arithmetic.
  3. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    I get horrid stuttering if I have "image.frame" in:

    [OBJC]CGRect newFrame = image.frame;[/OBJC]

    And it won't move at all if I have "Self.view.frame" in the same place.

    Is there something I am missing?

    Thanks.
  4. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    [OBJC]- (void)touchesMoved:etc: {
    CGPoint touchLocation = [[touches anyObject] locationInView:theSuperView];
    if (CGRectContainsPoint(imageView.frame, touchLocation)) {
    CGPoint centerPoint = theImageView.center;
    CGSize touchOffset = CGSizeMake(centerPoint.x - touchLocation.x, centerPoint.y - touchLocation.y)

    imageView.center = CGPointMake(touchLocation.x + touchOffset.width, touchLocation.y + touchOffset.height);
    }
    }[/OBJC]
  5. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    I'm trying to move it from the bottom of the image, not the center. Thanks though Skylar, it still pins to the center point.

    Please Register or Log in to view images

    Please Register or Log in to view images

    Please Register or Log in to view images



    Gets rid of the stuttering though!
  6. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    I wrote that off the top of my head. Perhaps the math is wrong. [EDIT: Looking at the code, I use size.width twice, that's most likely the error.] Regardless, if you're offsetting the new center point, it will work how you want it. The only difference you will have to make to the code provided is to make the new y coordinate of the center offset to enough to make the "grab point" of the image the bottom. Get that by subtracting CGRectGetMaxY(theViewYou'reMoving) from the centerpoint instead of the touchLocation.y.
  7. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Sorry, cocotutch sucks at Math (and any form of it), im waiting for the blast, but could u help more?
  8. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    Wow, much more elegant (and efficient).

Share This Page