一 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. web.py框架之i18n支持

    问题: 在web.py的模板文件中, 如何得到i18n的支持? Solution: 项目目录结构: proj/ |- code.py |- i18n/ |- messages.po |- en_US/ ...

  2. 用memset设置无穷大无穷小

    memeset是以字节为单位进行赋值的,对字符数组可以直接用. 但对于int数组就不行了. 但设置无穷大来说有个技巧: 如果我们将无穷大设为0x3f3f3f3f,那么奇迹就发生了,0x3f3f3f3f ...

  3. 【Java线程安全】 — ThreadLocal

    [用法] 首先明确,ThreadLocal是用空间换时间来解决线程安全问题的,方法是各个线程拥有自己的变量副本. 既然如此,那么是涉及线程安全,必然有一个共享变量,我给大家声明一个: public c ...

  4. Linux上传文件与执行

    ls ——查看文件夹 mkdir——新建文件夹 cd:——进入文件 nohup python3 文件名.py & ——让代码在后台运行 ps -aux | grep 文件——查看进程 ps-a ...

  5. ubuntu 16.04 安装和配置vncserver

    https://www.linode.com/docs/applications/remote-desktop/install-vnc-on-ubuntu-16-04/#connect-to-vnc- ...

  6. 基于 redis 的分布式锁实现 Distributed locks with Redis debug 排查错误

    小结: 1. 锁的实现方式,按照应用的实现架构,可能会有以下几种类型: 如果处理程序是单进程多线程的,在 python下,就可以使用 threading 模块的 Lock 对象来限制对共享变量的同步访 ...

  7. gitlab备份、恢复、升级

    1.备份 gitlab的备份很简单,只要使用命令: gitlab-rake gitlab:backup:create 即可将当前的数据库.代码全部备份到/var/opt/gitlab/backups ...

  8. 【论文阅读】Deep Adversarial Subspace Clustering

    导读: 本文为CVPR2018论文<Deep Adversarial Subspace Clustering>的阅读总结.目的是做聚类,方法是DASC=DSC(Deep Subspace ...

  9. java框架之Hibernate(1)-简介及初使用

    简介 hibernate 是一个开源 ORM ( Object / Relationship Mipping ) 框架,它是对象关联关系映射的持久层框架,它对 JDBC 做了轻量级的封装,而我们 ja ...

  10. 自动微分(AD)学习笔记

    1.自动微分(AD) 作者:李济深链接:https://www.zhihu.com/question/48356514/answer/125175491来源:知乎著作权归作者所有.商业转载请联系作者获 ...