Question

I'm trying to animate the same images in a different position like this:

    for (int i = 0; i == 3; i++) {

        UIImageView *imageToAnimate = [[UIImageView alloc] initWithFrame: imageFrameTwo];

        [imageToAnimate setImage: [UIImage imageNamed: imageTwo]];

        CGPoint point0 = imageTwoImage.layer.position;
        CGPoint point1 = { point0.x + 10*i, point0.y - 10*i +  50};

        CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position.y"];
        anim.fromValue  = @(point0.y);
        anim.toValue    = @(point1.y);
        anim.duration   = 15.0f;
        anim.repeatCount = HUGE_VALF;
        anim.cumulative  = NO;
        anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

        // First we update the model layer's property.
        imageToAnimate.layer.position = point1;

        // Now we attach the animation.
        [imageToAnimate.layer  addAnimation:anim forKey:@"position.y"];
        [theView addSubview:imageToAnimate];
    }

But nothing happens. Without cycle it works well. Am I doing something wrong?

Was it helpful?

Solution

I won't go into details but you can try using animation blocks. 0 and 100 are the origin points. Also, make sure you are not using autoLayout, you should either disable it, or animate using autolayout constraints instead of frame changes.

[UIView animateWithDuration:0.3 
                      delay:1 
                    options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState                                 
                  animations:^ {

//[yourView setFrame:CGRectMake(0,100,yourView.frame.size.width,yourView.frame.size.height )];
[yourView setFrame:CGRectMake(yourView.frame.origin.x + 100,100,yourView.frame.size.width,yourView.frame.size.height )];


                                }
                  completion:^ (BOOL finished) {

                            }] ;

For example, to animate on x axis you just need to have a different x value in the

CGRectMake ( <desired x value here>, y, ....)

The line inside the animation block should slide your view to right by 100 pixels now.

OTHER TIPS

[UIView animateWithDuration:DURATION delay:DELAY options:YOUR_UIViewAnimation animations:^(void)

This should do the trick ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top