【问题标题】:How to avoid tilt/flip on CATransform3D rotation in iOS?如何避免 iOS 中 CATransform3D 旋转的倾斜/翻转?
【发布时间】:2023-03-26 02:28:01
【问题描述】:

我在视图中使用以下功能进行 3dRotation。但我不想在视图中倾斜/翻转,我只想在视图中向左/向右/向上/向下移动。

我如何避免在我的视图中倾斜和翻转旋转?

- (void)Move3dPan:(UIPanGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateChanged)
    {
        CGPoint displacement = [gesture translationInView:self.view];
        CATransform3D currentTransform = self.popUpView.layer.sublayerTransform;

        if (displacement.x==0 && displacement.y==0)
        {
            // no rotation, nothing to do
            return;
        }

        CGFloat totalRotation = sqrt(displacement.x * displacement.x + displacement.y * displacement.y) * M_PI / 360.0;
        CGFloat xRotationFactor = displacement.x/totalRotation;
        CGFloat yRotationFactor = displacement.y/totalRotation;

        CATransform3D rotationalTransform = CATransform3DRotate(currentTransform, totalRotation,
                                                            (xRotationFactor * currentTransform.m12 - yRotationFactor * currentTransform.m11),
                                                            (xRotationFactor * currentTransform.m22 - yRotationFactor * currentTransform.m21),
                                                            (xRotationFactor * currentTransform.m32 - yRotationFactor * currentTransform.m31));

        [CATransaction setAnimationDuration:0];

        self.popUpView.layer.sublayerTransform = rotationalTransform;

        [gesture setTranslation:CGPointZero inView:self.view];
    }
}

【问题讨论】:

标签: ios objective-c catransform3d catransform3drotate


【解决方案1】:
-(void) startAnimatingCupView

{

    [self stopAnimatingCupView];

    timer = [NSTimer scheduledTimerWithTimeInterval:0.8 repeats:true block:^(NSTimer * _Nonnull timer) {

        [self animateCupView];

    }];

}


-(void) animateCupView{

    [UIView animateWithDuration:0.4 animations:^{

        self.imgCup.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(180));

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.4 animations:^{

            self.imgCup.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(360));

        }];

    }];

}


-(void) stopAnimatingCupView{

    if (timer != nil) {

        [timer invalidate];

        timer = nil;

    }

    [self.imgCup.layer setTransform:CATransform3DIdentity];

}

我希望这会奏效

【讨论】:

  • 感谢您的回答。我想使用 UIPanGestureRecognizer 进行 3drotation on touch move。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-17
  • 1970-01-01
相关资源
最近更新 更多