iOS-下拉放大视图

效果图

123.gif

HqPullZoomView.h

//
//  HqPullZoomView.h
//  OC-Use
//
//  Created by macpro on 2017/6/19.
//  Copyright © 2017年 macpro. All rights reserved.
//

#import <UIKit/UIKit.h>

[@interface](https://my.oschina.net/u/996807) HqPullZoomView : UIView

- (void)showInView:(UIScrollView *)scrollView;

[@end](https://my.oschina.net/u/567204)

HqPullZoomView.m

    //
    //  HqPullZoomView.m
    //  OC-Use
    //
    //  Created by macpro on 2017/6/19.
    //  Copyright © 2017年 macpro. All rights reserved.
    //

    #import "HqPullZoomView.h"

    #define HqKeyPathContentOffset @"contentOffset"

    [@interface](https://my.oschina.net/u/996807) HqPullZoomView ()

    [@property](https://my.oschina.net/property) (nonatomic,strong) UIScrollView *scrollView;

    @end

    @implementation HqPullZoomView

    - (void)dealloc{
        [self removeObservers];
    }

    - (void)showInView:(UIScrollView *)scrollView{
        [self removeFromSuperview];
        _scrollView = scrollView;
        if (_scrollView) {
        
            [_scrollView insertSubview:self atIndex:0];

            _scrollView.contentInset = UIEdgeInsetsMake(self.frame.size.height, 0, 0, 0);
            NSLog(@"startOffetY = %f",_scrollView.contentOffset.y);

            [self addObservers];

            CGPoint offset = _scrollView.contentOffset;
            
            if ([scrollView isKindOfClass:[UIScrollView class]]) {
                if (offset.y==0) {
                    offset = CGPointMake(0, -scrollView.contentInset.top);
                    _scrollView.contentOffset = offset;
                }
            }

        }
    }

    #pragma mark - KVO监听
    - (void)addObservers
    {
        NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
        [self.scrollView addObserver:self forKeyPath:HqKeyPathContentOffset options:options context:nil];

    }

    - (void)removeObservers
    {
        [self.scrollView removeObserver:self forKeyPath:HqKeyPathContentOffset];
    }
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
        if ([keyPath isEqualToString:HqKeyPathContentOffset]) {
            NSValue *offset = (NSValue *)(change[NSKeyValueChangeNewKey]);
            CGFloat offsetY = offset.CGPointValue.y;
            
            NSLog(@"offsetY = %f",offsetY);
            if (offsetY<0) {
                
                CGRect rect = self.frame;
                if (-offsetY<=0) {
                    return;
                }
                rect.origin.y = offsetY;
                rect.size.height = -offsetY;
                self.frame = rect;
            }
        }
    }
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code
    }
    */

    @end

使用姿势

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.tableView];
    
    HqPullZoomView *pullZommView = [[HqPullZoomView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 250)];
    pullZommView.backgroundColor = [UIColor purpleColor];
    [pullZommView showInView:self.tableView];
    

}

转载于:https://my.oschina.net/hehuiqi/blog/1590308

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值