我的第一個 Swift 開源庫 PRSlideView-Swift

2015-01-26 20:56:43 +08:00
 Elethom

昨天用 Swift 重寫了之前的一個開源庫 PRSlideView, 今天整理好發佈到 GitHub 上了.

注: 沒有發佈到 CocoaPods 是因為沒有足夠的磁盤空間安裝新的編譯工具通過 CocoaPods 發佈前驗證. 你可以通過給我捐贈一台 最新的頂配 MacBook Pro 以解決這個問題. XD

View on GitHub

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.

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:

:-)

Contact

3483 次点击
所在节点    iDev
19 条回复
Cee
2015-01-26 20:59:08 +08:00
正好看到前来膜拜(ノ ゚∀。)ノ
boom11235
2015-01-26 21:02:54 +08:00
膜拜
WildCat
2015-01-26 21:03:57 +08:00
膜拜
WildCat
2015-01-26 21:04:35 +08:00
膜拜(不能队形,楼上被吞了)
RIcter
2015-01-26 21:15:57 +08:00
捐赠顶配的mbpr!
Elethom
2015-01-26 21:30:20 +08:00
@Cee
@boom11235
@WildCat
@RIcter

這麼不乖小心 @Livid 打你們屁屁喔~
Livid
2015-01-26 21:36:52 +08:00
正确的姿势是点击主题下面的“感谢”按钮 🙏
kxxoling
2015-01-26 22:24:06 +08:00
@Livid iPad safari 下点击 +1 无效。
msxcms
2015-01-26 22:28:08 +08:00
にっこにっこにー
KotiyaSanae
2015-01-27 00:58:58 +08:00
菊苣菊苣菊
moliliang
2015-01-27 01:31:47 +08:00
好想看看效果啊。。
Elethom
2015-01-27 02:59:11 +08:00
@moliliang
有 demo, 可以直接 run.
hkongm
2015-01-27 08:49:57 +08:00
已感谢。
chenggiant
2015-01-27 09:28:41 +08:00
已感谢。
jprovim
2015-01-27 10:04:32 +08:00
寫的質量還OK.
cralison
2015-01-27 10:17:54 +08:00
下来学习,谢谢:)
ciwonderful
2015-01-27 12:50:07 +08:00
已感谢。
tony1016
2015-01-27 13:18:41 +08:00
不错哦,鼓励一下
Thu
2015-01-31 13:40:02 +08:00
Git上给两张效果图就更好了,在下载前能有个总体的印象

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/165654

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX