//移动效果
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 );
了解动画需要先了解仿射变换,先在View中添加一个按钮和一个图片, 通过这演示常见的变换,直接上码:
#import "ViewController.h"
#define M_PI 3.14159265358979323846264338327950288
<br>#define M_PI_2 1.57079632679489661923132169163975144
<br>#define M_PI_4 0.785398163397448309615660845819875721
@interface ViewController ()
{
UIButton *btn;
UIImageView *imgview;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
btn=[UIButton buttonWithType:UIButtonTypeSystem];
btn.frame=CGRectMake(30, 30, 50, 50);
btn.backgroundColor=[UIColor redColor];
[btn setTitle:@"按钮" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
imgview=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"test.jpg"]];
imgview.frame=CGRectMake(100, 100, 120, 120);
[self.view addSubview:imgview];
}
一.平移变换,当我实验平移时,将下面代码放在按钮点击方法中,第一个方法第一次点击有效,第二个是每次点击都会平移,第三个不会平移
//平移变换
imgview.transform=CGAffineTransformMakeTranslation(10, 10); //每次移动参照点都是初始点
imgview.transform=CGAffineTransformTranslate(imgview.transform, 20, 20);//平移参照第一个参数的点
imgview.transform=CGAffineTransformIdentity;//初始点
二、旋转
//旋转
imgview.transform=CGAffineTransformMakeRotation(M_PI/4);//旋转参照初始值 正数顺时针 负数逆时针
imgview.transform=CGAffineTransformRotate(imgview.transform, -M_PI/4);//旋转参照第一个参数的值
三、缩放
// 缩放
imgview.transform=CGAffineTransformScale(imgview.transform,0.8,0.8);//在第一个参数的基础上缩放 第二三参数1时为正常大小
imgview.transform=CGAffineTransformMakeScale(0.5,0.5);//在原视图的基础上缩放
四、翻转
//翻转
// CGAffineTransform DefaultAffineTransform =CGAffineTransformMake(1, 0, 0, 1, 0, 0);//默认
// imgview.transform=CGAffineTransformInvert(imgview.transform); //调用此行代码并不会产生翻转效果,因为恒等矩阵*恒等矩阵还是恒等矩阵,这样就没有变化
CGAffineTransform AffineTransform =CGAffineTransformMake(0, 1, 1, 1, 1, 0);
imgview.transform=CGAffineTransformInvert(AffineTransform);
五、合并
//合并 将两个仿射合并成一个仿射效果
// CGAffineTransform AffineTransform1 =CGAffineTransformMakeScale(0.5,0.5);
CGAffineTransform AffineTransform1=CGAffineTransformScale(imgview.transform,0.8,0.8);
CGAffineTransform AffineTransform2 =CGAffineTransformMakeRotation(M_PI/4);
CGAffineTransform AffineTransform3=CGAffineTransformConcat(AffineTransform1,AffineTransform2);
CGAffineTransform AffineTransform4 =CGAffineTransformMake(0, 1, 1, 1, 1, 0);
imgview.transform=CGAffineTransformConcat(AffineTransform3,AffineTransform4);
六、判断是否相同
CGAffineTransform DefaultAffineTransform =CGAffineTransformMake(1, 0, 0, 1, 0, 0);
CGAffineTransform DefaultAffineTransform1 =CGAffineTransformMake(1, 0, 0, 1, 0, 0);
// CGAffineTransform AffineTransform1 =CGAffineTransformMakeScale(0.5,0.5);
bool flag=CGAffineTransformEqualToTransform(DefaultAffineTransform, DefaultAffineTransform1);
NSLog(@"%d",flag);
时间: 2024-11-29 18:19:30