UIImageView positioning problems?
i wrote the following code:
-(void)viewDidLoad{
JumpVelocity = 10;
aStandRightArray = [[NSArray alloc] initWithObjects:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"aStandR1" ofType:@"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"aStandR2" ofType:@"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"aStandR3" ofType:@"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"aStandR2" ofType:@"png"]],
nil];
aJumpRightArray = [[NSArray alloc] initWithObjects:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"aJumpR" ofType:@"png"]], nil];
aStandRight = [[UIImageView alloc] initWithFrame:CGRectMake(0, 242, 55, 65)];
aStandRight.animationImages = aStandRightArray;
aStandRight.animationDuration = 0.5;
[self.view addSubview:aStandRight];
aJumpRight = [[UIImageView alloc] initWithFrame:CGRectMake(0, 234, 69, 65)];
aJumpRight.animationImages = aJumpRightArray;
[self.view addSubview:aJumpRight];}
-(IBAction)buttonJumpRight:(id)sender{
[aStandRight stopAnimating];
[aStandRight removeFromSuperview];
[aJumpRight startAnimating];
jumpTimer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(playerJumpRight) userInfo:nil repeats:YES];}
-(void)playerJumpRight{
[aJumpRight removeFromSuperview];
aJumpRight.center = CGPointMake(aJumpRight.center.x + JumpVelocity, 234);
[self.view addSubview:aJumpRight];
if(aJumpRight.center.x >= 84.0)
{
[jumpTimer invalidate];
jumpTimer = nil;
[aJumpRight stopAnimating];
aStandRight.center = CGPointMake(84, 242);
[aStandRight startAnimating];
[self.view addSubview:aStandRight];
}
}
basically what i am trying to do here is load up a standing animation, then when the user presses the buttonJumpRight button i stop then unload the standing animation. after that i load up the jump animation and begin to move it across the screen with the playerJumpRight function.
everything seems to work fine with two exception:
the jump animation moves like expected along the x axis but for some reason does not keep its original y position which in the code above is "2开发者_如何转开发34".
when the jump animation x position meets the requirements for the if statement everything works like expected newly created position for the standing animation is way off of the desire position of (84, 242).
i been searching for quite some time, trying out many different possible solutions but fail at every try. please excused my newbieism as i just start coding for ios/objective c.
i greatly appreciate any help you can offer.
If you remove a subview from its superview, then the center
property becomes meaningless as it refers to the superview's coordinate system. Don't remove and re-add aJumpRight
from the superview, just amend it's center
property, this will move the view along which I think is what you are after.
Note that you can also just animate the change to the center
using block-based animation, see the UIView class reference here for details.
Also, you may be confusing the center
of a view with it's frame.origin
. The latter is the top left of the view. I don't think setting the center
for a view without a superview has any effect, though I'm not sure on that one. You can definitely set the frame origin in this situation.
精彩评论