封装一个下拉一体效果

下拉一体‘.gif

下拉一体效果,是利用tableview的contentInset。把view添加到tableview上,根据scrollview的滚动实现即可。
所以可以封装一个一劳永逸。

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface PullDownZoom : NSObject
-(instancetype)initWithTableview:(UIScrollView *)tableview headerH:(CGFloat)headerH header:(UIView *)header;

-(void)scrollZoom;
@end

传入tableview 与需要下拉的view,以及对应的高度,处理如下。

#import "PullDownZoom.h"
#import "UIView+Frame.h"
@interface PullDownZoom()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *tableview;
@property(nonatomic,assign)CGFloat headerH;
@property(nonatomic,strong)UIView *header;
@end
@implementation PullDownZoom
-(instancetype)initWithTableview:(UIScrollView *)tableview headerH:(CGFloat)headerH header:(UIView *)header
{
    
    self = [super init];
    if (self) {
        self.tableview = tableview;
        
      
        self.tableview.delegate = self;
        self.headerH = headerH;
        self.header = header;
        
        self.tableview.contentInset = UIEdgeInsetsMake(self.headerH, 0, 0, 0);
    }
    return self;
}
-(void)scrollZoom{
    [self scrollViewDidScroll:self.tableview];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat y = scrollView.contentOffset.y;
    
    if ( y  < - self.headerH) {
        self.header.y  = y;
        self.header.height = -y;
        
    }
    
}
@end

所以只需要两步,即可。

1、第一步:引入,初始化

 self.zoom =  [[PullDownZoom alloc]initWithTableview:self.tableview headerH:300 header:self.header];

2、第二步:在scrollview滚动的地方设置

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    [self.zoom scrollZoom];
    
}

demo地址:下拉一体

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,301评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,697评论 25 709
  • 我们谈论起往事 就像是在说别人的故事 不经意间 红了脸 湿了眼
    安小晨阅读 184评论 0 1
  • 最近做了一个项目,加载网页使用的是WKWebView,网页需要同步客户端登录后的cookie。 UIWebView...
    也嘉阅读 1,912评论 0 1
  • 曾经有段时光,总是选择在最深的夜里听木村好夫。 那些日子,每天疯狂地写枯燥的技术文章。写字写累了,便倒...
    我就是七彩熊猫阅读 1,125评论 0 0