【代码笔记】iOS-自定义switch

一,效果图。

二,工程图。

三,代码。

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

时间: 2024-08-14 03:31:53

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

【代码笔记】自定义开关

一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> #import "ToggleView.h" @interface RootViewController : UIViewController <ToggleViewDelegate> @property(nonatomic, strong)ToggleView *toggleViewWithLabel; @property(nona

IOS 自定义按钮(代码实现)+九宫格

在一些下载应用里整个页面都是按钮,有好多好多,但是仔细观察不难发现他们很有规律.就像下面一样?? 很有规律的排列在屏幕上,那么这需要我们怎么去做能. 正如标题,我们需要了解两个知识点,分别是自定义按钮和九宫格,九宫格是一种算法.在这里我给大家列出方法,并不过多解释,希望会对大家有帮助. 代码如下: 自定义按钮部分 // // CXButton.m // CX-自定义按钮(代码实现)+九宫格 // // Created by ma c on 16/3/18. // Copyright ? 2016

iOS 自定义导航栏笔记

一.UINavigationBar的结构 导航栏几乎是每个页面都会碰到的问题,一般两种处理方式:1.隐藏掉不显示 2.自定义 1. 添加导航栏 TestViewController * mainVC = [[TestViewController alloc] init]; UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:mainVC]; self.window.ro

WWDC 2014 Session笔记 - iOS界面开发的大一统

本文是我的 WWDC 2014 笔记 中的一篇,涉及的 Session 有 What's New in Cocoa Touch Building Adaptive Apps with UIKit What's New in Interface Builder View Controller Advancements in iOS 8 A Look Inside Presentation Controllers iOS 8 和 OS X 10.10 中一个被强调了多次的主题就是大一统,Apple

自定义Switch过程详解

作者: remcarpediem 联系方式:segmentfault,csdn,简书 本文转载请注明作者.文章来源,链接,版权归作者所有. ?前段时间,我看到了一篇关于Android动画的文章Android View 仿iOS SwitchButton Material Design,十分喜欢文章作者的笔风,可惜每个人的笔风都不同,不过我倒是实现了一个类似的Switch组件,项目地址为https://github.com/ztelur/FunSwitch,就用这篇文章来讲述一下实现过程和机制吧.

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

iOS自定义转场动画实战讲解 转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerAnimated:completion:这一组函数以模态视图的方式展现.隐藏视图.如果用到了navigationController,还可以调用pushViewController:animated:和popViewController这一组函数将新的视图控制器压栈.弹栈. 下图中所有转场动画都是自定义的

android开发笔记之自定义开关按钮

今天来讲讲自定义单个控件,就拿开关按钮来讲讲,相信大家见了非常多这样的了,先看看效果: 我们可以看到一个很常见的开关按钮,那就来分析分析. 首先: 这是由两张图片构成: ①一张为有开和关的背景图片 ②一张为控制开和关的滑动按钮 第一步: 写个类继承View,并重写几个方法: 第一个为构造函数,重写一个参数的函数和两个参数的函数就够了,因为两个参数的函数能够使用自定义属性 第二个为控制控件的大小–>protected void onMeasure(int widthMeasureSpec, int

iOS自定义的UISwitch按钮

UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc]initWithFrame:CGRectMake(200.0,10.0,0.0,0.0)]; 是不是很奇怪,大小竟然是0.0×0.0,没错,系统会自动帮你决定最佳的尺寸,你自己写的尺寸会被忽略掉,你只要定义好相对父视图的位置就好了.默认尺寸为79 * 27. 二.显示控件 [ parrentView

IOS 自定义UITableView

根据不同需要,需要使用tableview的结构,但是里面每一个cell,又需要自己的样式,所以学习了一下怎样把自己定义的cell加到tableview里面 首先要自己创建一个类,继承UITableViewCell,然后新建一个空的xib文件,并在class属性设置为对应的类名 代码部分: <pre name="code" class="objc"> #import "SettingViewController.h" #import &

黑马程序员--IOS学习笔记--IOS初级视频目录

1.第一天: 1.1.开发概述 1.2.IOS设备发展史 1.3.Mac OS X系统介绍 1.4.Mac OS X常见设置 1.5.Mac OS X系统操作(一) 1.6.Mac OS X系统操作(一) 1.7.Mac OS X系统操作(一) 1.8.开发环境Xcode安装.使用 1.9.IOS开发体验之按钮.界面切换 1.10.IOS开发体验之按钮.界面切换(补充) 1.11.IOS开发体验之打电话发短信 1.12.IOS开发体验之<Tom猫> 1.13.计算机程序 1.14.IOS开发基