简体   繁体   中英

animate image/UIView iOS

On the homepage of my application I want to animate a PNG image sliding into the view, then sliding out again before a second image slides in, then out. Time just for the viewer to read before each disappears. I dont know how to do this and would like some input ? seems pretty simple I hope it is and someone can direct me how to do it. I also want it on loop, so whenever you go back to the screen it will animate still.

Any ideas please? Only a day or so to finish

You can use Core Animation for this. It's real easy to do. Example animation:

UIImageView *imageView = ...;
imageView.frame = CGRectMake(-160, 100, 250, 200);
[UIView animateWithDuration:1.5 animations:^{
    imageView.frame = CGRectMake(160, 100, 250, 200);
}];

This animates a UIImageView from x-coordinate -160 to 160 in 1.5 seconds.

More or less just expanding on Tom van Zummeren's answer. This should be enough to get the basic idea across.

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

    UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, -280, 280, 280)];
    [myImageView setImage:[UIImage imageNamed:@"1.png"]];
    [self.view addSubview:myImageView];

    [UIView animateWithDuration:2.0 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [myImageView setCenter:self.view.center];
    }completion:^(BOOL done){
        [UIView animateWithDuration:2.0 delay:3.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
            [myImageView setCenter:CGPointMake(myImageView.center.x, self.view.bounds.size.height + (myImageView.bounds.size.height / 2))];
        }completion:^(BOOL done){
            [myImageView setFrame:CGRectMake(20, -280, 280, 280)];
            NSLog(@"%@",NSStringFromCGRect(myImageView.frame));
            [myImageView setImage:[UIImage imageNamed:@"2.png"]];
        }];
    }];
}

//Starting point

pImgFlowing.frame = CGRectMake(544, 817, 84, 18);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

//Ending point

pImgFlowing.frame = CGRectMake(544, 774, 84, 61);
[UIView commitAnimations];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM