IOS开发 CGAffineTransform相关函数

2024-04-08 04:48

本文主要是介绍IOS开发 CGAffineTransform相关函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

CoreGraphics.h

CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI_2);
[xxx setTransform:rotation];
呵呵就这么简单的两行代码就可以实现了!
顺便记录一些常量,以后用的着!
#define M_E         2.71828182845904523536028747135266250   e
#define M_LOG2E     1.44269504088896340735992468100189214   log 2e
#define M_LOG10E    0.434294481903251827651128918916605082  log 10e
#define M_LN2       0.693147180559945309417232121458176568  log e2
#define M_LN10      2.30258509299404568401799145468436421   log e10
#define M_PI        3.14159265358979323846264338327950288   pi
#define M_PI_2      1.57079632679489661923132169163975144   pi/2
#define M_PI_4      0.785398163397448309615660845819875721  pi/4
#define M_1_PI      0.318309886183790671537767526745028724  1/pi
#define M_2_PI      0.636619772367581343075535053490057448  2/pi
#define M_2_SQRTPI  1.12837916709551257389615890312154517   2/sqrt(pi)
#define M_SQRT2     1.41421356237309504880168872420969808   sqrt(2)
#define M_SQRT1_2   0.707106781186547524400844362104849039  1/sqrt(2)
 
 
from:http://donbe.blog.163.com/blog/static/138048021201061054243442/

CGAffineTransformMakeTranslation(width, 0.0);是改变位置的,


CGAffineTransformRotate(transform, M_PI);是旋转的。


CGAffineTransformMakeRotation(-M_PI);也是旋转的


transform = CGAffineTransformScale(transform, -1.0, 1.0);是缩放的。


view.transform = CGAffineTransformIdentity;线性代数里面讲的矩阵变换,这个是恒等变换


当 你改变过一个view.transform属性或者view.layer.transform的时候需要恢复默认状态的话,记得先把他们重置可以使用

view.transform = CGAffineTransformIdentity,

或者view.layer.transform = CATransform3DIdentity,

假设你一直不断的改变一个view.transform的属性,而每次改变之前没有重置的话,你会发现后来 的改变和你想要的发生变化了,不是你真正想要的结果


Quartz转换实现的原理:Quartz把绘图分成两个部分,
    用户空间,即和设备无关,
    设备空间,
用户空间和设备空间中间存在一个转换矩阵 : CTM
本章实质是讲解CTM
 
Quartz提供的3大功能
移动,旋转,缩放
 
演示如下,首先加载一张图片
void CGContextDrawImage (
   CGContextRef c,
   CGRect rect,
   CGImageRef image
);
 
 
 
 
 
移动函数
CGContextTranslateCTM (myContext, 100, 50);
 
 
 
旋转函数
include <math.h>
static inline double radians (double degrees) {return degrees * M_PI/180;}
CGContextRotateCTM (myContext, radians(–45.));
 
 
 
缩放
CGContextScaleCTM (myContext, .5, .75);
 
 
 
翻转, 两种转换合成后的效果,先把图片移动到右上角,然后旋转180度
CGContextTranslateCTM (myContext, w,h);
CGContextRotateCTM (myContext, radians(-180.));
 
 
 
组合几个动作
CGContextTranslateCTM (myContext, w/4, 0);
CGContextScaleCTM (myContext, .25,  .5);
CGContextRotateCTM (myContext, radians ( 22.));
 
 
 
 
 
CGContextRotateCTM (myContext, radians ( 22.));
CGContextScaleCTM (myContext, .25,  .5);
CGContextTranslateCTM (myContext, w/4, 0);
 
 
 
 
上面是通过直接修改当前的ctm实现3大效果,下面是通过创建Affine Transforms,然后连接ctm实现同样的3种效果
这样做的好处是可以重用这个Affine Transforms
应用Affine Transforms 到ctm的函数
void CGContextConcatCTM (
   CGContextRef c,
   CGAffineTransform transform
);
 
 
Creating Affine Transforms
移动效果
CGAffineTransform CGAffineTransformMakeTranslation (
   CGFloat tx,
   CGFloat ty
);
 
CGAffineTransform CGAffineTransformTranslate (
   CGAffineTransform t,
   CGFloat tx,
   CGFloat ty
);
 
旋转效果
CGAffineTransform CGAffineTransformMakeRotation (
   CGFloat angle
);
 
