using CGPointMake in relation to other objects

Discussion in 'iOS Programming' started by Narcs, Jan 28, 2012.

  1. Narcs, Jan 28, 2012
    Last edited by a moderator: Jan 29, 2012

    Narcs macrumors newbie

    Joined:
    Jan 19, 2012
    #1
    I have a UIImageView called hitBox, and another image called mainSprite. So what I wanna do is when an IBAction is pressed, it will move hitBox to above mainSprite. I don't know if this is syntax but this is what i know moves things on the screen.

    Code:
    hitBox.center = CGPointMake(x coordinate, y coordinate);
    I just want to replace x coordinate and y coordinate with something that will place it somewhere in relation mainSprite.

    THANKS :)
     
  2. xStep, Jan 28, 2012
    Last edited by a moderator: Jan 29, 2012

    xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    To move the hitBox above mainSprite, you just have to calculate the height of each, and divide by 2 if you want the hitBox to sit right on top of mainSprite. Use that new value for the y coordinate after subtracting it from the y value of mainSprite. To align the center of each, assign the x center point to the new center for hitBox.

    If what you want is to cover mainSprite with hitBox, then you also have to change the place they in appear in their superview hierarchy. Look at the following methods in the UIView class.

    Code:
    bringSubviewToFront:
    sendSubviewToBack:
    exchangeSubviewAtIndex:withSubviewAtIndex:
    insertSubview:aboveSubview:
    insertSubview:belowSubview:
    
     
  3. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    Look at the NSView "convertPoint" and "convertRect" methods.
     

Share This Page