我目前对一个似乎无法找到答案的问题感到沮丧。我有一个由用户控制的UIImageView,根据他们按下的方向向左或向右移动20个像素。与此同时,我试图用NSArray用不同的按钮按下动画。但是,每次我为UIImageView设置动画时,位置都会重置。
例如,我将向右移动80像素,并且一旦按下动画按钮,它就会在动画制作之前切换回原始位置。这是非常令人愤怒的。这是我的代码:
('pos'是CGPoint设置为(20,0)像素)
-(IBAction)moveleft {
player.center = CGPointMake(player.center.x-pos.x,player.center.y+pos.y);
}
-(IBAction)moveright {
player.center = CGPointMake(player.center.x+pos.x,player.center.y+pos.y);
}
-(IBAction)animove {
isMove = TRUE;
player.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"move0002.png"],
[UIImage imageNamed:@"move0003.png"],
[UIImage imageNamed:@"move0004.png"],
[UIImage imageNamed:@"move0005.png"],
[UIImage imageNamed:@"move0006.png"],
[UIImage imageNamed:@"move0007.png"],
[UIImage imageNamed:@"move0008.png"],
[UIImage imageNamed:@"move0009.png"],
[UIImage imageNamed:@"move0010.png"], nil];
player.animationDuration = 0.35;
player.animationRepeatCount = 1;
[player startAnimating];
}
总而言之,-(IBAction)moveleft
和-(IBAction)moveright
都可以正常工作,
-(IBAction)animove
,但一旦animove
被取消,UIImageView player
会在左右移动之前移回原来的位置。
任何帮助/建议将不胜感激。
答案 0 :(得分:3)
尽管这是一个非常古老的问题,但我遇到了同样的问题并找到了答案:UnCheck AutoLayout。
祝你好运,就像我一样偶然发现。
答案 1 :(得分:0)
答案 2 :(得分:0)
在移动对象时尝试记住位置,然后在点击动画按钮时重置它。
-(IBAction)moveleft {
rememberPosition = CGPointMake(player.center.x-pos.x,player.center.y+pos.y);
player.center = rememberPosition;
}
-(IBAction)moveright {
rememberPosition = CGPointMake(player.center.x+pos.x,player.center.y+pos.y);
player.center = rememberPosition;
}
-(IBAction)animove {
player.center = rememberPosition;
player.animationImages = [NSArray arrayWithObjects: ...];
player.animationDuration = 0.35;
player.animationRepeatCount = 1;
[player startAnimating];
}