UIView 执行动画过程中获取坐标位置

UIView 在执行动画的过程中,frame 和 bounds 是不会发生变化的,因为此时我们看到变化的是展示层layer.presentationLayer,不做动画的时候显示的是模型层的内容layer.modelLayer,所以在动画过程中要获取模型层的坐标。

复制下面的代码,放在合适的位置,程序启动后,看到视图开始做动画后点击屏幕,可以在控制台看到打印出的信息:

@property (nonatomic, strong) UIView    *myView;
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.myView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    self.myView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.myView];
    
    NSLog(@"presentationLayer.bounds: x: %f y: %f w:%f h:%f",self.myView.layer.presentationLayer.bounds.origin.x,self.myView.layer.presentationLayer.bounds.origin.y, self.myView.layer.presentationLayer.bounds.size.width, self.myView.layer.presentationLayer.bounds.size.height);
    
    NSLog(@"presentationLayer.frame: x: %f y: %f w:%f h:%f",self.myView.layer.presentationLayer.frame.origin.x,self.myView.layer.presentationLayer.frame.origin.y, self.myView.layer.presentationLayer.frame.size.width, self.myView.layer.presentationLayer.frame.size.height);
    NSLog(@"start animatioin");
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        CABasicAnimation * animation = [CABasicAnimation animation];
        animation.keyPath = @"position";
        animation.duration = 4;
        animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(150, 150)];
        animation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)];
        [self.myView.layer addAnimation:animation forKey:nil];
    });
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    NSLog(@"********************************* start  presentationLayer bounds and frame *****************************");
    NSLog(@"presentationLayer.bounds: x: %f y: %f w:%f h:%f",self.myView.layer.presentationLayer.bounds.origin.x,self.myView.layer.presentationLayer.bounds.origin.y, self.myView.layer.presentationLayer.bounds.size.width, self.myView.layer.presentationLayer.bounds.size.height);
    
    NSLog(@"layer.presentationLayer.frame: x: %f y: %f w:%f h:%f",self.myView.layer.presentationLayer.frame.origin.x,self.myView.layer.presentationLayer.frame.origin.y, self.myView.layer.presentationLayer.frame.size.width, self.myView.layer.presentationLayer.frame.size.height);
    NSLog(@"");
    NSLog(@"********************************* end  presentationLayer bounds and frame *****************************");

    
    NSLog(@"********************************* start view bounds and frame *********************************");
            NSLog(@"self.myView.bounds: x: %f y: %f w:%f h:%f",self.myView.bounds.origin.x,self.myView.bounds.origin.y, self.myView.bounds.size.width, self.myView.bounds.size.height);
            NSLog(@"self.myView.frame: x: %f y: %f w:%f h:%f",self.myView.frame.origin.x,self.myView.frame.origin.y, self.myView.frame.size.width, self.myView.frame.size.height);
    NSLog(@"********************************* end view bounds and frame *********************************");
    NSLog(@"");
    NSLog(@"");

}

我这里打印的信息是这样的,因为点击屏幕的时间可能和你的不一致,导致 start presentationLayer bounds and frame 下面的数据可能和你的也不一致,但是可以看到 start presentationLayer bounds and frame 下面的数据在变化。

2018-01-25 13:15:16.183495+0800  presentationLayer.bounds: x: 0.000000 y: 0.000000 w:0.000000 h:0.000000
2018-01-25 13:15:16.184050+0800  presentationLayer.frame: x: 0.000000 y: 0.000000 w:0.000000 h:0.000000
2018-01-25 13:15:16.184317+0800  start animatioin
2018-01-25 13:15:18.728711+0800  ********************************* start  presentationLayer bounds and frame
2018-01-25 13:15:18.728953+0800  presentationLayer.bounds: x: 0.000000 y: 0.000000 w:100.000000 h:100.000000
2018-01-25 13:15:18.729118+0800  layer.presentationLayer.frame: x: 104.572459 y: 113.717378 w:100.000000 h:100.000000
2018-01-25 13:15:18.729269+0800  
2018-01-25 13:15:18.729419+0800  ********************************* end  presentationLayer bounds and frame 
2018-01-25 13:15:18.729517+0800  ********************************* start view bounds and frame 
2018-01-25 13:15:18.729610+0800  self.myView.bounds: x: 0.000000 y: 0.000000 w:100.000000 h:100.000000
2018-01-25 13:15:18.729698+0800  self.myView.frame: x: 100.000000 y: 100.000000 w:100.000000 h:100.000000
2018-01-25 13:15:18.729771+0800  ********************************* end view bounds and frame
2018-01-25 13:15:18.729968+0800  
2018-01-25 13:15:18.730204+0800  
2018-01-25 13:15:19.919499+0800  ********************************* start  presentationLayer bounds and frame 
2018-01-25 13:15:19.919741+0800  presentationLayer.bounds: x: 0.000000 y: 0.000000 w:100.000000 h:100.000000
2018-01-25 13:15:19.919883+0800  layer.presentationLayer.frame: x: 119.458246 y: 158.374739 w:100.000000 h:100.000000
2018-01-25 13:15:19.920021+0800  
2018-01-25 13:15:19.920146+0800  ********************************* end  presentationLayer bounds and frame 
2018-01-25 13:15:19.920261+0800  ********************************* start view bounds and frame
2018-01-25 13:15:19.920582+0800  self.myView.bounds: x: 0.000000 y: 0.000000 w:100.000000 h:100.000000
2018-01-25 13:15:19.920699+0800  self.myView.frame: x: 100.000000 y: 100.000000 w:100.000000 h:100.000000
2018-01-25 13:15:19.920798+0800  ********************************* end view bounds and frame 
2018-01-25 13:15:19.920938+0800  
2018-01-25 13:15:19.921238+0800  
2018-01-25 13:15:21.464713+0800  ********************************* start  presentationLayer bounds and frame 
2018-01-25 13:15:21.464964+0800  presentationLayer.bounds: x: 0.000000 y: 0.000000 w:100.000000 h:100.000000
2018-01-25 13:15:21.465244+0800  layer.presentationLayer.frame: x: 138.774335 y: 216.323006 w:100.000000 h:100.000000
2018-01-25 13:15:21.465357+0800  
2018-01-25 13:15:21.465456+0800  ********************************* end  presentationLayer bounds and frame 
2018-01-25 13:15:21.465581+0800  ********************************* start view bounds and frame
2018-01-25 13:15:21.466028+0800  self.myView.bounds: x: 0.000000 y: 0.000000 w:100.000000 h:100.000000
2018-01-25 13:15:21.466178+0800  self.myView.frame: x: 100.000000 y: 100.000000 w:100.000000 h:100.000000
2018-01-25 13:15:21.466364+0800  ********************************* end view bounds and frame

参考链接:iOS CoreAnimation专题——原理篇(三) CALayer的模型层与展示层

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,572评论 6 30
  • 有了前两篇的概念基础,本篇从以下两点结合具体代码探索下CoreAnimation的一些原理。 UIView动画实现...
    正谦阅读 2,344评论 2 15
  • 转载:http://www.jianshu.com/p/32fcadd12108 每个UIView有一个伙伴称为l...
    F麦子阅读 6,312评论 0 13
  • 每个UIView有一个伙伴称为layer,一个CALayer。UIView实际上并没有把自己画到屏幕上;它绘制本身...
    shenzhenboy阅读 3,155评论 0 17
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,141评论 5 13