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 *)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的模型层与展示层

你可能感兴趣的:(UIView 执行动画过程中获取坐标位置)