我有兴趣获得一个动画,不仅可以动画从A点到B点,还可以改变背景图像。 假设初始位置为0,0且最终位置为0,100;持续时间是1秒,我有4个背景图像,然后我希望背景每0/4秒和25像素更改。
到目前为止,我有下一个动画图像的代码,但我仍然需要实现这个动作,我不知道如何做到这一点。
先谢谢,这是我到目前为止的代码:
UIImageView * animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50,50)];
testArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"test_1.png"], [UIImage imageNamed:@"test_2.png"], [UIImage imageNamed:@"test_3.png"],nil];
animTime =3.0;
[animatedImageView setAnimationImages:testArray] ;
animatedImageView.animationDuration = animTime;
animatedImageView.animationRepeatCount = 1;
[self.view addSubview: animatedImageView];
[animatedImageView startAnimating];
更新
我已将Mobile Project Lab的答案实施到我的代码中
// I have a pathArray that is in C style and is bidimensional, that stores the path in reverse
// the maps is a 2d tile map
// the dimensions of the tiles are 64x64 px
//thes size of the character is 128x128
for (int possitionInThePathArray = sizeOfPathArray - 1; possitionInThePathArray >= 0; possitionInThePathArray--) {
xWalkingDirection = pathArray[possitionInThePathArray-1][1] - pathArray[possitionInThePathArray][1];
yWalkingDirection = pathArray[possitionInThePathArray-1][0] - pathArray[possitionInThePathArray][0];
if (xWalkingDirection== 0 && yWalkingDirection == -1){
//walking animation to North for 1 tile
NSArray *testArray;
float animTime;
testArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"scientist_s0.png"],
[UIImage imageNamed:@"scientist_s1.png"],
[UIImage imageNamed:@"scientist_s2.png"],
[UIImage imageNamed:@"scientist_s3.png"],
[UIImage imageNamed:@"scientist_s4.png"],
[UIImage imageNamed:@"scientist_s5.png"],
[UIImage imageNamed:@"scientist_s6.png"],
[UIImage imageNamed:@"scientist_s7.png"],
[UIImage imageNamed:@"scientist_s8.png"],
[UIImage imageNamed:@"scientist_s9.png"], nil]; // 4th image added
animTime = 10.0; // time is changed to 10.0
[myCharacterFrame setAnimationImages:testArray];
myCharacterFrame.animationDuration = animTime;
myCharacterFrame.animationRepeatCount = 1;
[myCharacterFrame startAnimating];
[UIView animateWithDuration:animTime
animations:^{
myCharacterFrame.frame = CGRectOffset(myCharacterFrame.frame, 0.0f, -64.0f); // move 100 on x axis, 0 on y axis
}
completion:^(BOOL finished){
NSLog(@"animation done");
}];
yMyCharacterFrame = yMyCharacterFrame-64.0;
myCharacterFrame.frame = CGRectMake(xMyCharacterFrame, yMyCharacterFrame, 128, 128);
myCharacterFrame.image = [UIImage imageNamed:@"scientist_s0.png"];
}else if (xWalkingDirection== 1 && yWalkingDirection == 0){
//walking animation to Est for 1 tile
.....
//and so on for all the four directions of walking
我现在面临的问题是动画没有被正确触发,因此在代码移动之前会有一个动画发生
答案 0 :(得分:3)
NSArray *testArray;
float animTime;
UIImageView *animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
testArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"test_001.png"], [UIImage imageNamed:@"test_002.png"],[UIImage imageNamed:@"test_001.png"], [UIImage imageNamed:@"test_002.png"], nil]; // 4th image added
animTime = 1.0; // time is changed to 1.0
[animatedImageView setAnimationImages:testArray];
animatedImageView.animationDuration = animTime;
animatedImageView.animationRepeatCount = 1;
[self.view addSubview: animatedImageView];
[animatedImageView startAnimating];
[UIView animateWithDuration:animTime
animations:^{
animatedImageView.frame = CGRectOffset(animatedImageView.frame, 100.0f, 0.0f); // move 100 on x axis, 0 on y axis
}
completion:^(BOOL finished){
NSLog(@"animation done");
}];