CGAffineTransform CGAffineTransformRotate (
   CGAffineTransform t,
   CGFloat angle
);
 
缩放效果
CGAffineTransform CGAffineTransformMakeScale (
   CGFloat sx,
   CGFloat sy
);
 
CGAffineTransform CGAffineTransformScale (
   CGAffineTransform t,
   CGFloat sx,
   CGFloat sy
);
 
反转效果
CGAffineTransform CGAffineTransformInvert (
   CGAffineTransform t
);
 
只对局部产生效果
CGRect CGRectApplyAffineTransform (
   CGRect rect,
   CGAffineTransform t
);
 
判断两个AffineTrans是否相等
bool CGAffineTransformEqualToTransform (
   CGAffineTransform t1,
   CGAffineTransform t2
);
 
 
 
获得Affine Transform
CGAffineTransform CGContextGetUserSpaceToDeviceSpaceTransform (
   CGContextRef c
);
 
下面的函数只起到查看的效果,比如看一下这个用户空间的点,转换到设备空间去坐标是多少
CGPoint CGContextConvertPointToDeviceSpace (
   CGContextRef c,
   CGPoint point
);
 
CGPoint CGContextConvertPointToUserSpace (
   CGContextRef c,
   CGPoint point
);
 
CGSize CGContextConvertSizeToDeviceSpace (
   CGContextRef c,
   CGSize size
);
 
CGSize CGContextConvertSizeToUserSpace (
   CGContextRef c,
   CGSize size
);
 
CGRect CGContextConvertRectToDeviceSpace (
   CGContextRef c,
   CGRect rect
);
 
CGRect CGContextConvertRectToUserSpace (
   CGContextRef c,
   CGRect rect
);
 
 
CTM真正的数学行为
这个转换矩阵其实是一个 3x3的 举证
如下图
 
 
下面举例说明几个转换运算的数学实现
x y 是原先点的坐标
下面是从用户坐标转换到设备坐标的计算公式
 
 
 
 
下面是一个identity matrix,就是输入什么坐标,出来什么坐标,没有转换
 
最终的计算结果是 x=x,y=y,  
 
 
 可以用函数判断这个矩阵是不是一个 identity matrix
bool CGAffineTransformIsIdentity (
   CGAffineTransform t
);
 
 
 
 
参考:http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_affine/dq_affine.html








- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation   duration:(NSTimeInterval)duration
{
        
    
        if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
        {
                b=YES;
                
                self.view=mainvv;
                self.view.transform = CGAffineTransformIdentity;
                self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
                self.view.bounds = CGRectMake(0.0, 0.0, 768.0, 1004.0);
                
        }
        else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
        {
                b=NO;
                
                self.view = self.vv;
                self.view.transform = CGAffineTransformIdentity;
                self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
                self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 748.0);
                
                
                
        }
        else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        {
                
                b=YES;
                self.view=mainvv;
                self.view.transform = CGAffineTransformIdentity;
                self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
                self.view.bounds = CGRectMake(0.0, 0.0, 768.0, 1004.0);
                
        }
        else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
        {
                
                b=NO;
                self.view = self.vv;
                self.view.transform = CGAffineTransformIdentity;
                self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
                self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 748.0);
                
        }
        
        
}


3

Quartz转换实现的原理:Quartz把绘图分成两个部分,
    用户空间,即和设备无关,
    设备空间,
用户空间和设备空间中间存在一个转换矩阵 : CTM
本章实质是讲解CTM

Quartz提供的3大功能
移动,旋转,缩放

演示如下,首先加载一张图片
void CGContextDrawImage (
   CGContextRef c,
   CGRect rect,
   CGImageRef image
);


 

 
移动函数
CGContextTranslateCTM (myContext, 100, 50);




旋转函数
include <math.h>
static inline double radians (double degrees) {return degrees * M_PI/180;}
CGContextRotateCTM (myContext, radians(–45.));



缩放
CGContextScaleCTM (myContext, .5, .75);



翻转, 两种转换合成后的效果,先把图片移动到右上角,然后旋转180度
CGContextTranslateCTM (myContext, w,h);
CGContextRotateCTM (myContext, radians(-180.));



组合几个动作
CGContextTranslateCTM (myContext, w/4, 0);
CGContextScaleCTM (myContext, .25,  .5);
CGContextRotateCTM (myContext, radians ( 22.));


 


