iOS绕X轴旋转

没有近大远小的效果

- (void)downAnimation {
    _photoImageView.layer.transform = CATransform3DMakeRotation(0, 1, 0, 0);
    
    [UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionRepeat animations:^{
        _photoImageView.layer.transform = CATransform3DMakeRotation(M_PI / 4, 1, 0, 0);
    } completion:^(BOOL finished) {
        _photoImageView.layer.transform = CATransform3DMakeRotation(0, 1, 0, 0);
    }];
}

实现近大远小效果

- (void)downAnimation {
    CATransform3D rotate = CATransform3DMakeRotation(0, 1, 0, 0); //角度,X轴,Y轴,Z轴
    _photoImageView.layer.transform = CATransform3DPerspect(rotate, CGPointMake(0, 0), 120);
    
    [UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionRepeat animations:^{
        CATransform3D rotate = CATransform3DMakeRotation(M_PI / 4, 1, 0, 0);
        _photoImageView.layer.transform = CATransform3DPerspect(rotate, CGPointMake(0, 0), 120);
    } completion:^(BOOL finished) {
        CATransform3D rotate = CATransform3DMakeRotation(0, 1, 0, 0);
        _photoImageView.layer.transform = CATransform3DPerspect(rotate, CGPointMake(0, 0), 120);
    }];
}

- (void)upAnimation {
    CATransform3D rotate = CATransform3DMakeRotation(M_PI / 4, 1, 0, 0);
    _photoImageView.layer.transform = CATransform3DPerspect(rotate, CGPointMake(0, 0), 120);
    
    [UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionRepeat animations:^{
        CATransform3D rotate = CATransform3DMakeRotation(0, 1, 0, 0);
        _photoImageView.layer.transform = CATransform3DPerspect(rotate, CGPointMake(0, 0), 120);
    } completion:^(BOOL finished) {
        CATransform3D rotate = CATransform3DMakeRotation(M_PI / 4, 1, 0, 0);
        _photoImageView.layer.transform = CATransform3DPerspect(rotate, CGPointMake(0, 0), 120);
    }];
}

- (void)endAnimation {
    [_photoImageView.layer removeAllAnimations];
}
//disZ,表示观察者到投射面的距离,值越小,近大远小效果越明显
CATransform3D CATransform3DMakePerspective(CGPoint center, float disZ)
{
    CATransform3D transToCenter = CATransform3DMakeTranslation(-center.x, -center.y, 0);
    CATransform3D transBack = CATransform3DMakeTranslation(center.x, center.y, 0);
    CATransform3D scale = CATransform3DIdentity;
    scale.m34 = -1.0f/disZ;
    return CATransform3DConcat(CATransform3DConcat(transToCenter, scale), transBack);
}

CATransform3D CATransform3DPerspect(CATransform3D t, CGPoint center, float disZ)
{
    return CATransform3DConcat(t, CATransform3DMakePerspective(center, disZ));
}

iOS绕X轴旋转_第1张图片
向上.gif

你可能感兴趣的:(iOS绕X轴旋转)