On Jun 17, 2009, at 9:10 PM, Chunk 1978 wrote:

... perhaps there's a way to set the transforming object's origin to
it's center some how?  i could work with that...

The problem is a more subtle one than simply a question of what order in which to perform the transforms. What's happening is that as your cube scales in size, its origin changes and that screws up the translation.

I created a sample project that moves a small image from (0,0) to the center of the screen, while rotating it by 90 degrees and scaling it by a factor of 2 in each direction. You can download it from here:

http://www.restlessbrain.com/AffineTs.zip

Or you can simply look at the code below. The key lines are:

    CGAffineTransform transform;

    transform = CGAffineTransformMakeScale(kScaleX, kScaleY);
    transform = CGAffineTransformTranslate(transform,
        targetCenter.x / kScaleX, targetCenter.y / kScaleY);
    transform = CGAffineTransformRotate(transform, DEGS_TO_RADS(90));

Note how you have to divide the target position coordinates by the same scale factor you're changing the size of the object you're applying the transform to.

The constants and macro are simply:

#define kAnimDuration           2.0

#define kScaleX                 2.0
#define kScaleY                 2.0

#define DEGS_TO_RADS(DEGS)      (DEGS * M_PI / 180.0)

- (IBAction) actionApplyTransform: (id) sender
{
    // NSLog(@"ViewController: -actionApplyTransform:");

    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    CGPoint screenCenter = { CGRectGetMidX(screenBounds),
                             CGRectGetMidY(screenBounds) };

    CGRect imgBounds = imgView.bounds;
    CGPoint imgCenter = { CGRectGetMidX(imgBounds),
                          CGRectGetMidY(imgBounds) };

    // The image's origin is not at its center, so when
    // targetting a destination point, we must account
    // for that.
    CGPoint targetCenter;
    targetCenter.x = screenCenter.x - imgCenter.x;
    targetCenter.y = screenCenter.y - imgCenter.y;

    [UIView beginAnimations: nil context: NULL];
    [UIView setAnimationDuration: kAnimDuration];
    [UIView setAnimationDelegate: self];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];

    CGAffineTransform transform;

    transform = CGAffineTransformMakeScale(kScaleX, kScaleY);

    // As the image scales in size, its origin changes.
    // Thus, we have to scale the destination point by
    // same scale factor, but "in reverse".
    transform = CGAffineTransformTranslate(transform,
        targetCenter.x / kScaleX, targetCenter.y / kScaleY);

    transform = CGAffineTransformRotate(transform, DEGS_TO_RADS(90));

    imgView.transform = transform;

    [UIView commitAnimations];
}

// Wagner
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to