UIImageView动画坐标

设置了一个UIImageVIew水平方向的动画,代码中使用到NSTimer

timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
-(void)onTimer
{
    [UIView animateWithDuration:10.0f animations:^{
          //Moving_Cloud is an image view
            Moving_Cloud.frame = CGRectMake(200.0f, 150.0f, Moving_Cloud.frame.size.width, Moving_Cloud.frame.size.height);
           }];

}

现在的问题时我需要获取"Moving_Cloud"移动后的坐标。
请帮忙,谢谢。

你可以在动画完成后获取"Moving_Cloud"的坐标。你可以使用UIView类的animateWithDuration:animations:completion:方法来实现这个功能。


首先,你需要修改你的onTimer方法,把它改为这个样子:

-(void)onTimer
{
    [UIView animateWithDuration:10.0f animations:^{
          //Moving_Cloud is an image view
            Moving_Cloud.frame = CGRectMake(200.0f, 150.0f, Moving_Cloud.frame.size.width, Moving_Cloud.frame.size.height);
           } completion:^(BOOL finished) {
               // 在这里获取Moving_Cloud的坐标
               CGPoint position = Moving_Cloud.frame.origin;
               NSLog(@"Moving_Cloud position: %@", NSStringFromCGPoint(position));
           }];
}

在动画完成后,你可以使用Moving_Cloud.frame.origin来获取它的坐标。


你还可以使用CGRectGetMinX、CGRectGetMinY、CGRectGetMaxX和CGRectGetMaxY来获取"Moving_Cloud"的横纵坐标。

例如:

CGFloat x = CGRectGetMinX(Moving_Cloud.frame);
CGFloat y = CGRectGetMinY(Moving_Cloud.frame);