Swift iOS开发小书 ,帮你快速上手开发 www.ituring.com.cn/book/2413ios
你建立了一个迷幻的View,想要向全世界共享它。怎么办?cocoapods能够帮忙。git
##建立一个工程,其中有你须要分享的代码github
首先,咱们建立这样的一个Single View App。添加一个swift文件(名字为:FantasticView.swift),内容为:swift
import UIKit
class FantasticView : UIView {
let colors : [UIColor] = [.red, .orange, .yellow, .green, .blue, .purple]
var colorCounter = 0
override init(frame: CGRect) {
super.init(frame: frame)
let scheduledColorChanged = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { (timer) in //1
UIView.animate(withDuration: 2.0) { //2
self.layer.backgroundColor = self.colors[self.colorCounter % 6].cgColor //3
self.colorCounter+=1 //4
}
}
scheduledColorChanged.fire()
// The Main Stuff
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// You don't need to implement this
}
}复制代码
而后编写代码,作一个demo,把这个视图用起来,代码覆盖ViewController.swift:bash
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let fantasticView = FantasticView(frame: self.view.bounds)
self.view.addSubview(fantasticView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}复制代码
跑起来后,你能够看到一个变化颜色的视图,说明类FantasticView正常工做了。这个文件,就是咱们想要分享的。app
接下来,若是想要CocoaPods帮忙,给建立一个文件.podspec文件,并把这个文件传递到CocoaPods网站上。此文件会告诉想要使用此pod的人以下信息:ide
1. 这个pod叫什么名字
2. 版本号多少
3. 代码包括哪些文件
4. 文件在哪里复制代码
等等必要的和描述性的信息。网站
文件咱们会放到github仓库上,所以,一个github的帐号是必要的。仓库须要建立一个release,由于podspec会引用此release。个人相关信息:ui
你须要在以下的命令和操做中,替代为你的对应信息。首先this
在命令行内导航到你的工程目录,执行命令以便把代码传递到github仓库内
git init
git add .
git commit -m "init"
git remote add origin
git push -u origin master
依然须要在命令行内,导航到你的工程目录,而后:
touch FantasticView.podspec复制代码
粘贴内容为:
Pod::Spec.new do |s|
s.name = 'FantasticView'
s.version = '0.1.0'
s.summary = '一些屁话'
s.description = <<-DESC
更多行
行的
屁话
DESC
s.homepage = 'https://github.com/1000copy/FantasticView'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { '1000copy' => '1000copy@gmail.com' }
s.source = { :git => 'https://github.com/1000copy/FantasticView.git', :tag => s.version.to_s }
s.ios.deployment_target = '10.0'
s.source_files = 'FantasticView/*'
end复制代码
其中特别重要的是s.name ,s.version , s.source ,s.source_files,没有这些信息,发布pod是不可能的。
接着执行命令,检查此podspec是否正确:
pod lib lint复制代码
执行命令发布,首先注册你的邮件,而后推送podspec。
pod trunk register 1000copy@gmail.com
pod trunk push FantasticView.podspec复制代码
注意,注册邮件后,cocoapods会发送一个邮件给你,你点击里面的确认连接,便可生效。
你应该看到以下的信息反馈:
🎉 Congrats
🚀 FantasticView (0.1.0) successfully published
📅 August 16th, 06:03
🌎 https://cocoapods.org/pods/FantasticView
👍 Tell your friends!复制代码
如今,你能够像其余pod同样使用FantasticView了。