iOS CGAffine Transform属性

transform一般用来让对象进行旋转,缩放和移动等操作,常用的transform结构体方法分两大类:(1)创建“基于控件基础位置”的变化

CGAffineTransformMakeScale(1.5, 1,5); 缩放

CGAffineTransformMakeRotation(M_PI);旋转

CGAffineTransformMakeTranslation();平移

(2)创建“基于transform参数”的形变

- (void)viewDidLoad {

[super viewDidLoad];

//    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];

self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];

self.imageV.image = [UIImage imageNamed:@"gay.jpg"];

[self.view addSubview:self.imageV];

//设置高亮

//创建向上按钮

UIButton *topBtn = [UIButton buttonWithType:UIButtonTypeCustom];

topBtn.frame= CGRectMake(100, 250, 40, 40);

[topBtn setBackgroundImage:[UIImage imageNamed:@"shang.png"] forState:UIControlStateNormal];

[self.view addSubview:topBtn];

[topBtn addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];

topBtn.tag = 1;

//创建向下按钮

UIButton *downBtn = [UIButton buttonWithType:UIButtonTypeCustom];

downBtn.frame = CGRectMake(100, 350, 40, 40);

[downBtn setBackgroundImage:[UIImage imageNamed:@"xia.png"] forState:UIControlStateNormal];

[self.view addSubview:downBtn];

[downBtn setTag:2];

[downBtn addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];

//zuo

UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];

leftBtn.frame = CGRectMake(50  , 300, 40, 40);

[leftBtn setBackgroundImage:[UIImage imageNamed:@"zuo.png"] forState:UIControlStateNormal];

[self.view addSubview:leftBtn];

[leftBtn setTag:4];

[leftBtn addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];

//you

UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];

rightBtn.frame = CGRectMake(150, 300, 40, 40);

[rightBtn setBackgroundImage:[UIImage imageNamed:@"you.png"] forState:UIControlStateNormal];

[self.view addSubview:rightBtn];

[rightBtn setTag:3];

[rightBtn addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];

//放大按钮

UIButton *plusBtn = [UIButton buttonWithType:UIButtonTypeCustom];

plusBtn.frame = CGRectMake(75, 400, 40, 40);

[plusBtn setBackgroundImage:[UIImage imageNamed:@"plus"] forState:UIControlStateNormal];

[plusBtn setTag:1];///???????

[self.view addSubview:plusBtn];

[plusBtn addTarget:self action:@selector(Zoom:) forControlEvents:UIControlEventTouchUpInside];

//缩小按钮

UIButton *minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];

minusBtn.frame = CGRectMake(120, 400, 40, 40);

[minusBtn setBackgroundImage:[UIImage imageNamed:@"minus"] forState:UIControlStateNormal];

[self.view addSubview:minusBtn];

[minusBtn setTag:0];

[minusBtn addTarget:self action:@selector(Zoom:) forControlEvents:UIControlEventTouchUpInside];

//左旋转

UIButton *leferRotateBtn = [UIButton buttonWithType:UIButtonTypeCustom];

leferRotateBtn.frame = CGRectMake(175, 400, 40, 40);

[leferRotateBtn setBackgroundImage:[UIImage imageNamed:@"zuozhuan"] forState:UIControlStateNormal];

[self.view addSubview:leferRotateBtn];

[leferRotateBtn addTarget:self action:@selector(rotate:) forControlEvents:UIControlEventTouchUpInside];

leferRotateBtn.tag = 100;

//右旋转

UIButton *rightRotateBtn = [UIButton buttonWithType:UIButtonTypeCustom];

rightRotateBtn.frame = CGRectMake(225, 400, 40, 40);

[rightRotateBtn setBackgroundImage:[UIImage imageNamed:@"youzhuan"] forState:UIControlStateNormal];

[self.view addSubview:rightRotateBtn];

rightRotateBtn.tag = 101;

[rightRotateBtn addTarget:self action:@selector(rotate:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)rotate:(UIButton *)sender

{

if (sender.tag == 100) {

//逆时针

self.imageV.transform = CGAffineTransformRotate(self.imageV.transform, -M_1_PI);

}

else

{

self.imageV.transform = CGAffineTransformRotate(self.imageV.transform, M_1_PI);

}

}

- (void)Zoom:(UIButton *)sender

{

//使用bounds,以中心点为原点进行缩放

CGRect bounds = self.imageV.bounds;

if (sender.tag) {

bounds.size.height += 30;

bounds.size.width  += 30;

}else

{

bounds.size.height  -= 50;

bounds.size.width  -= 50;

}

//设置首尾动画

[UIView beginAnimations:nil context:nil];

self.imageV.bounds = bounds;

[UIView setAnimationDuration:2.0];

[UIView commitAnimations];

}

-(void)Click:(UIButton *)sender

{

NSLog(@"CLICK a");

CGPoint center = self.imageV.center;

switch (sender.tag) {

case 1:

center.y -= 30;

NSLog(@"%ld",(long)sender.tag);

break;

case 2:

center.y += 30;

break;

case 3:

center.x += 50;

break;

case 4:

center.x -= 50;

break;

default:

break;

}

[UIView beginAnimations:nil context:nil];

self.imageV.center = center;

[UIView setAnimationDuration:2.0];

[UIView commitAnimations];

}


推荐阅读更多精彩内容