我的第一個 Swift 語言的開源項目終於發佈到 CocoaPods 啦!

2015-01-27 15:59:32 +08:00
 Elethom

原 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

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.

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:

:-)

Contact

2342 次点击
所在节点    分享创造
3 条回复
Elethom
2015-01-27 16:01:22 +08:00
@Livid
GitHub Flavored Markdown 的 blockquote 樣式有些奇怪... 左側沒有帶顏色的 padding, 也沒有背景之類的樣式來突出引用.
yetone
2015-01-27 23:42:09 +08:00
我是熬着夜专门来赞你的
Elethom
2015-01-28 00:01:56 +08:00
@yetone
感謝! :-)

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

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

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

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

© 2021 V2EX