CGAffineTransformMakeTranslation无法正确翻译

时间:2015-10-14 23:25:27

标签: ios objective-c

我在模拟器模式和真实设备之间看到了CGAffineTransformMakeTranslation的两种不同行为。

(在iOS 8.1上) 对于iPhone4,要让CGAffineTransformMakeTranslation在真实设备上运行,我必须翻译两倍的金额。 在iPhone4s模拟器上,我只需按照不缩放的数量进行翻译。

也许我错过了一些明显的东西,但由于两者的逻辑像素数相同,我不应该调整CGAffineTransformMakeTranslation来使翻译工作。

在这里,我有一个视图,我正在通过滑动拉入视线:

CGRect menuOrigin = self.rootController.containerView.bounds;
switch (direction) {
    case UISwipeGestureRecognizerDirectionLeft:
    {
        NSLog(@"Initial Position: %@", NSStringFromCGPoint(self.rootController.containerView.frame.origin));
        NSLog(@"Initial Scale: %f", [self contentScaleFactor]);
        NSLog(@"Initial Center: %@", NSStringFromCGPoint([self center]));
        CGAffineTransform transform = CGAffineTransformMakeTranslation(-1.0 * widthToSlide, 0);
        self.rootController.containerView.transform = transform;
        [UIView animateWithDuration:0.4 animations:^{
            self.rootController.containerView.transform = transform;
            [self.rootController.containerView layoutIfNeeded];
        } completion:^(BOOL finished) {
            NSLog(@"Final Position: %@", NSStringFromCGPoint(self.rootController.containerView.frame.origin));
            NSLog(@"Final Scale: %f", [self contentScaleFactor]);
            NSLog(@"Final Center: %@", NSStringFromCGPoint([self center]));
        }];
    }
        break;
    case UISwipeGestureRecognizerDirectionRight:
    {
        CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 0.0);
        [UIView animateWithDuration:0.4 animations:^{
            self.rootController.containerView.transform = transform;
            [self.rootController.containerView layoutIfNeeded];
        } completion:^(BOOL finished) {
        }];
    }
    default:
        break;
}

[self.rootController.containerView setNeedsDisplay];
[self setNeedsDisplay];
对于iPhone4真实设备,NSLog就像这样:

2015-10-15 01:12:37.547 Local[5307:60b] swipeLocalPull adjusting width 183
2015-10-15 01:12:37.556 Local[5307:60b] Screen size: {320, 480}
2015-10-15 01:12:37.559 Local[5307:60b] Initial Position: {0, 66}
2015-10-15 01:12:37.974 Local[5307:60b] Final Position: {-91.5, 66}

和iPhone4s的iOS 8.1模拟器:

2015-10-15 01:19:19.479 Local[56380:3061577] swipeLocalPull adjusting width 183
2015-10-15 01:19:19.480 Local[56380:3061577] Screen size: {320, 480}
2015-10-15 01:19:19.480 Local[56380:3061577] Initial Position: {0, 66}
2015-10-15 01:19:19.886 Local[56380:3061577] Final Position: {-183, 66}

模拟器已正确翻译。请注意,真正的设备只翻译到-91.5的一半。为什么这样,我如何检测或补偿这一点。对不起,如果之前已经问过这个问题,但我找不到这种行为的根本原因。我现在只关心x翻译。

任何人都有这种行为的线索。?

更新 根据要求,这里有一些额外的输出:

--- iPhone4值---

2015-10-15 17:20:59.591 Local[5617:60b] swipeLocalPull adjusting width 183
2015-10-15 17:20:59.596 Local[5617:60b] Screen size: {320, 480}
2015-10-15 17:20:59.599 Local[5617:60b] Initial Position: {0, 66}
2015-10-15 17:20:59.601 Local[5617:60b] Initial Scale: 2.000000
2015-10-15 17:20:59.603 Local[5617:60b] Initial Center: {201, 99}
2015-10-15 17:21:00.015 Local[5617:60b] Final Position: {-91.5, 66}
2015-10-15 17:21:00.018 Local[5617:60b] Final Scale: 2.000000
2015-10-15 17:21:00.037 Local[5617:60b] Final Center: {201, 99}

--- iPhone4s模拟器---

2015-10-15 17:39:59.850 Local[57069:3104390] swipeLocalPull adjusting width 183
2015-10-15 17:39:59.851 Local[57069:3104390] Screen size: {320, 480}
2015-10-15 17:39:59.851 Local[57069:3104390] Initial Position: {0, 66}
2015-10-15 17:39:59.851 Local[57069:3104390] Initial Scale: 2.000000
2015-10-15 17:39:59.851 Local[57069:3104390] Initial Center: {201, 99}
2015-10-15 17:39:59.852 Local[57069:3104390] Final Position: {-183, 66}
2015-10-15 17:39:59.852 Local[57069:3104390] Final Scale: 2.000000
2015-10-15 17:39:59.852 Local[57069:3104390] Final Center: {201, 99}

它们似乎都是@ 2x规模,因此我希望相同的代码能够同时适用于这两种规范。

工作代码

我得到了以下工作(如果有人解释了原因,那么我只会将其标记为答案):

switch (direction) {
        case UISwipeGestureRecognizerDirectionLeft:
        {
            NSLog(@"Initial Position: %@", NSStringFromCGPoint(self.rootController.containerView.frame.origin));
            NSLog(@"Initial Scale: %f", [self contentScaleFactor]);
            NSLog(@"Initial Center: %@", NSStringFromCGPoint([self center]));

            CGPoint currentPos = self.rootController.containerView.center;
            currentPos.x -= widthToSlide;
            [UIView animateWithDuration: .4
                                  delay: 0
                                options: (UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction)
                             animations:^{
                                 self.rootController.containerView.center = currentPos;
                             }
                             completion:^(BOOL finished) { }
             ];
        }
            break;
        case UISwipeGestureRecognizerDirectionRight:
        {
            CGPoint currentPos = self.rootController.containerView.center;
            currentPos.x += widthToSlide;

            [UIView animateWithDuration: .4
                                  delay: 0
                                options: (UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction)
                             animations:^{
                                 self.rootController.containerView.center = currentPos;
                             }
                             completion:^(BOOL finished) { }
             ];
        }
        default:
            break;
    }

    [self.rootController.containerView setNeedsDisplay];
    [self setNeedsDisplay];

0 个答案:

没有答案