Created
June 23, 2015 03:16
-
-
Save codetalks-new/118fe77cbb72c7018cd8 to your computer and use it in GitHub Desktop.
LoadMoreControl like UIRefreshControl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LoadMoreControl.swift | |
// GuilinLegal | |
// | |
// Created by Haizhen Lee on 15/6/19. | |
// Copyright (c) 2015年 banxi1988. All rights reserved. | |
// | |
import UIKit | |
final class LoadMoreControl: UIControl{ | |
enum LoadMoreState:Int{ | |
case Pulling | |
case Pulled | |
case Canceled | |
case Loading | |
case Loaded | |
case LoadFailed | |
var tipLabel:String{ | |
if self == .Pulling || self == .Canceled{ | |
return "下拉显示下20条" | |
}else if self == .Pulled{ | |
return "释放显示下20条" | |
}else if self == .Loading{ | |
return "正在加载..." | |
}else if self == .Loaded{ | |
return "加载完成" | |
}else if self == .LoadFailed{ | |
return "加载失败" | |
} | |
return "" | |
} | |
} | |
private var loadMoreState = LoadMoreState.Canceled | |
let titleLabel = UILabel(frame: CGRectZero) | |
private let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge) | |
init() { | |
super.init(frame: CGRect(x: 0, y: 0, width: 320, height: 44)) | |
addSubview(titleLabel) | |
addSubview(activityIndicator) | |
titleLabel.setTranslatesAutoresizingMaskIntoConstraints(false) | |
titleLabel.pinCenterY() | |
titleLabel.pinCenterX() | |
titleLabel.textColor = UIColor(white: 0.3, alpha: 1.0) | |
titleLabel.font = UIFont.systemFontOfSize(14) | |
activityIndicator.pinTrailingToSibing(titleLabel) | |
activityIndicator.pinCenterY() | |
activityIndicator.hidden = true | |
} | |
private func transitionToState(loadMoreState:LoadMoreState){ | |
self.loadMoreState = loadMoreState | |
activityIndicator.hidden = loadMoreState != .Loading | |
titleLabel.text = loadMoreState.tipLabel | |
} | |
var isPulling: Bool{ return loadMoreState == .Pulling } | |
var isPulled: Bool{ return loadMoreState == .Pulled } | |
var isCanceled: Bool{ return loadMoreState == .Canceled } | |
var isLoading: Bool{ return loadMoreState == .Loading } | |
func startPull(){ | |
transitionToState(.Pulling) | |
} | |
func pulled(){ | |
transitionToState(.Pulled) | |
} | |
func canceled(){ | |
transitionToState(.Canceled) | |
} | |
func startLoad(){ | |
transitionToState(.Loading) | |
sendActionsForControlEvents(UIControlEvents.ValueChanged) | |
} | |
func loaded(){ | |
transitionToState(.Loaded) | |
} | |
func loadFailed(){ | |
transitionToState(.LoadFailed) | |
} | |
required init(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func scrollViewWillBeginDragging(scrollView: UIScrollView) { | |
NSLog("\(__FUNCTION__) contentSize=\(scrollView.contentSize), frame=\(scrollView.frame) overflowY=\(scrollView.overflowY)") | |
} | |
override func scrollViewDidScroll(scrollView: UIScrollView) { | |
let contentOffset = scrollView.contentOffset | |
let overflowY = scrollView.frame.height + contentOffset.y - scrollView.contentSize.height | |
NSLog("\(__FUNCTION__) contentoffset=\(contentOffset) overflowY=\(overflowY)") | |
if !loadMoreControl.isLoading{ | |
if scrollView.isPulledUp && !loadMoreControl.isCanceled { | |
if !loadMoreControl.isPulled{ | |
loadMoreControl.pulled() | |
NSLog("startPull") | |
} | |
// overflowY 有两次达到此状态,一些上上拉,一次是加弹,所以需要判断一下 | |
}else if scrollView.isPullingUp{ | |
if !loadMoreControl.isPulling{ | |
NSLog("startPull") | |
loadMoreControl.startPull() | |
} | |
} | |
} | |
} | |
override func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { | |
NSLog("\(__FUNCTION__) velocity=\(velocity) targetContentOffset=\(targetContentOffset.memory)") | |
} | |
override func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) { | |
NSLog("\(__FUNCTION__) willDecelerate?=\(decelerate)") | |
if !loadMoreControl.isLoading{ | |
if scrollView.isPulledUp{ | |
NSLog("startLoad") | |
loadMoreControl.startLoad() | |
}else{ | |
NSLog("canceled") | |
loadMoreControl.canceled() | |
} | |
} | |
} | |
override func scrollViewWillBeginDecelerating(scrollView: UIScrollView) { | |
NSLog("\(__FUNCTION__)") | |
} | |
override func scrollViewDidEndDecelerating(scrollView: UIScrollView) { | |
NSLog("\(__FUNCTION__) offset=\(scrollView.contentOffset)") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment