user2963333
user2963333

Reputation: 135

Animation of UIImage initiated by button

I wish to animate a 50x50 UIImage across the screen like an ECG blip at the touch of a button.

I am using a test project and have this working without a button:

@interface IPhoneViewController ()

@end

@implementation IPhoneViewController

- (void)viewDidLoad

{
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}


 -(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [UIView animateWithDuration:1.0F animations:^{
    CGPoint oldPosition = self.dot.layer.position;
    CGPoint newPosition = CGPointMake(oldPosition.x + 170, oldPosition.y);
    self.dot.layer.position = newPosition;


    }];

 }



@end

When I add the line - (IBAction)onButtonPressed:(id)sender; { just above this code, an error is created at the line -(void)viewDidAppear:(BOOL)animated { stating "Use of undeclared identifier 'viewDidAppear'".

Firstly, how can I start the animation with the button and secondly, I would like to alter the direction of the animation in an up and down motion like ECG machines do, but if I add another set of directions after the first, only the last instructions are implemented.

 -(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

[UIView animateWithDuration:1.0F animations:^{
    CGPoint oldPosition = self.dot.layer.position;
    CGPoint newPosition = CGPointMake(oldPosition.x + 170, oldPosition.y);
    self.dot.layer.position = newPosition;


}];
 [UIView animateWithDuration:1.0F animations:^{
     CGPoint oldPosition = self.dot.layer.position;
     CGPoint newPosition = CGPointMake(oldPosition.x + 10, oldPosition.y - 100);
     self.dot.layer.position = newPosition;


 }];

}

Thanks for any help.

Upvotes: 1

Views: 295

Answers (1)

Tcharni
Tcharni

Reputation: 644

Here is your solution:

- (IBAction)onButtonPressed:(id)sender{
[UIView animateWithDuration:1.0F animations:^{
    CGPoint oldPosition = self.dot.layer.position;
    CGPoint newPosition = CGPointMake(oldPosition.x + 170, oldPosition.y);
    self.dot.layer.position = newPosition;


}completion:^(BOOL finished){
[UIView animateWithDuration:1.0F animations:^{
    CGPoint oldPosition = self.dot.layer.position;
    CGPoint newPosition = CGPointMake(oldPosition.x + 10, oldPosition.y - 100);
    self.dot.layer.position = newPosition;


}];
}];


}

This should work, and your animations should be one after the other.

Upvotes: 2

Related Questions