iOS - 在动画之后移动UIImageView重置为原始位置,为什么?
问题描述:
我目前沮丧的问题,我似乎无法找到答案。我有一个由用户控制的UIImageView,根据他们按的方向左右移动20个像素。同时,我正尝试使用不同按钮按下的NSArray对其进行设置。但是,每次我为UIImageView设置动画时,位置都会重置。iOS - 在动画之后移动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
在左侧或右侧移动之前移回原始位置。
任何帮助/建议将不胜感激。
答
尝试在移动对象时记住位置,并且在按下动画按钮时重置它。
-(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];
}
答
尽管这是一个非常古老的问题,但我遇到了同样的问题并找到了答案:UnCheck AutoLayout。
好运人谁像我一样在这个跌倒。
观看斯坦福视频15 - 可编辑文本,模态视图控制器,您将获得一切你想要的东西,但它为一个标签完成,你可以为你的图像视图。下一个视频16 - 核心运动,分段控制和警报--YouTube让你一起做多个动画。 – 2013-03-15 06:39:55
您可以显示startAnimating的代码,也可以解释我们在哪里设置了位置点 – Sandro 2013-03-15 09:57:12