ITPullToRefreshScrollView
是NSScrollView
的子类,允许通过下拉进行类似于iOS 7风格的刷新。ITPullToRefreshScrollView
是为Play by Play应用程序创建的,由David Keegan提供资金。
ITPullToRefreshScrollView
有两个子模块。您需要复制以下文件:
位于/ITPullToRefreshScrollView/Classes/下的文件:
ITPullToRefreshScrollView.h
ITPullToRefreshScrollView.m
ITPullToRefreshClipView.h
ITPullToRefreshClipView.m
ITPullToRefreshEdgeView.h
ITPullToRefreshEdgeView.m
DuxScrollViewAnimation.h
DuxScrollViewAnimation.m
位于/Modules/ITProgressIndicator/ITProgressIndicator/Classes/下的文件:
ITProgressIndicator.h
ITProgressIndicator.m
NSBezierPath+Geometry.h
NSBezierPath+Geometry.m
位于/Modules/NSBKeyframeAnimation/NSBKeyframeAnimation/Classes/NSBKeyframeAnimation/下的文件:
NSBKeyframeAnimation.h
NSBKeyframeAnimation.m
NSBKeyframeAnimationFunctions.h
NSBKeyframeAnimationFunctions.c
请确保将它们复制到项目中,并将其添加到目标。
请确保查看示例项目。
在Interface Builder中设置滚动视图的custom class为ITPullToRefreshScrollView
。接下来您需要将一个IBOutlet
连接到滚动视图。
@property (assign) IBOutlet ITPullToRefreshScrollView *scrollView;
然后您可以在代码中配置,哪个滚动视图的边缘可以进行刷新。
// Make top & bottom refreshable
self.scrollView.refreshableEdges = ITPullToRefreshEdgeTop | ITPullToRefreshEdgeBottom;
要接收来自滚动视图的通知,您需要创建一个代理。为此,您的代理类必须实现ITPullToRefreshScrollViewDelegate
协议。然后您可以实现以下方法。
/**
* This method get's invoked when the scroll view started refreshing
*
* @param scrollView - The scroll view that started refreshing
* @param edge - The edge that started refreshing
*/
- (void)pullToRefreshView:(ITPullToRefreshScrollView *)scrollView
didStartRefreshingEdge:(ITPullToRefreshEdge)edge {
// Do stuff that takes very long
// Don't forget to call this line!
[scrollView stopRefreshingEdge:edge];
}
/**
* This method get's invoked when the scroll view stopped refreshing
*
* @param scrollView - The scroll view that stopped refreshing
* @param edge - The edge that stopped refreshing
*/
- (void)pullToRefreshView:(ITPullToRefreshScrollView *)scrollView
didStopRefreshingEdge:(ITPullToRefreshEdge)edge {
// Do UI stuff
}
最后,您必须设置滚动视图的代理以接收通知。
self.scrollView.delegate = self;
要创建自定义ITPullToRefreshEdgeView
子类,覆写以下方法
// --------------------------
// ------------------- Events
// --------------------------
/**
* This method is called when the edge is triggered
*
* @param scrollView - The sender scroll view
*/
- (void)pullToRefreshScrollViewDidTriggerRefresh:(ITPullToRefreshScrollView *)scrollView;
/**
* This method is called when the edge is untriggered
*
* @param scrollView - The sender scroll view
*/
- (void)pullToRefreshScrollViewDidUntriggerRefresh:(ITPullToRefreshScrollView *)scrollView;
/**
* This method is called when the edge starts refreshing
*
* @param scrollView - The sender scroll view
*/
- (void)pullToRefreshScrollViewDidStartRefreshing:(ITPullToRefreshScrollView *)scrollView;
/**
* This method is called when the edge stops refreshing
*
* @param scrollView - The sender scroll view
*/
- (void)pullToRefreshScrollViewDidStopRefreshing:(ITPullToRefreshScrollView *)scrollView;
/**
* This method is called when part of the edge view becomes visible
*
* @param scrollView - The sender scroll view
* @param progress - The amount of the edge view that is visible (from 0.0 to 1.0)
*/
- (void)pullToRefreshScrollView:(ITPullToRefreshScrollView *)scrollView didScrollWithProgress:(CGFloat)progress;
// --------------------------
// ------------ Customisation
// --------------------------
/**
* Override this to remove the progress indicator and create other components
*/
- (void)installComponents;
/**
* The height of the edge view.
* Override this method to achieve a custom edge view height.
*
* @return The height of the edge view
*/
- (CGFloat)edgeViewHeight;
/**
* Override this method to draw a custom background.
* You can also just override `drawRect:` and to all the drawing on your own.
*
* @param The rect which should be drawn on
*/
- (void)drawBackgroundInRect:(NSRect)dirtyRect;