昨天用 Swift 重寫了之前的一個開源庫 PRSlideView, 今天整理好發佈到 GitHub 上了.
注: 沒有發佈到 CocoaPods 是因為沒有足夠的磁盤空間安裝新的編譯工具通過 CocoaPods 發佈前驗證. 你可以通過給我捐贈一台 最新的頂配 MacBook Pro 以解決這個問題. XD
This is the Swift language version of PRSlideView.
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.
slideView.delegate = self
slideView.dataSource = self
slideView.scrollDirection = .Vertical
slideView.infiniteScrollingEnabled = true
slideView.registerClass(
PRAlbumPage.self,
identifier: PRAlbumPage.description()
)
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")
}
}
// 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
}
// 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.
This code is distributed under the terms and conditions of the MIT license.
You can support me by:
:-)
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.