一,效果图。

二,工程图。

三,代码。

ViewController.h

#import <UIKit/UIKit.h>
#import "CustomSwitch.h" @interface ViewController : UIViewController
@property (nonatomic, strong) CustomSwitch * leftSwitch;
@property (nonatomic, strong) CustomSwitch * rightSwitch;
@end

ViewController.m

#import "ViewController.h"
#import "CustomSwitch.h" #define TAG_COLOR BOTTOM_CLICK_COLOR
#define BOTTOM_CLICK_COLOR Color(41, 115, 192, 1)
#define Color(r, g, b,d) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:d] @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//初始化选择框页面
[self addSwitchView];
}
#pragma -mark -functions
//初始化选择框页面
-(void)addSwitchView
{
CustomSwitch *leftSwitch = [[CustomSwitch alloc]initWithFrame:CGRectMake(20, 100, 200, 50) onColor:TAG_COLOR offColor:Color(214, 214, 214, 1) font:[UIFont systemFontOfSize:15] ballSize:25]; leftSwitch.onText = @"技术支持";
leftSwitch.offText = @"未技术支持";
leftSwitch.userInteractionEnabled = YES;
leftSwitch.on = NO;
leftSwitch.tag = 0;
[leftSwitch addTarget:self action:@selector(handleTapLeftSwitch:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:leftSwitch]; CustomSwitch *rightSwitch = [[CustomSwitch alloc]initWithFrame:CGRectMake(20, 200, 200, 50) onColor:TAG_COLOR offColor:Color(214, 214, 214, 1) font:[UIFont systemFontOfSize:15] ballSize:25];
rightSwitch.onText = @"已解决";
rightSwitch.offText = @"未解决";
rightSwitch.userInteractionEnabled = YES;
rightSwitch.on = NO;
leftSwitch.tag = 1;
[rightSwitch addTarget:self action:@selector(handleTapRightSwitch:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:rightSwitch]; }
#pragma -mark -functions
//左侧按钮点击事件
- (void)handleTapLeftSwitch:(CustomSwitch *)customSwitch
{
NSLog(@"左侧按钮点击事件");
}
//右侧按钮点击事件
- (void)handleTapRightSwitch:(CustomSwitch *)customSwitch
{
NSLog(@"右侧按钮点击事件"); } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

【代码笔记】iOS-自定义switch的更多相关文章

  1. 【代码笔记】Web-JavaScript-JavaScript switch语句

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  2. iOS 自定义导航栏笔记

    一.UINavigationBar的结构 导航栏几乎是每个页面都会碰到的问题,一般两种处理方式:1.隐藏掉不显示 2.自定义 1. 添加导航栏 TestViewController * mainVC ...

  3. iOS自定义的UISwitch按钮

    UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc] ...

  4. iOS 自定义转场动画浅谈

    代码地址如下:http://www.demodashi.com/demo/11612.html 路漫漫其修远兮,吾将上下而求索 前记 想研究自定义转场动画很久了,时间就像海绵,挤一挤还是有的,花了差不 ...

  5. iOS自定义转场动画实战讲解

    iOS自定义转场动画实战讲解   转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerA ...

  6. 【iOS自定义键盘及键盘切换】详解

    [iOS自定义键盘]详解 实现效果展示: 一.实现的协议方法代码 #import <UIKit/UIKit.h> //创建自定义键盘协议 @protocol XFG_KeyBoardDel ...

  7. 笔记-iOS 视图控制器转场详解(上)

    这是一篇长文,详细讲解了视图控制器转场的方方面面,配有详细的示意图和代码,为了使得文章在微信公众号中易于阅读,seedante 辛苦将大量长篇代码用截图的方式呈现,另外作者也在 Github 上附上了 ...

  8. IOS开发笔记 IOS如何访问通讯录

    IOS开发笔记  IOS如何访问通讯录 其实我是反对这类的需求,你说你读我的隐私,我肯定不愿意的. 幸好ios6.0 以后给了个权限控制.当打开app的时候你可以选择拒绝. 实现方法: [plain] ...

  9. Hadoop学习笔记—5.自定义类型处理手机上网日志

    转载自http://www.cnblogs.com/edisonchou/p/4288737.html Hadoop学习笔记—5.自定义类型处理手机上网日志 一.测试数据:手机上网日志 1.1 关于这 ...

  10. OpenGL ES: iOS 自定义 UIView 响应屏幕旋转

    iOS下使用OpenGL 如果使用GLKit View 那么不用担心屏幕旋转的问题,说明如下: If you change the size, scale factor, or drawable pr ...

随机推荐

  1. 解决微信小程序要求的TLS版本必须大于等于1.2的问题

    一.环境: CentOS 6.8 nginx 1.6.0 php 7.0.10 二.背景 最近开发一个小程序,而小程序对后台接口服务器的要求是: 1.请求域名在request合法域名中 2.基于 ht ...

  2. canvas 实现签名效果

    效果图 概述 在线签名,现在在很多场景下都能看到,而且在移动端见的比较多. 用canvas和svg都可以实现,而且跨平台能力也很好. canvas基于像素,提供 2D 绘制函数,提供的功能更原始,适合 ...

  3. git fetch 、git pull 与 git pull --rebase

    1. git fetch 与 git pull 都是从远程拉取代码到本地,git fetch只是拉取到本地,git pull不仅拉取到本地还merge到本地分支中.所以git pull是git fet ...

  4. POJ 2840

    #include<iostream> #include<stdio.h> #include<string> using namespace std; int mai ...

  5. POJ 2505

    #include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("a ...

  6. This Gradle plugin requires Studio 3.0 minimum

    从github上下载的项目遇到一个问题:Error:This Gradle plugin requires Studio 3.0 minimum 意思就是说studio版本不高,导入的项目的版本是3. ...

  7. SVN Hooks的介绍及使用

    阅读此篇文章你可以: 对SVN Hooks有一定的了解 获取两个最常用的SVN Hooks案例 SVN hooks介绍 Hooks 钩子,主要实现的功能就是在特定事件发生之前或者之后自动执行事先定义好 ...

  8. Vue笔记:使用node开发vue入门实例

    安装NPM 首先在命令终端输入 npm -v 检测是否安装 npm.如果没有,按照下面教程进行安装. 下载地址: nodejs中文网 到官网下载自己系统对应的版本,这里我们下载Windows系统的64 ...

  9. 散列算法-SHA

    一种生成信息摘要的算法.主要用于数据一致性和完整性的校验 SHA算法分很多版本,最大的分类是SHA-1和SHA-2.SHA-2包括很多子版本,SHA-224,SHA-256,SHA-384,SHA-5 ...

  10. html中img图片居中

    直接看代码 style="display:block; margin:0 auto;" 可以看到,蓝色的代码是让 块状元素居中 而红色的代码,是让img转成块状元素 ···原来im ...