user3447979 user3447979 - 1 year ago 52
Objective-C Question

CGAffineTransformTranslate is Shrinking an image Instead of moving it

I am trying to make a code that programmatically makes an image appear (eventually multiple images), and then translates the image. When I try to translate this image, the image doesn't move and instead shrinks. I have tested the translation code on another image that I pasted into the storyboard already and it worked.

Here is my code:

//this makes a zombie/person
UIImageView *ZombieView =[[UIImageView alloc] initWithFrame: CGRectMake(135 , 0, 50, 75)];
UIImage *Zombie=[UIImage imageNamed:@"free-vector-stick-figure-clip-art_105575_Stick_Figure_clip_art_hight.png"];
[ZombieView setImage:Zombie];
[self.view addSubview:ZombieView];

//This makes the Person move down until he is behind the railing
[UIView animateWithDuration:9.5
options: UIViewAnimationOptionCurveLinear
CGAffineTransform trans = CGAffineTransformTranslate(self.ZombieView.transform, 0, 640);

I this is my header file:

@property (strong, nonatomic) IBOutlet UIImageView *ZombieView;

@property (strong, nonatomic) IBOutlet UIImage *Zombie;

I am sure the answer is simple, but I am new to coding in IOS so please keep this in mind. Thank You!


I don't see any problem with your animation code, except that within the animation block you are referencing to the ZombieView that is connected to the one via IBOutlet, not the one you declare directly above the animation block.

And, you are using the transform of the UIImageView (self.ZombieView) connected through the IBOutlet to create a translate that you apply to the transform of the UIImageView (ZombieView) that is declared directly above the animation block.

Perhaps that is the reason.

Other than that, your animation should work.


I just remembered that using transforms when autolayout is enabled could produce unexpected results. So, in your case, if self.ZombieView is created on storyboard with that turned on and with you using that transform and apply it to the ZombieView transform, what you are experiencing can be explained.

So: within the animation block, change self.ZombieView.transform to ZombieView.transform and it should do as you expect.

Hope this helps.