CGContextRotateCTM (myContext, radians ( 22.));
CGContextScaleCTM (myContext, .25,  .5);
CGContextTranslateCTM (myContext, w/4, 0);




上面是通过直接修改当前的ctm实现3大效果,下面是通过创建Affine Transforms,然后连接ctm实现同样的3种效果
这样做的好处是可以重用这个Affine Transforms
应用Affine Transforms 到ctm的函数
void CGContextConcatCTM (
   CGContextRef c,
   CGAffineTransform transform
);


Creating Affine Transforms
移动效果
CGAffineTransform CGAffineTransformMakeTranslation (
   CGFloat tx,
   CGFloat ty
);

CGAffineTransform CGAffineTransformTranslate (
   CGAffineTransform t,
   CGFloat tx,
   CGFloat ty
);

旋转效果
CGAffineTransform CGAffineTransformMakeRotation (
   CGFloat angle
);

CGAffineTransform CGAffineTransformRotate (
   CGAffineTransform t,
   CGFloat angle
);

缩放效果
CGAffineTransform CGAffineTransformMakeScale (
   CGFloat sx,
   CGFloat sy
);

CGAffineTransform CGAffineTransformScale (
   CGAffineTransform t,
   CGFloat sx,
   CGFloat sy
);

反转效果
CGAffineTransform CGAffineTransformInvert (
   CGAffineTransform t
);

只对局部产生效果
CGRect CGRectApplyAffineTransform (
   CGRect rect,
   CGAffineTransform t
);

判断两个AffineTrans是否相等
bool CGAffineTransformEqualToTransform (
   CGAffineTransform t1,
   CGAffineTransform t2
);



获得Affine Transform
CGAffineTransform CGContextGetUserSpaceToDeviceSpaceTransform (
   CGContextRef c
);

下面的函数只起到查看的效果,比如看一下这个用户空间的点,转换到设备空间去坐标是多少
CGPoint CGContextConvertPointToDeviceSpace (
   CGContextRef c,
   CGPoint point
);

CGPoint CGContextConvertPointToUserSpace (
   CGContextRef c,
   CGPoint point
);

CGSize CGContextConvertSizeToDeviceSpace (
   CGContextRef c,
   CGSize size
);

CGSize CGContextConvertSizeToUserSpace (
   CGContextRef c,
   CGSize size
);

CGRect CGContextConvertRectToDeviceSpace (
   CGContextRef c,
   CGRect rect
);

CGRect CGContextConvertRectToUserSpace (
   CGContextRef c,
   CGRect rect
);


CTM真正的数学行为
这个转换矩阵其实是一个 3x3的 举证
如下图


下面举例说明几个转换运算的数学实现
x y 是原先点的坐标
下面是从用户坐标转换到设备坐标的计算公式




下面是一个identity matrix,就是输入什么坐标,出来什么坐标,没有转换

最终的计算结果是 x=x,y=y,  


 可以用函数判断这个矩阵是不是一个 identity matrix
bool CGAffineTransformIsIdentity (
   CGAffineTransform t
);


移动矩阵


 

缩放矩阵

 

旋转矩阵

 

旋转加移动矩阵




//********************************//

layer 动画-(transform.rotation篇)

x轴旋转:
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
theAnimation.duration=8;
theAnimation.removedOnCompletion = YES;
theAnimation.fromValue = [NSNumber numberWithFloat:0];
theAnimation.toValue = [NSNumber numberWithFloat:3.1415926];[yourView.layer addAnimation:theAnimation forKey:@"animateTransform"];y轴旋转:
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
theAnimation.duration=8;
theAnimation.removedOnCompletion = YES;
theAnimation.fromValue = [NSNumber numberWithFloat:0];
theAnimation.toValue = [NSNumber numberWithFloat:3.1415926];[yourView.layer addAnimation:theAnimation forKey:@"animateTransform"];z轴旋转:
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
theAnimation.duration=8;
theAnimation.removedOnCompletion = YES;
theAnimation.fromValue = [NSNumber numberWithFloat:0];
theAnimation.toValue = [NSNumber numberWithFloat:3.1415926];[yourView.layer addAnimation:theAnimation forKey:@"animateTransform"];以上缩放是以view的中心点为中心缩放的,如果需要自定义缩放点,可以设置卯点:
//中心点
[yourView.layer setAnchorPoint:CGPointMake(0.5, 0.5)];//左上角
[yourView.layer setAnchorPoint:CGPointMake(0, 0)];//右下角
[yourView.layer setAnchorPoint:CGPointMake(1, 1)];可设参数:theAnimation.repeatCount = 0;
theAnimation.autoreverses = NO;旋转的另一种实现:(以下代码绕z轴旋转180度)[self.topViewController.view.layer setAnchorPoint:CGPointMake(0.5, 0.5)];[self.topViewController.view.layer setTransform:CATransform3DMakeRotation(0, 0, 0, 1)];[UIView animateWithDuration:8 delay:0.0f options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveEaseIn animations:^{[self.topViewController.view.layer setTransform:CATransform3DMakeRotation(3.1415926, 0, 0, 1)];} completion:^(BOOL finished) {}];


