【问题标题】:Accessing the current position of UIView during animation在动画期间访问 UIView 的当前位置
【发布时间】:2023-03-03 21:42:01
【问题描述】:

我正在尝试查找在屏幕上显示动画的 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 的当前位置?

【问题讨论】:

    标签: iphone objective-c ios animation cgpoint


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!!除了没有找到“-presentationLayer”方法的警告之外,这非常有效。我必须导入框架吗?
    • 完全可以接受答案? :-) 是的,我忘了提到你应该链接 QuartzCore.framework 并添加一个 import #import ,就像你在处理 CALayers 时所做的那样。
    • 有没有类似的东西可以获取当前的变换?
    【解决方案2】:

    斯威夫特 5: 在动画期间获取临时位置和大小(CGRect):

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

    【讨论】:

      猜你喜欢
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 2014-08-31
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      相关资源
      最近更新 更多