V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
Elethom
V2EX  ›  分享创造

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

  •  
  •   Elethom ·
    Elethom · 2015-01-27 15:59:32 +08:00 · 2350 次点击
    这是一个创建于 3597 天前的主题,其中的信息可能已经有所发展或是发生改变。

    原 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

    Cocoapods

    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

    3 条回复    2015-01-28 00:01:56 +08:00
    Elethom
        1
    Elethom  
    OP
       2015-01-27 16:01:22 +08:00
    @Livid
    GitHub Flavored Markdown 的 blockquote 樣式有些奇怪... 左側沒有帶顏色的 padding, 也沒有背景之類的樣式來突出引用.
    yetone
        2
    yetone  
       2015-01-27 23:42:09 +08:00
    我是熬着夜专门来赞你的
    Elethom
        3
    Elethom  
    OP
       2015-01-28 00:01:56 +08:00
    @yetone
    感謝! :-)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2790 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:13 · PVG 23:13 · LAX 07:13 · JFK 10:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.