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 HqPullZoomView : UIView

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

@end

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 HqPullZoomView ()

    @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];
    

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容