iOS开发 -------- transform属性(形变)
一 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属性(形变)的更多相关文章
- Hello_IOS ios开发transform属性
#import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutl ...
- ios开发transform属性
#import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutl ...
- iOS开发-automaticallyAdjustsScrollViewInsets属性
iOS开发-automaticallyAdjustsScrollViewInsets属性 Available in iOS 7.0 and later. 简单点说就是automaticallyAdju ...
- iOS开发-获取属性和方法
iOS开发数据存储有两种方式,属性列表和对象编码,属性列表可以通过NSArray,NSMutableArray,NSMutableDictionary,存储对象我们可以通过归档和解档来完成.如果我们想 ...
- iOS开发之--属性关键字以及set和get方法
一.属性分为三大类 1.读写性控制 a.readOnly只读,只会生成get方法,不会生成set方法 b.readWrite可读可写,会生成set方法,也会生成get方法(默认设置) 2.setter ...
- iOS开发transform的使用
// // ViewController.m // 18-transform的使用 #import "ViewController.h" @interface ViewCont ...
- IOS学习5——属性与成员变量
[转]iOS中属性与成员变量的区别 ios中属性修饰符的作用 1. 属性用property声明 2. 简而言之,对于目前的ios开发,属性和成员变量的区别,完全可以不管. 3. 这个是历史原因造成的. ...
- iOS开发UI篇—transframe属性(形变)
iOS开发UI篇—transframe属性(形变) 1. transform属性 在OC中,通过transform属性可以修改对象的平移.缩放比例和旋转角度 常用的创建transform结构体方法分两 ...
- iOS开发基础篇-transform属性
一. transform 属性 在OC中,通过 transform 属性可以修改对象的平移.缩放比例和旋转角度. 1)创建“基于控件初始位置”的形变 CGAffineTransformMakeRot ...
随机推荐
- 从零开始搭建Go语言开发环境
一步一步,从零搭建Go语言开发环境. 安装Go语言及搭建Go语言开发环境 下载 下载地址 Go官网下载地址:https://golang.org/dl/ Go官方镜像站(推荐):https://gol ...
- codechef cook 103 div2
第一次打codechef...不太会用这oj. A: #include <bits/stdc++.h> #define mk(a,b) make_pair(a,b) #define pii ...
- D2
Cosmic Cleaner: 为什么大家都知道球缺怎么求,我没听说过啊??? 我真的是印象里今天第一次听说球缺这个东西啊... 我一看,哇,神仙几何题,毫无头绪,投了投了,然后就被过穿了??? tl ...
- Nestjs 增加全局前缀
文档 const app = await NestFactory.create(AppModule); app.setGlobalPrefix('v1'); // http://localhost:5 ...
- JavaScript Promise:去而复返
原文:http://www.html5rocks.com/en/tutorials/es6/promises/ 作者:Jake Archibald 翻译:Amio 女士们先生们,请准备好迎接 Web ...
- hadoop伪分布环境快速搭建
1.首先下载一个完成已经进行简单配置好的镜像文件(hadoop,HBASE,eclipse,jdk环境已经搭建好,tomcat为7.0版本,建议更改为tomcat8.5版本,运行比较稳定). 2安装V ...
- java+js实现展示本地文件夹下的所有图片demo[申明:来源于网络]
java+js实现展示本地文件夹下的所有图片demo[申明:来源于网络] 地址:http://blog.csdn.net/allgis/article/details/46364875
- Codeforces 1089K - King Kog's Reception - [线段树][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem K]
题目链接:https://codeforces.com/contest/1089/problem/K time limit per test: 2 seconds memory limit per t ...
- [redis] 与redis cluster有关的学习笔记
主要是以下三个官方文档,只略读了前两个,第三个还没有读. <redis cluster tutorial> <redis sentinel> <redis cluster ...
- Spring Boot事务管理(下)
在上两篇 Spring Boot事务管理(上)和Spring Boot事务管理(中)的基础上介绍注解@Transactional. 5 @Transactional属性 属性 类型 描述 value ...