在动画期间访问UIView的当前位置

时间:2011-10-02 05:20:09

标签: iphone objective-c ios animation cgpoint

我正在尝试找到在屏幕上设置动画的iPhone ImageView的当前位置。

我像这样创建imageView并为其设置动画

-(IBAction)button:(UIButton *)sender{
    UIImageView *myImageView =[[UIImageView alloc] initWithImage:
                                     [UIImage imageNamed:@"ball.png"]];    
    myImageView.frame = CGRectMake(0, self.view.frame.size.height/2, 40, 40);    
    [self.view addSubview:myImageView];
    [myArray addObject:myImageView];

    [UIView animateWithDuration:.5 
                          delay:0 
                options:(UIViewAnimationOptionAllowUserInteraction) // | something here?)
                     animations:^{
                         myImageView.frame = CGRectOffset(myImageView.frame, 500, 0);    
                     }
                     completion:^(BOOL finished){
                         [myArray removeObject:myImageView];
                         [myImageView removeFromSuperview];
                         [myImageView release];
                     }
     ];
}

然后检测imageView的当前CGPoint我每隔60秒调用一次这个方法

-(void)findPoint{

    for (UIImageView *projectile in bullets) {        
         label.text = [NSString stringWithFormat:@"%f", projectile.center.x];
         label2.text = [NSString stringWithFormat:@"%f", projectile.center.y];
    }
}

然而,这只给了我动画结束的点,我想获得在屏幕上动画的图像的当前CGPoint。除了UIViewAnimationOptionAllowUserInteraction之外,还有一个我需要应用的选项吗?如果没有,我如何获得动画imageView的当前位置?

2 个答案:

答案 0 :(得分:48)

您可以使用presentationLayer - CALayer的一个属性,它“提供与当前显示的图层版本非常接近的”。只需使用它:

CGRect projectileFrame = [[projectile.layer presentationLayer] frame];

答案 1 :(得分:1)

迅速5: 使用以下方法获取动画过程中的临时位置和大小(CGRect):

let currentFrame = projectileFrame.layer.presentation()!.frame