CGAffineTransformMakeTranslation

Discussion in 'iOS Development' started by deepp, Mar 5, 2009.

  1. deepp

    deepp New Member

    Joined:
    Sep 4, 2008
    Messages:
    73
    Likes Received:
    0
    Hi,
    I have been trying to move a tennis ball in screen using this method and to do that i have done everything lik setting delegate to self starting animation.
    What i want to do is when i touch tennis ball it should jump in up direction and it does but when again i tap to ball while its in between moving down direction it does not move up.So my question is when i translte ball using CGAffineTransformMakeTranslation() method does balls frame also get moved?As i tried to get its x, y co-ordintes and width and height too but its giving 0,0 for x and y cordinate and garbage value for width and height.I am checking height and width coz i am using a condition CGRectContainsPoint to check the tap point and ball position but its not working.
    can anyone tell me where i am goin wrong?
  2. deepp

    deepp New Member

    Joined:
    Sep 4, 2008
    Messages:
    73
    Likes Received:
    0
    There's no one who can help me in the same?can anyone tell me if CGAffineMakeTranslation changes the co-ordinate of a view i just need to know that and if changes then how can i track them ?
    Pleeeez any1 help me...
  3. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Nevermind the CoreGraphics. Just use theView.center = newPoint in your animation block. It will make life a whole lot easier for you.

    As for why your code isn't working, we have no clue. We can't tell you unless we have some code to look at.


    EDIT: Also, if you are checking the location if the ball using coordinates alone, that would be fail. Please see my Multitouch/Single Touch/Tap handlif tutorial in re tutorial section. The tut with the beach ball.
  4. deepp

    deepp New Member

    Joined:
    Sep 4, 2008
    Messages:
    73
    Likes Received:
    0
    Thanks Skylar for your reply and ofcourse i have been learning from you only ,using your sample code and guidance.
    And i have seen both the tutorial and using that only, i did simple change that i am displaying a simple ball image by initializing it with a rect(200,400,25,25) and rest is your code just one more change i did is changed the
    CGAffineTransform to CGAffineTransformMakeTranslation(0,-200)
    and now m checking that whenevr anyone clicks the ball it should move up.But when i click the ball from its original position it moves up but while its moving down and i click it it does not respond to that click until it reaches its original position.

    And sorry i did not print my code in this coz i dont have net connection in my macbook to copy and paste.
  5. deepp

    deepp New Member

    Joined:
    Sep 4, 2008
    Messages:
    73
    Likes Received:
    0
    Hi i have attached me .m file which is a subclass of viw.
    Please someone check it and let me know where i am goin wrong.
    Thanks

    Attached Files:

  6. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Why did you post it into a Word Document file? Why not just the .m?
  7. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Again, translating isn't the most appropriate thing to do here. Simply move the ball by its center.

    This is the "appropriate" UImageView initializer. Using this method requires no setting of the frame. Simply, set the origin of the image view, or the center, or whatever and you're good to go. That way you don't accidently stretch or compress the image. i've also added some memory management for you, because as of now, you're leaking your imageView and all its ivars (class variables), including the image itself. Ouch!

    Code:
    UIImageView *imageView = [[UIImageView alloc] initWithImage:aUIImageVariable];
    imageView.origin = someCGPoint;
    [someView addSubview:imageView];
    [imageView release];
    To get the frame of the ball view, use this instead, it's quicker, and easier on the system.
    Code:
    CGRect ballRect = ballView.bounds; //or ballView.frame : It's the same thing.
    Or, instead of wasting the memory on a pointless CGRect, just refer to the frame itself. Anytime you'd refer to the ballRect, refer to ballView.bounds. Obviously, I'm taking liberties with your variable names. You should look into Objective-C naming conventions, but that's neither here nor there.




    NOW: To fix the problem you actually have. User the CENTER point, and set that in the animation block, instead of translation.
    1. Move your UIView animation block into its own method.
    2. In your mousedown, upon checking it's within the ball's frame, call that method.
    3. When the ball begins it's "up" motion, set a BOOL called "ballIsBouncing," or whatever to TRUE.
    4. So long as that boolean value is not TRUE /before/ it is set in the method, set the original center point into a CGPoint variable.
    5. Track the number of times the user touches the ball!
    6. In your "down" motion's method, set the point to animate the center to to the original point, as defined in that CGPoint you set in the "up" method. This way, your ball will always fall back down to Earth, and not to where the
      finger last touched the screen.
    7. Set the time you animate the reset of the center to its original point to the time the up animation lasts times the number of times the user taps the ball. Adjust this appropriately until you have a smooth animation. Remember that the user taps the ball in the fraction of the time it takes to go up. the user can also tap it on its downward fall. What I gave here is "good enough," but far from perfect.
    8. At the end of your moving down's animationDidStop selector, set the ballIsBouncing BOOL to NO.

    This should be "good enough" to get you going.




    Also, why are you doing all this within a view? You should be using a UIViewController to control your views.
  8. deepp

    deepp New Member

    Joined:
    Sep 4, 2008
    Messages:
    73
    Likes Received:
    0
    Thanks a lot

    Thank you very much Skylar for such a descriptive help with an explanatio for other memory related issues.It would definitely help me.I would definitely try now and see the result.

    Thanks a lot.

    Please Register or Log in to view images

Share This Page