ios - 如何避免在 iOS 中对 CATransform3D 旋转进行倾斜/翻转?

标签 ios objective-c catransform3d catransform3drotate

我在 View 中使用了下面的 3dRotation 函数。但我不想在 View 上倾斜/翻转,我只想在 View 上左/右/上/下移动。

我如何避免在 View 中倾斜和翻转旋转?

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

最佳答案

-(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];

}

我希望这能奏效

关于ios - 如何避免在 iOS 中对 CATransform3D 旋转进行倾斜/翻转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41054948/

相关文章:

cocoa - 对所有 CALayer 元素应用相同的透视和旋转(与 Coverflow 不同)

iphone - 突出显示此按钮类型不支持的色调颜色

iOS NSDictionary 保存到 NSUserDefaults 不保存值

objective-c - 报亭套件推送通知而不使用应用程序如何获取用户信息

UIImageView 的 Swift 3D 旋转(CGAffineTransform 或 CATransform3D)

ios - 使用CATransform3D实现梯形变换

ios - 如何使用 Xamarin 和 iOS 控制背景音频

ios - App Store 图片未显示在 Xcode 中

ios - 将自定义Objective-C框架添加到项目时的链接器错误

ios - 线程 1 : breakpoint 1. 2;使用 Objective-C 将 Realm 数据加载到自定义 TableView 中