user3338143
user3338143

Reputation: 81

Xcode How to move an image up and down animation

Hey im trying to move an imageview down to the middle of the screen then back up to the top . here is my code , right now it just moves to the middle and stays there .

-(void)lineGoBack
{
    //move diagonally down
    line.center = CGPointMake(line.center.x,
                              line.center.y-10);
    //start time for first method
    [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(LineMovement) userInfo:nil repeats:NO];
}


//The LINE
-(void)LineMovement
{ 
    lineMovementOffset = CGPointMake(line.center.x, screenSizeY/2);

    //move diagonally up
    line.center = CGPointMake(line.center.x,
                              line.center.y+10);

    //start time for second method
    [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(lineGoBack) userInfo:nil repeats:NO];
}

Upvotes: 2

Views: 9067

Answers (2)

Paul Hunter
Paul Hunter

Reputation: 4463

I don't like using blocks based animations for chained animations - they tend to grow difficult to read. I would suggest using keyframe animations. This also enables you to easily define a number of intermediary points to your animation, or add more advanced features to your animation, e.g. curves.

Here is an example of how you can animate a UIView linearly between three points.

CGPoint startPoint = (CGPoint){100.f, 100.f};
CGPoint middlePoint = (CGPoint){400.f, 400.f};
CGPoint endPoint = (CGPoint){600.f, 100.f};

CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, startPoint.x, startPoint.y);
CGPathAddLineToPoint(thePath, NULL, middlePoint.x, middlePoint.y);
CGPathAddLineToPoint(thePath, NULL, endPoint.x, endPoint.y);

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.duration = 3.f;
animation.path = thePath;
[self.animatedView.layer addAnimation:animation forKey:@"position"];
self.animatedView.layer.position = endPoint;

To do an infinite animation between two points, you could use the code below. Applying it to a UIImageView would work the same.

CGPoint startPoint = (CGPoint){100.f, 100.f};
CGPoint endPoint = (CGPoint){400.f, 400.f};

CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, startPoint.x, startPoint.y);
CGPathAddLineToPoint(thePath, NULL, endPoint.x, endPoint.y);

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.duration = 3.f;
animation.path = thePath;
animation.autoreverses = YES;
animation.repeatCount = INFINITY;
[self.animatedView.layer addAnimation:animation forKey:@"position"];

Upvotes: 8

cweinberger
cweinberger

Reputation: 3588

I would propose to use [UIView -animateWithDuration:animations:completion] for that:

[UIView animateWithDuration:0.5f
                 animations:^{

                     imageView.center = CGPointMake(imageView.center.x, imageView.center.y + 10.0f);

                 } completion:^(BOOL finished) {

                     [UIView animateWithDuration:0.5f
                                      animations:^{

                                          imageView.center = CGPointMake(imageView.center.x, imageView.center.y - 10.0f);

                                      } completion:^(BOOL finished) {

                                      }];
                 }];

See Apple Documentation.

Besides that: method names shouldn't start with an uppercase letter

Upvotes: 4

Related Questions