一 transform属性
在OC中,通过transform属性可以修改对象的平移,比例和旋转角度
常用的创建transform结构体的方法分两大类
(1) 创建"基于控件初始位置"的形变
CGAffineTransformMakeTranslation (平移)
CGAffineTransformMakeScale (缩放)
CGAffineTransformMakeRotation (旋转)
(2) 创建"基于transform参数"的形变
CGAffineTransformTranslate
CGAffineTransformScale
CGAffineTransformRotate
在OC中,所有跟角度相关的数值,都是弧度值,180º = M_PI
正数表示顺时针旋转
负数代表逆时针旋转
二 代码示例
//
// RootViewController.m
// 练习使用按钮的frame和center属性
//
// Created by lovestarfish on 15/11/1.
// Copyright © 2015年 S&G. All rights reserved.
// #import "RootViewController.h" @interface RootViewController () @property (nonatomic,retain) UIButton *headImageView; @end @implementation RootViewController - (void)dealloc {
self.headImageView = nil;
[super dealloc];
} //枚举类型,从1开始
//枚举类型有一个很大的作用,就是用来代替程序中的魔法数字
typedef enum {
ktopbtntag = ,
kdownbtntag,
kleftbtntag,
krightbtntag
}btntag; /**
* viewDidLoad是视图加载完成后调用的方法,通常在此方法中执行视图控制器的初始工作
*/
- (void)viewDidLoad { //在viewDidLoad方法中,不要忘记调用父类的方法实现
[super viewDidLoad]; self.title = @"按钮的frame和center属性"; //一 手写控件代码
//1.使用类创建一个按钮对象,设置按钮对象为自定义型
UIButton *headBtn = [UIButton buttonWithType:UIButtonTypeCustom]; //2.设置对象的各项属性
//(1)位置等通用属性设置
headBtn.frame = CGRectMake(137.5, , , ); //(2)设置普通状态下按钮的属性
[headBtn setBackgroundImage:[UIImage imageNamed:@"xib1"] forState:UIControlStateNormal];
[headBtn setTitle:@"点我" forState:UIControlStateNormal];
[headBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; //(3)设置高亮状态下按钮的属性
[headBtn setBackgroundImage:[UIImage imageNamed:@"xib2"] forState:UIControlStateHighlighted];
[headBtn setTitle:@"还行吧" forState:UIControlStateHighlighted];
[headBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; //3.把对象添加到视图中展现出来
[self.view addSubview:headBtn];
self.headImageView = headBtn; //二 写四个控制图片上下左右移动方向的控制按钮
//向上
UIButton *topBut = [UIButton buttonWithType:UIButtonTypeSystem];
topBut.frame = CGRectMake(167.5, , , );
[topBut setTitle:@"向上" forState:UIControlStateNormal];
topBut.tag = ;
[topBut addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:topBut]; //向下
UIButton *downBtn = [UIButton buttonWithType:UIButtonTypeSystem];
downBtn.frame = CGRectMake(167.5, , , );
[downBtn setTitle:@"向下" forState:UIControlStateNormal];
downBtn.tag = ;
[downBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:downBtn]; //向左
UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
leftBtn.frame = CGRectMake(, , , );
[leftBtn setTitle:@"向左" forState:UIControlStateNormal];
leftBtn.tag = ;
[leftBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:leftBtn]; //向右
UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
rightBtn.frame = CGRectMake(, , , );
[rightBtn setTitle:@"向右" forState:UIControlStateNormal];
rightBtn.tag = ;
[rightBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:rightBtn]; //三 写两个缩放按钮
//放大
UIButton *plusBtn = [UIButton buttonWithType:UIButtonTypeSystem];
plusBtn.frame = CGRectMake(, , , );
[plusBtn setTitle:@"放大" forState:UIControlStateNormal];
plusBtn.tag = ;
[plusBtn addTarget:self action:@selector(zoom:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:plusBtn];
//缩小
UIButton *minusBtn = [UIButton buttonWithType:UIButtonTypeSystem];
minusBtn.frame = CGRectMake(, , , );
[minusBtn setTitle:@"缩小" forState:UIControlStateNormal];
minusBtn.tag = ;
[minusBtn addTarget:self action:@selector(zoom:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:minusBtn]; //向左旋转按钮
UIButton *leftRotateBtn = [UIButton buttonWithType:UIButtonTypeSystem];
leftRotateBtn.frame = CGRectMake(137.5, , , );
[leftRotateBtn setTitle:@"向左旋转" forState:UIControlStateNormal];
leftRotateBtn.tag = ;
[leftRotateBtn addTarget:self action:@selector(rotate:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:leftRotateBtn];
//向右旋转按钮
UIButton *rightRotateBtn = [UIButton buttonWithType:UIButtonTypeSystem];
rightRotateBtn.frame = CGRectMake(137.5, , , );
[rightRotateBtn setTitle:@"向右旋转" forState:UIControlStateNormal];
rightRotateBtn.tag = ;
[rightRotateBtn addTarget:self action:@selector(rotate:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:rightRotateBtn]; } /**
* 控制方向的多个按钮调用同一个方法
*/
- (void)click:(UIButton *)button {
CGPoint center = self.headImageView.center;
switch (button.tag) {
case ktopbtntag:
center.y -= ;
break;
case kdownbtntag:
center.y += ;
break;
case kleftbtntag:
center.x -= ;
break;
case krightbtntag:
center.x += ;
break;
default:
break;
}
//设置首尾动画
[UIView beginAnimations:nil context:nil];
self.headImageView.center = center;
//设置时间
[UIView setAnimationDuration:2.0];
[UIView commitAnimations];
NSLog(@"移动!");
} /**
* 缩放
*/
- (void)zoom:(UIButton *)button {
//使用frame,以自己的左上角(自己的原点)为原点
/*
CGRect frame = self.headImageView.frame;
if (button.tag) {
frame.size.height += 30;
frame.size.width += 30;
} else {
frame.size.width -= 50;
frame.size.height -= 50;
}
self.headImageView.frame = frame;
*/ //使用bounds,以中心点为原点进行缩放
CGRect bounds = self.headImageView.bounds;
if (button.tag) {
bounds.size.height += ;
bounds.size.width += ;
} else {
bounds.size.height -= ;
bounds.size.width -= ;
} //设置首尾动画
[UIView beginAnimations:nil context:nil];
self.headImageView.bounds = bounds;
[UIView setAnimationDuration:2.0];
[UIView commitAnimations];
} /**
* 旋转
*/
- (void)rotate:(UIButton *)button {
//位移(不累加)
// self.headImageView.transform = CGAffineTransformMakeTranslation(50, 200);
//缩放
// self.headImageView.transform = CGAffineTransformMakeScale(1.2, 0.5);
//在原有的基础上位移(是累加的)
// self.headImageView.transform = CGAffineTransformTranslate(self.headImageView.transform, 50, 50);
//在原有的基础上进行缩放
// self.headImageView.transform = CGAffineTransformScale(self.headImageView.transform, 1.5, 2.0); //在原有的基础上进行旋转
if (button.tag) {
//旋转的角度为1/pi,逆时针
self.headImageView.transform = CGAffineTransformRotate(self.headImageView.transform, -M_1_PI);
} else {
//旋转的角度为pi/2,顺时针
self.headImageView.transform = CGAffineTransformRotate(self.headImageView.transform, M_PI_2);
} } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
} @end
三 实现效果