iOS绕X轴旋转

2018-05-07  本文已影响0人  Chase_Eleven

没有近大远小的效果

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

向上.gif
上一篇 下一篇

猜你喜欢

热点阅读