在动画中访问UIView的当前位置。

时间:2021-12-07 15:54:27

I am trying to find the current position of an iPhone ImageView that is animating across the screen.

我正在试图找到iPhone ImageView的当前位置,它正在屏幕上显示动画。

I create and animate the imageView like so

我像这样创建和动画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];
                     }
     ];
}

then to detect the current CGPoint of the imageView I call this method every 60th of a second

然后,为了检测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];
    }
}

However this only gives me the point where the animation ends, I want to get the current CGPoint of the image that is animating across the screen. Is there an option that I need to apply other than UIViewAnimationOptionAllowUserInteraction to do this? If not, how do I get the current position of an animating imageView?

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

2 个解决方案

#1


44  

You can use the presentationLayer - a property of the CALayer that "provides a close approximation to the version of the layer that is currently being displayed". Just use it like this:

您可以使用presentationLayer——CALayer的一个属性,它“提供了与当前显示的层的版本非常接近的特性”。就像这样使用:

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

#2


-1  

Much easier to set up a KVO for view.frame, as outlined in this answer. You can inspect the new value to get the new frame as a CGRect.

更容易为视图设置一个KVO。您可以检查新值以获得新框架作为CGRect。

#1


44  

You can use the presentationLayer - a property of the CALayer that "provides a close approximation to the version of the layer that is currently being displayed". Just use it like this:

您可以使用presentationLayer——CALayer的一个属性,它“提供了与当前显示的层的版本非常接近的特性”。就像这样使用:

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

#2


-1  

Much easier to set up a KVO for view.frame, as outlined in this answer. You can inspect the new value to get the new frame as a CGRect.

更容易为视图设置一个KVO。您可以检查新值以获得新框架作为CGRect。