ios - 当UIView正在做动画时,如何获取UIView的frame?
PHPz
PHPz 2017-04-18 09:17:43
0
1
295

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

PHPz
PHPz

学习是最好的投资!

membalas semua(1)
迷茫

Gunakan atribut presentationLayer untuk mendapatkan bingkai terkini.
Sampel:

- (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);
            });
        }
    });
}

Pautan rujukan

Tambahan:
Prinsip sampel di atas ialah selepas animasi bermula, bingkai terkini diperoleh secara berterusan melalui presentationLayer dalam sub-utas keutamaan rendah sehingga animasi tamat. Sudah tentu, anda boleh menggunakan NSTimer untuk mencapai ini.

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan