下拉刷新对象RefreshObject
下拉刷新对象RefreshObject
效果
说明
1. 分离了动画实现与刷新逻辑
2. 你可以根据自己的需要,设计自己的动画效果,你的动画只需要继承协议,实现协议里面的方法即可
3. 本设计方案是用的组件方式,代码复用率很高,灵活性很强
源码
https://github.com/YouXianMing/RefreshObject
- //
- // RefreshObjectAnimationProtocal.h
- // TableViewRefresh
- //
- // Created by YouXianMing on 15/6/25.
- // Copyright (c) 2015年 YouXianMing. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @protocol RefreshObjectAnimationProtocal <NSObject>
- @required
- - (void)animationWithPercent:(CGFloat)percent;
- - (void)startRefreshAnimation;
- - (void)endRefreshAnimation;
- @end
- //
- // RefreshObject.h
- // UIScrollView
- //
- // Created by YouXianMing on 15/6/24.
- // Copyright (c) 2015年 YouXianMing. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import <UIKit/UIKit.h>
- @class RefreshObject;
- typedef enum : NSUInteger {
- NORMAL_STATE, // 正常状态
- REFRESH_STATE, // 刷新状态
- } ERefreshState;
- @protocol RefreshObjectDelegate <NSObject>
- @required
- /**
- * 开始刷新
- *
- * @param refreshObject
- */
- - (void)startRefreshing:(RefreshObject *)refreshObject;
- /**
- * 结束刷新
- *
- * @param refreshObject
- */
- - (void)endRefresh:(RefreshObject *)refreshObject;
- - (void)moving:(RefreshObject *)refreshObject offset:(CGFloat)offset percent:(CGFloat)percent;
- @end
- @interface RefreshObject : NSObject
- /**
- * 代理
- */
- @property (nonatomic, weak) id <RefreshObjectDelegate> delegate;
- /**
- * 当前状态
- */
- @property (nonatomic, readonly) ERefreshState state;
- @property (nonatomic) CGFloat height;
- @property (nonatomic, weak) UIScrollView *scrollView;
- /**
- * === 子类可以重写该方法实现新的刷新效果 ===
- *
- * 开始刷新
- */
- - (void)beginRefreshing;
- /**
- * === 子类可以重写该方法实现新的刷新效果 ===
- *
- * 结束刷新
- */
- - (void)endRefresh;
- @end
- //
- // RefreshObject.m
- // UIScrollView
- //
- // Created by YouXianMing on 15/6/24.
- // Copyright (c) 2015年 YouXianMing. All rights reserved.
- //
- #import "RefreshObject.h"
- @interface RefreshObject ()
- @end
- @implementation RefreshObject
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
- // 当前位置
- float currentPostion = _scrollView.contentOffset.y;
- if (_scrollView.isDragging) {
- // 拖拽中
- if (_state == NORMAL_STATE) {
- // 获取位移的信息
- if (_delegate) {
- CGFloat percent = ;
- if (currentPostion <= ) {
- percent = -currentPostion / _height;
- }
- [_delegate moving:self offset:currentPostion percent:percent];
- }
- }
- } else {
- // 停止拖拽
- if (currentPostion < -_height) {
- [self beginRefreshing];
- }
- }
- }
- - (void)beginRefreshing {
- if (_state == NORMAL_STATE) {
- _state = REFRESH_STATE;
- if (_delegate) {
- [_delegate startRefreshing:self];
- }
- [UIView animateWithDuration:0.3 animations:^{
- _scrollView.contentInset = UIEdgeInsetsMake(_height, , , );
- } completion:^(BOOL finished) {
- }];
- }
- }
- - (void)endRefresh {
- if (_delegate) {
- [_delegate endRefresh:self];
- }
- [UIView animateWithDuration:0.3f animations:^{
- _scrollView.contentInset = UIEdgeInsetsMake(, , , );
- } completion:^(BOOL finished) {
- _state = NORMAL_STATE;
- }];
- }
- @end
- //
- // UIScrollView+RefreshObject.h
- // UIScrollView
- //
- // Created by YouXianMing on 15/6/24.
- // Copyright (c) 2015年 YouXianMing. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #import "RefreshObject.h"
- @interface UIScrollView (RefreshObject)
- /**
- * 需要主动赋值
- */
- @property (nonatomic, strong) RefreshObject *refreshObject;
- /**
- * 添加观察者
- */
- - (void)addObserver;
- /**
- * 移除观察者
- */
- - (void)removeObserver;
- @end
- //
- // UIScrollView+RefreshObject.m
- // UIScrollView
- //
- // Created by YouXianMing on 15/6/24.
- // Copyright (c) 2015年 YouXianMing. All rights reserved.
- //
- #import "UIScrollView+RefreshObject.h"
- #import <objc/runtime.h>
- @implementation UIScrollView (RefreshObject)
- #pragma mark - 添加属性
- @dynamic refreshObject;
- NSString * const _recognizerRefreshObject = @"recognizerRefreshObject";
- - (void)setRefreshObject:(RefreshObject *)refreshObject {
- objc_setAssociatedObject(self, (__bridge const void *)(_recognizerRefreshObject), refreshObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- - (RefreshObject *)refreshObject {
- return objc_getAssociatedObject(self, (__bridge const void *)(_recognizerRefreshObject));
- }
- #pragma mark -
- - (void)addObserver {
- if (self.refreshObject && self.refreshObject.scrollView == nil) {
- // 获取scrollView
- self.refreshObject.scrollView = self;
- // 添加监听
- [self addObserver:self.refreshObject
- forKeyPath:@"contentOffset"
- options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
- context:nil];
- }
- }
- - (void)removeObserver {
- if (self.refreshObject) {
- // 移除监听
- [self removeObserver:self.refreshObject
- forKeyPath:@"contentOffset"];
- self.refreshObject.scrollView = nil;
- self.refreshObject = nil;
- }
- }
- @end
细节
继承协议 RefreshObjectAnimationProtocal 并实现协议方法即可,使用的话,如下所示
下拉刷新对象RefreshObject的更多相关文章
- C#构造方法(函数) C#方法重载 C#字段和属性 MUI实现上拉加载和下拉刷新 SVN常用功能介绍(二) SVN常用功能介绍(一) ASP.NET常用内置对象之——Server sql server——子查询 C#接口 字符串的本质 AJAX原生JavaScript写法
C#构造方法(函数) 一.概括 1.通常创建一个对象的方法如图: 通过 Student tom = new Student(); 创建tom对象,这种创建实例的形式被称为构造方法. 简述:用来初 ...
- IOS第四天-新浪微博 -存储优化OAuth授权账号信息,下拉刷新,字典转模型
*************application - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOpti ...
- react + iscroll5 实现完美 下拉刷新,上拉加载
经过几天的反复折腾,总算做出一个体验还不错的列表页了,主要支持了下拉刷新,上拉加载两个功能. 一开始直接采用了react-iscroll插件,它是基于iscroll插件开发的组件.但是开发过程中,发现 ...
- Windows phone应用开发[22]-再谈下拉刷新
几周之前在博客更新一篇Windows phone应用开发[18]-下拉刷新 博文,有很多人在微博和博客评论中提到了很多问题.其实在实际项目中我基于这篇博文提出解决问题思路优化了这个解决方案.为了能够详 ...
- Windows phone应用开发[18]-下拉刷新
在windows phone 中采用数据列表时为了保证用户体验常遇到加载数据的问题.这个问题普遍到只要你用到数据列表就要早晚面对这个问题. 很多人会说这个问题已经有解决方案. 其实真正问题并不在于如何 ...
- 【转载】 Android PullToRefresh (ListView GridView 下拉刷新) 使用详解
Android下拉刷新pullToRefreshListViewGridView 转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/3 ...
- Android开发学习之路-下拉刷新以及GridView的使用
GridView是类似于ListView的控件,只是GridView可以使用多个列来呈现内容,而ListView是以行为单位,所以用法上是差不多的. 主布局文件,因为要做下拉刷新,所以加了一个Prog ...
- [Android]下拉刷新控件RefreshableView的实现
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4172483.html 需求:自定义一个ViewGroup,实现 ...
- Android PullToRefresh (GridView 下拉刷新上拉加载)
做这个需要自己去git hub上下载个pull-to-refresh 里面有个library为依赖包自己导到自己的项目中 (下载地址:https://github.com/chrisbanes/And ...
随机推荐
- nginx 超时问题: upstream timed out (110: Connection timed out) while reading response header from upstream
目录 错误内容 错误原因 错误解决办法 错误内容 我们可以在error.log 里面可以看到 错误内容:upstream timed out (110: Connection timed out) w ...
- spark job运行参数优化
http://www.cnblogs.com/LBSer/p/4129481.html 一.问题 使用spark join两张表(5000w*500w)总是出错,报的异常显示是在shuffle阶段. ...
- 玩转mongodb(四):细说插入、更新、删除和查询
插入: 使用insert或save方法想目标集合插入一个文档: db.person.insert({"name":"ryan","age": ...
- PTA (Advanced Level) 1021 Deepest Root
Deepest Root A graph which is connected and acyclic can be considered a tree. The hight of the tree ...
- import.html 页面导出execl
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- double类型转换为int类型四舍五入工具类
package com.qiyuan.util; import java.math.BigDecimal; import java.text.DecimalFormat; public class G ...
- oracle存储过程删除树状结构的表数据
今天在删除一个车辆品牌表的时候,遇到了一个问题,是在java的代码中做逻辑删除还是直接在Oracle中一次删除完成呢 思来想去觉得还是在sql里直接删除比较合适, 为什么呢? 第一,涉及数据库的读写操 ...
- [转]微信小程序之购物车功能
本文转自:https://www.cnblogs.com/linxin/p/6834206.html 前言 以往的购物车,基本都是通过大量的 DOM 操作来实现.微信小程序其实跟 vue.js 的用法 ...
- vb.net的String类型和Bytes转换(C#也适用)
1.Bytes---->StringSystem.Text.Encoding.Unicode.GetString(bytes, 0, bytes.Length) 2.String----> ...
- EL表达式无法获取Spring MVC的Model封装好的数据解决方法
1.在spring-mvc的配置文件中已经配置jsp的视图解析器 2.在Controller中使用Model的addAttribute方法添加属性name,msg 3.在jsp页面中使用${msg}取 ...