V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
Elethom
V2EX  ›  iDev

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

  •  3
     
  •   Elethom ·
    Elethom · Jan 26, 2015 · 4119 views
    This topic created in 4117 days ago, the information mentioned may be changed or developed.

    昨天用 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

    19 replies    2015-01-31 13:40:02 +08:00
    Cee
        1
    Cee  
       Jan 26, 2015
    正好看到前来膜拜(ノ ゚∀。)ノ
    boom11235
        2
    boom11235  
       Jan 26, 2015
    膜拜
    WildCat
        3
    WildCat  
       Jan 26, 2015
    膜拜
    WildCat
        4
    WildCat  
       Jan 26, 2015
    膜拜(不能队形,楼上被吞了)
    RIcter
        5
    RIcter  
       Jan 26, 2015 via iPad
    捐赠顶配的mbpr!
    Elethom
        6
    Elethom  
    OP
       Jan 26, 2015   ❤️ 1
    @Cee
    @boom11235
    @WildCat
    @RIcter

    這麼不乖小心 @Livid 打你們屁屁喔~
    Livid
        7
    Livid  
    MOD
    PRO
       Jan 26, 2015
    正确的姿势是点击主题下面的“感谢”按钮 🙏
    kxxoling
        8
    kxxoling  
       Jan 26, 2015
    @Livid iPad safari 下点击 +1 无效。
    msxcms
        9
    msxcms  
       Jan 26, 2015
    にっこにっこにー
    KotiyaSanae
        10
    KotiyaSanae  
       Jan 27, 2015
    菊苣菊苣菊
    moliliang
        11
    moliliang  
       Jan 27, 2015
    好想看看效果啊。。
    Elethom
        12
    Elethom  
    OP
       Jan 27, 2015 via iPhone
    @moliliang
    有 demo, 可以直接 run.
    hkongm
        13
    hkongm  
       Jan 27, 2015
    已感谢。
    chenggiant
        14
    chenggiant  
       Jan 27, 2015
    已感谢。
    jprovim
        15
    jprovim  
       Jan 27, 2015
    寫的質量還OK.
    cralison
        16
    cralison  
       Jan 27, 2015
    下来学习,谢谢:)
    ciwonderful
        17
    ciwonderful  
       Jan 27, 2015
    已感谢。
    tony1016
        18
    tony1016  
       Jan 27, 2015
    不错哦,鼓励一下
    Thu
        19
    Thu  
       Jan 31, 2015
    Git上给两张效果图就更好了,在下载前能有个总体的印象
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   743 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 94ms · UTC 20:42 · PVG 04:42 · LAX 13:42 · JFK 16:42
    ♥ Do have faith in what you're doing.