一 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

三 实现效果


iOS开发 -------- transform属性(形变)的更多相关文章

  1. Hello_IOS ios开发transform属性

    #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutl ...

  2. ios开发transform属性

    #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutl ...

  3. iOS开发-automaticallyAdjustsScrollViewInsets属性

    iOS开发-automaticallyAdjustsScrollViewInsets属性 Available in iOS 7.0 and later. 简单点说就是automaticallyAdju ...

  4. iOS开发-获取属性和方法

    iOS开发数据存储有两种方式,属性列表和对象编码,属性列表可以通过NSArray,NSMutableArray,NSMutableDictionary,存储对象我们可以通过归档和解档来完成.如果我们想 ...

  5. iOS开发之--属性关键字以及set和get方法

    一.属性分为三大类 1.读写性控制 a.readOnly只读,只会生成get方法,不会生成set方法 b.readWrite可读可写,会生成set方法,也会生成get方法(默认设置) 2.setter ...

  6. iOS开发transform的使用

    // //  ViewController.m //  18-transform的使用 #import "ViewController.h" @interface ViewCont ...

  7. IOS学习5——属性与成员变量

    [转]iOS中属性与成员变量的区别 ios中属性修饰符的作用 1. 属性用property声明 2. 简而言之,对于目前的ios开发,属性和成员变量的区别,完全可以不管. 3. 这个是历史原因造成的. ...

  8. iOS开发UI篇—transframe属性(形变)

    iOS开发UI篇—transframe属性(形变) 1. transform属性 在OC中,通过transform属性可以修改对象的平移.缩放比例和旋转角度 常用的创建transform结构体方法分两 ...

  9. iOS开发基础篇-transform属性

    一. transform 属性 在OC中,通过 transform 属性可以修改对象的平移.缩放比例和旋转角度. 1)创建“基于控件初始位置”的形变  CGAffineTransformMakeRot ...

随机推荐

  1. nginx获取uri里面的参数

    add_header Content-Disposition "attachment;fileName=$arg_filename"; 请求连接为:10.26.1.165/abc? ...

  2. ERP项目实施记录10

    好久没有更新,因为进度一直拖着.已经实施了20个月了,很多东西没有开发出来.原因多方面的,虽然在此打算吐槽一下开发公司,但其实很大部分责任还是在我们自己. 不多说了,看图:

  3. Gym 101194H / UVALive 7904 - Great Cells - [数学题+快速幂][2016 EC-Final Problem H]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

  4. Linux下一台服务器Redis主从复制(master-slave)配置

    主从概念 ⼀个master可以拥有多个slave,⼀个slave⼜可以拥有多个slave,如此下去,形成了强⼤的多级服务器集群架构 master用来写数据,slave用来读数据,经统计:网站的读写比率 ...

  5. sap 软件架构

    1:

  6. Git环境配置

    1,下载Git-2.16.2-64-bit.exe并安装, 全部为默认设置 下载地址:http://git-scm.com/download/win 2 在开始菜单中,单击Git CMD,执行下面命令 ...

  7. pwn学习日记Day4 基础知识积累

    知识杂项 *:字符串重复 空指令NOP:\x90 cmp:是比较指令,cmp的功能相当于减法指令.它不保存结果,只是影响相应的标志位. xor:将两个操作数进行异或运算,并将结果存放到操作数1中. s ...

  8. 前端学习历程--js事件监听

    一.事件监听使用场景 1.事件触发多个方法的时候,后一个方法会把前一个方法覆盖掉. window.onload = function(){  var btn = document.getElement ...

  9. linux下安装svn服务器

    http://www.cnblogs.com/zhoulf/archive/2013/02/02/2889949.html 安装说明系统环境:CentOS-6.3安装方式:yum install (源 ...

  10. rocket mq知识点

    1 消费类型 广播消费 : 一条消息被多个消费者消费 集群消费:一个 Consumer Group 中的 Consumer 实例平均分摊消费消息.例如某个 Topic 有 9 条消息,其中一个 Con ...