IOS ScrollView

发布时间:2019-06-14 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了IOS ScrollView脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

scroll View很重要 app当中随处可见,分页,tableview 图片放大等

frame bounds区别

都是表示位置和大小
frame 在父View 坐标系中,bounds在自己的坐标系中

ContentSize Contentoffset

ContentSize :scrollview 初始化时候要设置 需要滚动视图的大小

Contentoffset 是滚动之后位置偏移量 跟scroll view bounds保持一致

Demo

@interface ScrollVC() <UIScrollViewDelegate>
@property (strong ,nonatomic) UIScrollView * scrollView;
@property (strong ,nonatomic) UIImageView * imageView;
@end

@implementation ScrollVC
- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bigPic"]];
    self.scrollView=[[UIScrollView alloc]initWithFrame:self.view.bounds];

    self.scrollView.backgroundColor=[UIColor blackColor];
    self.scrollView.contentSize=self.imageView.bounds.size;

    [self.view addSubview:_scrollView];
    [self.scrollView addSubview:_imageView];
    self.scrollView.delegate=self;




    [self setScale];
}


// 设置 scroll
-(void)setScale{
    CGSize boundSize=self.scrollView.bounds.size;
    CGSize imageSize=self.imageView.bounds.size;

    float xScale=boundSize.width/imageSize.width;
    float yScale=boundSize.height/imageSize.height;
   //计算 适合屏幕 宽度 或者 高度的比例大小 fit 模式
    float zoomScale=MIN(xScale, yScale);
    //_scrollView.frame=self.view.bounds;
    _scrollView.minimumZoomScale=zoomScale;
    //开始 大小比例
    _scrollView.zoomScale=zoomScale  ;
    _scrollView.maximumZoomScale=3.0;

    //CGSize boundSize=self.scrollView.bounds.size;
    CGRect frameToCenter=_imageView.frame;
    //设置 imageView 被scroll的这个视图 居中屏幕
    if(frameToCenter.size.width<boundSize.width){
        frameToCenter.origin.x=(boundSize.width-frameToCenter.size.width  )/2;
    }
    if (frameToCenter.size.height<boundSize.height) {
        frameToCenter.origin.y=(boundSize.height-frameToCenter.size.height  )/2;

    }
    _imageView.frame=frameToCenter;

}

#pragma UIScrollViewDelegate

//设置那个view 需要 放大缩小
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return _imageView;
}

//屏幕 横向 变化 或者 view 大小变化
-(void)viewWillLayoutSubviews{

     [self setScale];
}

ContentInset使用

当scroll view 被navigationbar 挡住时候 往下移位设置

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0,CGRectGetHeight(_myMsgInputView.frame), 0.0);
    self.scrollView.contentInset = contentInsets;
contentInset实际上是内容的padding 比如tableview 底部如果不设padding 就会被tabbar 挡住

UIEdgeInsetsMake顺序为 上 左 下 右

scrollview 键盘显示内容遮挡问题

TPKeyboardAvoiding 这个包 封装 键盘 show hide 动态修改scroll 的 contestinset来解决问题。
https://github.com/michaeltyson/TPKeyboardAvoiding

脚本宝典总结

以上是脚本宝典为你收集整理的IOS ScrollView全部内容,希望文章能够帮你解决IOS ScrollView所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。