美文网首页
CGAffineTransform 实用技巧

CGAffineTransform 实用技巧

作者: 任兴金 | 来源:发表于2018-04-21 08:38 被阅读14次
    struct CGAffineTransform {
       CGFloat a;
       CGFloat b;
       CGFloat c;
       CGFloat d;
       CGFloat tx;
       CGFloat ty;
    };
    
    a表示x水平方向的缩放,tx表示x水平方向的偏移
    d表示y垂直方向的缩放,ty表示y垂直方向的偏移
    如果b和c不为零的话,那么视图肯定发生了旋转
    

    位移代码

    -(void)positionAnimation{
        _demoView.transform = CGAffineTransformIdentity;
        [UIView animateWithDuration:1.0f animations:^{
            _demoView.transform = CGAffineTransformMakeTranslation(100, 100);
        }];
    }
    

    缩放代码

    -(void)scaleAnimation{
        _demoView.transform = CGAffineTransformIdentity;
        [UIView animateWithDuration:1.0f animations:^{
            _demoView.transform = CGAffineTransformMakeScale(2, 2);
        }];
    }
    

    旋转变换

    -(void)rotateAnimation{
        _demoView.transform = CGAffineTransformIdentity;
        [UIView animateWithDuration:1.0f animations:^{
            _demoView.transform = CGAffineTransformMakeRotation(M_PI);
        }];
    }
    

    组合 变换

    -(void)combinationAnimation{
        //仿射变换的组合使用
        _demoView.transform = CGAffineTransformIdentity;
        [UIView animateWithDuration:1.0f animations:^{
            CGAffineTransform transform1 = CGAffineTransformMakeRotation(M_PI);
            CGAffineTransform transform2 = CGAffineTransformScale(transform1, 0.5, 0.5);
            _demoView.transform = CGAffineTransformTranslate(transform2, 100, 100);
        }];
    }
    

    矩阵反转

    -(void)invertAnimation{
        _demoView.transform = CGAffineTransformIdentity;
        [UIView animateWithDuration:1.0f animations:^{
            //矩阵反转
            _demoView.transform = CGAffineTransformInvert(CGAffineTransformMakeScale(2, 2));
        }];
    }
    

    四、补充

    CGAffineTransformIdentity : 单位矩阵变换,一般用于仿射变换的初始化或者还原。
    CGAffineTransformEqualToTransform(CGAffineTransform t1, 
    CGAffineTransform t2) : 判断两个变换矩阵是否相等
    CGAffineTransformConcat(CGAffineTransform t1, 
    CGAffineTransform t2) : 得到两个矩阵相加后得到的最终矩阵
    CGPoint CGPointApplyAffineTransform(CGPoint point, 
    CGAffineTransform t) : 某点通过矩阵变换之后的点
    CGSize CGSizeApplyAffineTransform(CGSize size, CGAffineTransform t) : 某个size通过矩阵变换之后的size
    CGRect CGRectApplyAffineTransform(CGRect rect, CGAffineTransform t) : 某个Rect通过矩阵变换之后的区域
    

    相关文章

      网友评论

          本文标题:CGAffineTransform 实用技巧

          本文链接:https://www.haomeiwen.com/subject/maywpxtx.html