ios - 当UIView正在做动画时,如何获取UIView的frame?

查看:456
本文介绍了ios - 当UIView正在做动画时,如何获取UIView的frame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

当UIView正在做动画时,如何获取UIView的frame?

解决方案

使用presentationLayer属性可以获取最新frame。
Sample:

- (IBAction)move {
    CGRect destRect = self.frame;
    destRect.origin.x = 300 - destRect.origin.x;

    __block BOOL animationComplete = FALSE;
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    [UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationCurveEaseInOut
                     animations:^{
                         [self setFrame:destRect];
                     }
                     completion:^(BOOL finished) {
                         animationComplete = YES;
                         [[UIApplication sharedApplication] endIgnoringInteractionEvents];
                     }];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, (unsigned long)NULL), ^{
        while (!animationComplete) {
            dispatch_sync(dispatch_get_main_queue(), ^{
                CALayer *layer = self.layer.presentationLayer;
                CGRect frame = [layer frame];
                CGPoint point = [layer position];
                float currentTranslation = [[layer valueForKeyPath:@"transform.translation.x"] floatValue];
                NSLog(@"%.1f : %.1f : %.1f : %.1f : %.1f : %.1f",frame.origin.x, currentTranslation, frame.origin.x, layer.bounds.origin.x, layer.position.x, point.x);
            });
        }
    });
}

参考链接

补充:
上面Sample的原理是动画开始后,在低优先级子线程中通过presentationLayer来不断获取最新frame,直到动画结束,当然这里你可以使用NSTimer来实现。

这篇关于ios - 当UIView正在做动画时,如何获取UIView的frame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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