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了。