先大概看下我们想实现简单的效果

源码

//
// DSegmentedControl.swift
// IOS学习之自定义UISegmentedControl
//
// Created by ding_qili on 16/9/12.
// Copyright © 2016年 ding_qili. All rights reserved.
// import UIKit @IBDesignable class DSegmentedControl: UIControl { /*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
var titleColor:UIColor = UIColor.gray //选择状态
var titleColorDisable:UIColor = UIColor.darkGray //未选中状态
let slide = CALayer() //滑块 @IBInspectable var titles:[String]{ //标题
didSet{
guard titles.count > 0 else {
return
}
for title in 0..<titles.count {
let label = UIButton();
label.setTitle(titles[title], for: UIControlState());
label.setTitleColor(titleColor, for: UIControlState.disabled)
label.setTitleColor(titleColorDisable, for: UIControlState())
label.addTarget(self, action: #selector(changeValue), for: UIControlEvents.touchUpInside);
self.addSubview(label);
}
setNeedsLayout()
} } override func tintColorDidChange() {
super.tintColorDidChange() } var selecteIndex:Int = 0 {
didSet{
refrushSubView()
}
} override init(frame: CGRect) {
titles = []
super.init(frame: frame)
finishInit()
} requi 大专栏  自定义一个简单的SegmentedControlred init?(coder aDecoder: NSCoder) {
titles = []
super.init(coder: aDecoder)
finishInit()
} func changeValue(_ sender:UIView){
if let index = self.subviews.index(of: sender){
selecteIndex = index;
self.sendActions(for: UIControlEvents.valueChanged)
}
} func finishInit(){
titleColor = self.tintColor
slide.frame = CGRect(x: 0, y: 0, width: self.frame.width/2, height: 1)
slide.backgroundColor = titleColor.cgColor
self.layer.addSublayer(slide)
self.backgroundColor = UIColor.clear
} override func layoutSubviews() {
super.layoutSubviews()
refrushSubView()
} func refrushSubView(){
for item in self.subviews.enumerated() {
item.element.frame = CGRect(x: self.frame.width/CGFloat(titles.count) * CGFloat(item.offset), y: 0, width: self.frame.width/CGFloat(titles.count), height: self.frame.height)
if selecteIndex == item.offset{
var center = item.element.center;
center.y = self.frame.height;
slide.position = center;
}
if let uIControl = item.element as? UIControl {
uIControl.isEnabled = (selecteIndex != item.offset);
} }
} }
1. 选择继承UIControl,是想要通过UIControlEvents.valueChanged的方式来通知调用者切换状态
2. 默认是用了TintColor作为主色调来使用
3. `segment.titles = ["简介","评论"]` 就可以使用了
4. `segment.addTarget(self, action: #selector(segmentValueChange), for: UIControlEvents.valueChanged)` 回调选择切换
5. 代码比较简单,用于简单的学习

最新文章

  1. Duilib嵌入CEF以及JavaScript与C++交互
  2. java中myeclipse连接mysql问题(java.lang.ClassNotFoundException: com.mysql.jdbc.Driver)
  3. 为什么有禁用Mac系统的Spotlight的需求:
  4. 剑指Offer 二维数组中的查找
  5. Floyd 求最短路(poj 1161)
  6. Andrew Ng机器学习公开课笔记 &ndash; Factor Analysis
  7. angular模板加载 ----ng-template
  8. iOS开发——UI篇Swift篇&amp;UIToolbar
  9. linux的cron服务及应用
  10. 字母A-Z写法
  11. 2016022610 - redis列表命令集合
  12. UITableView的简单应用介绍
  13. 数字签名.sys文件的步骤
  14. SQL Server 视图
  15. IO与文件读写---使用Apache commons IO包提高读写效率
  16. TCP和UDP 的区别和适用场合
  17. 一个页面多个iframe赋值
  18. HighCharts之气泡图报错
  19. How Tomcat works — 三、tomcat启动(2)
  20. rsync同步公网yum源搭建本地yum仓库

热门文章

  1. 在scala命令行中加入类库
  2. RepeatSubmitInterceptor extends HandlerInterceptorAdapter
  3. 60年前美国军方的这个编程原则,造就了多少伟大的框架--KISS原则
  4. 第二季第十一天 html5语义化标签 css透明度
  5. JavaSE--异常信息打印
  6. Django专题-AJAX
  7. 针对Oracle的一系列操作
  8. document.write的时机
  9. zabbix3.4--监控TCP十一种状态
  10. spring自定义aop