在动画期间访问UIView的当前位置 [英] Accessing the current position of UIView during animation

查看:197
本文介绍了在动画期间访问UIView的当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我像这样创建和动画imageView

I create and animate the imageView like so

-(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分钟调用一次这个方法第二个

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

-(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的当前位置?

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?

推荐答案

你可以使用presentationLayer - 一个属性的CALayer提供与当前正在显示的图层版本的近似。只需像这样使用它:

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:

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

这篇关于在动画期间访问UIView的当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