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 文件,打开后 二.在项目下右键添加新项 在设计页面中打开 从 ...
随机推荐
- 超级账本fabric原理之gossip详解
Goosip协议 去中心化.容错和最终一致性的算法 信息达到同步的最优时间:log(N). 功能: 节点发现 数据广播 gossip中有三种基本的操作: push - A节点将数据(key,value ...
- Mongodb副本集--Out of memory: Kill process 37325 (mongod)
1.Mongodb副本集--Out of memory: Kill process 37325 (mongod) 场景描述: 恢复一个22TB数据的mongodb实例的时候. 将备用结点加入mongo ...
- javascript基础修炼(7)——Promise,异步,可靠性
开发者的javascript造诣取决于对[动态]和[异步]这两个词的理解水平. 一. 别人是开发者,你也是 Promise技术是[javascript异步编程]这个话题中非常重要的,它一度让我感到熟悉 ...
- webpack4.0各个击破(10)—— Integration篇
webpack作为前端最火的构建工具,是前端自动化工具链最重要的部分,使用门槛较高.本系列是笔者自己的学习记录,比较基础,希望通过问题 + 解决方式的模式,以前端构建中遇到的具体需求为出发点,学习we ...
- C# IQueryable和IEnumerable的区别
在使用EF查询数据的时候,我们常用的查询数据方式有linq to sql,linq to object, 查询返回的结果有两种类型:IQueryable.IEnumerable,两者内部的处理机制是完 ...
- 【转】Android 开发规范(完结版)
摘要 1 前言 2 AS 规范 3 命名规范 4 代码样式规范 5 资源文件规范 6 版本统一规范 7 第三方库规范 8 注释规范 9 测试规范 10 其他的一些规范 1 前言 为了有利于项目维护.增 ...
- 为什么要学习Java虚拟机
为什么要学习Java虚拟机? 为什么要学习JVM? 学习Java虚拟机有什么好处? 这些问题就好像在问Java程序猿:你为什么要变强大! C++程序编译后可直接运行于物理机CPU上.而Java程序则不 ...
- Javascript异步编程之一异步原理
本系列的例子主要针对node.js环境,但浏览器端的原理应该也是类似的. 本人也是Javascript新手,把自己这段时间学习积累的要点总结下来,希望可以对同样在学习Javascript/node.j ...
- Easyui 实现点击不同树节点打开不同tab页展示不同datagrid表数据设计
实现点击不同树节点打开不同tab页展示不同datagrid表数据设计 by:授客 QQ:1033553122 测试环境 jquery-easyui-1.5.3 需求描述 如上图, 1.点击左侧树,叶子 ...
- 从Linux上传到Git过程
1.1 实验内容 本次课程讲的是在实验楼的在线环境中,如何使用 Github 去管理在在线环境中使用的代码.配置.资源等实验相关文件,怎样去添加.同步和下拉在远程仓库中的实验文件,以此来维持自身的实验 ...