質問

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
                    delay:0.0
                    options: UIViewAnimationOptionCurveLinear
                    animations:^{
                    CGAffineTransform trans = CGAffineTransformTranslate(self.ZombieView.transform, 0, 640);
                        ZombieView.transform=trans;
                    }
                     completion:nil];

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.

Addendum:

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.

他のヒント

if you just want to translate this view, why dont you set a new frame to that image?

    CGRect newFrame = CGRectOffset(ZombieView.frame, 0, 600)
    [UIView animateWithDuration:9.5
                          delay:0.0
                        options: UIViewAnimationOptionCurveLinear
                     animations:^{
                         ZombieView.frame = newFrame;
                    }];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top