原 po: 我的第一個 Swift 開源庫 PRSlideView-Swift
今天清理了一下磁盤, 更新到 beta 版本的 CocoaPods, 將寫好的項目發佈到 CocoaPods 上去了. \w/
在 CocoaPods 發佈 Swift 項目需要將 Command Line Tools 更新到最新版本:
$ xcode-select --install
更新 CocoaPods 到最新的測試版:
$ gem install cocoapods --pre
在國內使用淘寶源的朋友需要切換到官方源:
$ gem sources -r http://ruby.taobao.org/
$ gem sources -r https://ruby.taobao.org/
$ gem sources -a https://rubygems.org/
由于淘宝镜像这边没有实现 /api 下面的协议,而安装 pre 版本需要这些东西检查依赖,所以如果你需要这类安装需求的时候,请临时切换回官方的 RubyGems 源。
最後發佈 pod:
$ pod trunk push
PRSlideView-Swift
This is the Swift language version of PRSlideView.
General
Slide view with gracefully written UIKit-like methods, delegate and data source protocol. Infinite scrolling supported.
Note: Auto layout not supported due to the special behaviours of UIScrollView. Please use autoresizing mask instead or wrap it with a container view.
Installation
With CocoaPods
In your Podfile:
pod 'PRSlideView-Swift'
Usage
Configure a Slide View
slideView.delegate = self
slideView.dataSource = self
slideView.scrollDirection = .Vertical
slideView.infiniteScrollingEnabled = true
slideView.registerClass(
PRAlbumPage.self,
identifier: PRAlbumPage.description()
)
Create a Slide View Page Subclass
import UIKit
public class PRAlbumPage: PRSlideViewPage {
public private(set) var coverImageView: UIImageView
required public init(frame: CGRect, identifier: String) {
self.coverImageView = UIImageView()
super.init(frame: frame, identifier: identifier)
let coverImageView = self.coverImageView
coverImageView.frame = self.bounds
coverImageView.autoresizingMask = (.FlexibleWidth | .FlexibleHeight)
coverImageView.contentMode = .ScaleAspectFit
self.addSubview(coverImageView)
}
required public init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Use Data Source
// MARK: PRSlideViewDataSource
func numberOfPagesInSlideView(slideView: PRSlideView) -> Int {
return self.albumData.count
}
func slideView(slideView: PRSlideView, pageAtIndex index: Int) -> PRSlideViewPage {
let page: PRAlbumPage = slideView.dequeueReusablePageWithIdentifier(PRAlbumPage.description(), index: index) as PRAlbumPage
let imageName: String = self.albumData[index].stringByAppendingPathExtension("jpg")!
page.coverImageView.image = UIImage(named: imageName)
return page
}
Use Delegate
// MARK: PRSlideViewDelegate
func slideView(slideView: PRSlideView, didScrollToPageAtIndex index: Int) {
self.titleLabel.text = self.albumData[index]
}
func slideView(slideView: PRSlideView, didClickPageAtIndex index: Int) {
let alertView: UIAlertView = UIAlertView(
title: "You clicked on an album",
message: "Title: \(self.albumData[index])",
delegate: nil,
cancelButtonTitle: "OK")
alertView.show()
}
All done! You can check out the code in the demo provided.
License
This code is distributed under the terms and conditions of the MIT license.
Donate
You can support me by:
- sending me iTunes Gift Cards;
- via Alipay: [email protected]
- via PayPal: [email protected]
:-)
