transform一般用來(lái)讓對(duì)象進(jìn)行旋轉(zhuǎn)退个,縮放和移動(dòng)等操作语盈,常用的transform結(jié)構(gòu)體方法分兩大類(lèi):(1)創(chuàng)建“基于控件基礎(chǔ)位置”的變化
CGAffineTransformMakeScale(1.5, 1,5); 縮放
CGAffineTransformMakeRotation(M_PI);旋轉(zhuǎn)
CGAffineTransformMakeTranslation()缰泡;平移
(2)創(chuàng)建“基于transform參數(shù)”的形變
- (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];
//設(shè)置高亮
//創(chuàng)建向上按鈕
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;
//創(chuàng)建向下按鈕
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];
//左旋轉(zhuǎn)
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;
//右旋轉(zhuǎn)
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) {
//逆時(shí)針
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,以中心點(diǎn)為原點(diǎn)進(jìn)行縮放
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;
}
//設(shè)置首尾動(dòng)畫(huà)
[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];
}