//获取当前transform
CGAffineTransform transform = self.lblSeg.transform;//缩放
self.lblSeg.transform = CGAffineTransformMakeScale(.5, .5);//在一个transform的基础上再缩放
self.lblTest.transform = CGAffineTransformScale(transform, 0.5, 0.5);//旋转(弧度制)
self.lblSeg.transform = CGAffineTransformMakeRotation(M_2_PI);//在一个transform的基础上再旋转
self.lblTest.transform = CGAffineTransformRotate(transform,M_2_PI);//平移
self.lblSeg.transform = CGAffineTransformMakeTranslation(100, 100);//在一个transform的基础上再平移
self.lblTest.transform = CGAffineTransformTranslate(transform, 100, 100);



这篇关于IOS开发 CGAffineTransform相关函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/884607

相关文章

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

Hadoop企业开发案例调优场景

需求 (1)需求:从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。 (2)需求分析: 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster 平均每个节点运行10个 / 3台 ≈ 3个任务(4    3    3) HDFS参数调优 (1)修改:hadoop-env.sh export HDFS_NAMENOD

hdu1171(母函数或多重背包)

题意:把物品分成两份,使得价值最接近 可以用背包,或者是母函数来解,母函数(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v) 其中指数为价值,每一项的数目为(该物品数+1)个 代码如下: #include<iostream>#include<algorithm>

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设

OpenHarmony鸿蒙开发( Beta5.0)无感配网详解

1、简介 无感配网是指在设备联网过程中无需输入热点相关账号信息,即可快速实现设备配网,是一种兼顾高效性、可靠性和安全性的配网方式。 2、配网原理 2.1 通信原理 手机和智能设备之间的信息传递,利用特有的NAN协议实现。利用手机和智能设备之间的WiFi 感知订阅、发布能力,实现了数字管家应用和设备之间的发现。在完成设备间的认证和响应后,即可发送相关配网数据。同时还支持与常规Sof

sqlite3 相关知识

WAL 模式 VS 回滚模式 特性WAL 模式回滚模式(Rollback Journal)定义使用写前日志来记录变更。使用回滚日志来记录事务的所有修改。特点更高的并发性和性能;支持多读者和单写者。支持安全的事务回滚,但并发性较低。性能写入性能更好,尤其是读多写少的场景。写操作会造成较大的性能开销,尤其是在事务开始时。写入流程数据首先写入 WAL 文件,然后才从 WAL 刷新到主数据库。数据在开始

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

Linux_kernel驱动开发11

一、改回nfs方式挂载根文件系统         在产品将要上线之前,需要制作不同类型格式的根文件系统         在产品研发阶段,我们还是需要使用nfs的方式挂载根文件系统         优点:可以直接在上位机中修改文件系统内容,延长EMMC的寿命         【1】重启上位机nfs服务         sudo service nfs-kernel-server resta

【区块链 + 人才服务】区块链集成开发平台 | FISCO BCOS应用案例

随着区块链技术的快速发展,越来越多的企业开始将其应用于实际业务中。然而,区块链技术的专业性使得其集成开发成为一项挑战。针对此,广东中创智慧科技有限公司基于国产开源联盟链 FISCO BCOS 推出了区块链集成开发平台。该平台基于区块链技术,提供一套全面的区块链开发工具和开发环境,支持开发者快速开发和部署区块链应用。此外,该平台还可以提供一套全面的区块链开发教程和文档,帮助开发者快速上手区块链开发。