iOS开发基础篇-手写控件
一、手写控件的步骤
1)使用相应的控件类创建控件对象;
2)设置该控件的各种属性;
3)添加空间到视图中;
4)如果是 UIButton 等控件,还需考虑控件的单击事件等;
二、添加 UIButton 单击事件
[topbtn addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside];
1) addTarget:forControlEvents: 方法定义在 UIControl 类中,意味着所有继承自 UIControl 类的对象均可添加;
2)第一个参数为对象本身;
3)第二个参数为监听控件的事件。
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIButton *headImageView; //被控制用于移动的按钮
@end
//手工添加上下左右、放大缩小按钮
1 - (void)viewDidLoad {
[super viewDidLoad];
//设置按钮对象为自定义型
UIButton *headbtn = [UIButton buttonWithType:UIButtonTypeCustom];
//设置按钮对象普通状态下的各项属性
headbtn.frame = CGRectMake(, , , );
[headbtn setBackgroundImage:[UIImage imageNamed:@"beauty10"] forState:UIControlStateNormal];
[headbtn setTitle:@"点我!" forState:UIControlStateNormal];
[headbtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//设置按钮对象高亮状态下的属性
[headbtn setBackgroundImage:[UIImage imageNamed:@"beauty11"] forState:UIControlStateHighlighted];
[headbtn setTitle:@"还行吧~~" forState:UIControlStateHighlighted];
[headbtn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
//将按钮对象添加到视图中显示出来
[self.view addSubview:headbtn];
self.headImageView = headbtn; //建立IBOutlet连接 //向上移动的按钮
UIButton *topbtn = [UIButton buttonWithType:UIButtonTypeCustom];
topbtn.frame = CGRectMake(, , , );
[topbtn setTitle:@"上移" forState:UIControlStateNormal];
[topbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[topbtn setTag:];
[self.view addSubview:topbtn];
//添加按钮的单击事件
[topbtn addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside]; //向下移动的按钮
UIButton *downbtn = [UIButton buttonWithType:UIButtonTypeCustom];
downbtn.frame = CGRectMake(, , , );
[downbtn setTitle:@"下移" forState:UIControlStateNormal];
[downbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[downbtn setTag:];
[self.view addSubview:downbtn];
//添加按钮的单击事件
[downbtn addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside]; //向左移动的按钮
UIButton *leftbtn = [UIButton buttonWithType:UIButtonTypeCustom];
leftbtn.frame = CGRectMake(, , , );
[leftbtn setTitle:@"左移" forState:UIControlStateNormal];
[leftbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[leftbtn setTag:];
[self.view addSubview:leftbtn];
//添加按钮的单击事件
[leftbtn addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside]; //向右移动的按钮
UIButton *rightbtn = [UIButton buttonWithType:UIButtonTypeCustom];
rightbtn.frame = CGRectMake(, , , );
[rightbtn setTitle:@"右移" forState:UIControlStateNormal];
[rightbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[rightbtn setTag:];
[self.view addSubview:rightbtn];
//添加按钮的单击事件
[rightbtn addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside]; //放大的按钮
UIButton *zoombtn = [UIButton buttonWithType:UIButtonTypeCustom];
zoombtn.frame = CGRectMake(, , , );
[zoombtn setTitle:@"放大" forState:UIControlStateNormal];
[zoombtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[zoombtn setTag:];
[self.view addSubview:zoombtn];
//添加按钮的单击事件
[zoombtn addTarget:self action:@selector(zoom:) forControlEvents:UIControlEventTouchUpInside]; //缩小的按钮
UIButton *minusbtn = [UIButton buttonWithType:UIButtonTypeCustom];
minusbtn.frame = CGRectMake(, , , );
[minusbtn setTitle:@"缩小" forState:UIControlStateNormal];
[minusbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[minusbtn setTag:];
[self.view addSubview:minusbtn];
//添加按钮的单击事件
[minusbtn addTarget:self action:@selector(zoom:) forControlEvents:UIControlEventTouchUpInside];
}
先声明上下左右移动的枚举类型,及移动或放大缩小的常量值:
//ViewController.m
typedef NS_ENUM(NSUInteger, kMovingDir)
{
kMovingDirTop = ,
kMovingDirButtom,
kMovingDirLeft,
kMovingDirRight,
}; CGFloat const kMovingDelta = 50.0; //移动系数
CGFloat const kZoomingDelta = 50.0; //放大缩小系数
实现按钮的事件:
- (void)move:(id)sender {
UIButton *button = (UIButton *)sender;
CGPoint p = self.headImageView.center;
switch (button.tag) {
case kMovingDirTop: p.y -= kMovingDelta; break; //往上移动
case kMovingDirButtom: p.y += kMovingDelta; break; //往下移动
case kMovingDirLeft: p.x -= kMovingDelta; break; //往左移动
case kMovingDirRight: p.x += kMovingDelta; break; //往右移动
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
self.headImageView.center = p;
[UIView commitAnimations];
}
- (void)zoom:(id)sender {
UIButton *button = (UIButton *)sender;
CGRect rect = self.headImageView.bounds;
if (button.tag) { //缩小
rect.size.width -= kZoomingDelta;
rect.size.height -= kZoomingDelta;
} else { //放大
rect.size.width += kZoomingDelta;
rect.size.height += kZoomingDelta;
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
self.headImageView.bounds = rect;
[UIView commitAnimations];
}
示图如下:

示例代码:http://pan.baidu.com/s/1i3SainN
三、简单的动画
1)开始动画;
2)设置动画相关的参数,如时间等;
3)参与动画的行动
4)提交动画。
示例代码如下:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
self.headImageView.bounds = rect;
[UIView commitAnimations];
参考博客:iOS开发UI基础—手写控件,frame,center和bounds属性
iOS开发基础篇-手写控件的更多相关文章
- iOS开发UI篇—手写控件,frame,center和bounds属性
iOS开发UI基础—手写控件,frame,center和bounds属性 一.手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4 ...
- iOS开发UI基础—手写控件,frame,center和bounds属性
iOS开发UI基础—手写控件,frame,center和bounds属性 一.手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4 ...
- 手写控件,frame,center和bounds属性
一.手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4)如果是button等控件,还需考虑控件的单击事件等 (5)注意:View ...
- iOS开发UI篇—实现UItableview控件数据刷新
iOS开发UI篇—实现UItableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运 ...
- iOS开发基础篇-transform属性
一. transform 属性 在OC中,通过 transform 属性可以修改对象的平移.缩放比例和旋转角度. 1)创建“基于控件初始位置”的形变 CGAffineTransformMakeRot ...
- iOS开发基础篇-Button基础
一.简单介绍 UIButton 的功能:响应用户操作.显示文字.显示图片.调整内部图片和文字的位置. 二. UIButton 的状态 UIControlStateNormal :普通状态,为默认情 ...
- iOS开发——基础篇——iOS开发 Xcode8中遇到的问题及改动
iOS开发 Xcode8中遇到的问题及改动 新版本发布总会有很多坑,也会有很多改动. 一个一个填吧... 一.遇到的问题 1.权限以及相关设置 iOS10系统下调用系统相册.相机功能,或者苹果健康 ...
- cocos2dx基础篇(10) 按钮控件CCControlButton
[3.x] (1)去掉 “CC” (2)对象类 CCObject 改为 Ref (3)按钮事件回调依旧为 cccontrol_selector ,没有使用 CC_CALLBACK_2 (4)按钮状态 ...
- C#-WebForm-WebForm开发基础、如何给控件注册事件?——事件委托写法、http无状态性、三层结构
(小知识 - xml:可扩展的标记语言 html:超文本标记语言) 一.创建WebForm:新建→网站 此时文件夹中只有一个 config 文件,打开后 二.在项目下右键添加新项 在设计页面中打开 从 ...
随机推荐
- JDK源码分析(9)之 WeakHashMap 相关
平时我们使用最多的数据结构肯定是 HashMap,但是在使用的时候我们必须知道每个键值对的生命周期,并且手动清除它:但是如果我们不是很清楚它的生命周期,这时候就比较麻烦:通常有这样几种处理方式: 由一 ...
- SpringCloud应对高并发的思路
一.Eureka的高可用性 Eureka下面的服务实例默认每隔30秒会发送一个HTTP心跳给Eureka,来告诉Eureka服务还活着,每个服务实例每隔30秒也会通过HTTP请求向Eureka获取服务 ...
- 【细语】C#之扩展方法原理及其使用
1.写在前面 今天群里一个小伙伴问了这样一个问题,扩展方法与实例方法的执行顺序是什么样子的,谁先谁后(这个问题会在文章结尾回答).所以写了这边文章,力图从原理角度解释扩展方法及其使用. 以下为主要内容 ...
- Java8之Optional类
写在前头 今天再看阿里的Java开发手册,里面异常处理第10条提到这样一个建议. [推荐]防止 NPE ,是程序员的基本修养,注意 NPE 产生的场景:1 ) 返回类型为基本数据类型,return 包 ...
- [ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project child02
maven打包成war时,报错: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (d ...
- 什么是平衡二叉树(AVL)
前言 Wiki:在计算机科学中,AVL树是最早被发明的自平衡二叉查找树.在AVL树中,任一节点对应的两棵子树的最大高度差为1,因此它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下的时间复杂度都 ...
- Vue在ASP.NET MVC中的进行前后端的交互
Vue在ASP.NET MVC中的进行前后端的交互 Preface: 由于最近在研究前端相关的技术,作为前端非常优秀的框架Vue,个人在学习的过程中遇到一些问题,网上相关资料有限,所以在这这里总结一下 ...
- Mysql 连接数,最大并发数设置
项目中可能会遇到MySQL: ERROR 1040: Too many connections”的异常情况,造成这种情况的一种原因是访问量过高,MySQL服务器抗不住,这个时候就要考虑增加从服务器分散 ...
- Linux基础(Ubuntu16.04):安装vim及配置
1.进入终端 Ctrl + Alt +T 出现终端窗口 2.输入命令: sudo apt-get install vim-gtk 3.验证是否成功 安装完vim后查看命令 vi tab键,就会关联出 ...
- python运行逻辑
Python程序在解释器上执行分两个过程: 编译:如果Python进程在机器上拥有写入权限,那么它会把程序的字节码保存为一个以 .pyc 为扩展名的文件.当程序运行后,会在源代码的同一个目录下看到 . ...