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 文件,打开后 二.在项目下右键添加新项 在设计页面中打开 从 ...
随机推荐
- springmvc 项目完整示例05 日志 --log4j整合 配置 log4j属性设置 log4j 配置文件 log4j应用
log4j 就是log for java嘛,老外都喜欢这样子,比如那个I18n ---internationalization 不就是i和n之间有18个字母... http://logging.a ...
- Smobiler 4.4已正式发布!(Smobiler能让你在Visual Studio上开发APP)
Smobiler 4.4已经正式发布,还不快来看看?原文地址:https://www.smobiler.com/portal.php?mod=view&aid=53这次更新要感谢我们的用户,在 ...
- HTTP 常见的状态码
- JavaScript-数字和字符串比较大小
JavaScript经常会比较字符串的大小,有的时候容易混淆,因此简单的梳理一下JavaScript字符串的比较: //1.数字比较 console.log('数字比较:' + (12 < 3) ...
- 一文搞懂TCP与UDP的区别
摘要:计算机网络基础 引言 网络协议是每个前端工程师都必须要掌握的知识,TCP/IP 中有两个具有代表性的传输层协议,分别是 TCP 和 UDP,本文将介绍下这两者以及它们之间的区别. 一.TCP/I ...
- 20190321-HTML基本结构
目录 1.HTML概念 超文本标记语言 2.HTML版本 HTML HTML5 3.HTML基本结构 基本结构 元素.标签.属性 4.HTML常用标签 内容 1.HTML概念 HTML(HyperTe ...
- 前端入门18-JavaScript进阶之作用域链
声明 本系列文章内容全部梳理自以下几个来源: <JavaScript权威指南> MDN web docs Github:smyhvae/web Github:goddyZhao/Trans ...
- html5 拖拽上传文件时,屏蔽浏览器默认打开文件
参考: https://www.cnblogs.com/kingsm/p/9849339.html
- 博弈论进阶之Multi-SG
Multi-Nim 从最简单的Nim模型开始 它的定义是这样的 有\(n\)堆石子,两个人可以从任意一堆石子中拿任意多个石子(不能不拿)或把一堆数量不少于\(2\)石子分为两堆不为空的石子,没法拿的人 ...
- 洛谷P5108 仰望半月的夜空(后缀数组)
题意 题目链接 Sol warning:下面这个做法只有95分,本地拍了1w+组都没找到错误我表示十分无能为力 我们考虑每个串的排名去更新答案,显然排名为\(1\)的后缀的前缀一定是当前长度的字典序最 ...