Question

I've seen similar questions but none of the answers given have worked in my case. I want to give my character the illusion that he is walking, but he will really remain at the same coordinates and the background will be moving instead, the way I want to do this is to use a IBAction to start my timer, my code so far looks like this.

-(IBAction)StartGame:(id)sender
{    
    PersonMovement = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(PersonMovement) userInfo:nil repeats:YES];
}

now using that timer, every 0.05 seconds I want the UIImageView of my person to alternate between (step1.png and step2.png) relaying back and forth.

Was it helpful?

Solution

You should just use UIImageViews animationImages and animationDuration properties to accomplish this swapping.

Your animationImages array would be your two images and your animationDuration would be 0.1 to show both over the span of 0.1 seconds. Then call startAnimating to start the sequence

https://developer.apple.com/library/ios/documentation/uikit/reference/UIImageView_Class/Reference/Reference.html#//apple_ref/occ/instp/UIImageView/animationImages

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *image1 = [UIImage imageNamed:@"image1"];
    UIImage *image2 = [UIImage imageNamed:@"image2"];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 200.0f, 200.0f)];
    [self.view addSubview:imageView];

    imageView.animationImages = @[image1, image2];
    imageView.animationDuration = 0.1;
    [imageView startAnimating];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top