前言:發(fā)現(xiàn)在很多的app中都會采用到這個(gè)scrollview懸停(想了解的看一看)
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scView;
@property (nonatomic, strong) UIView *topView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:scView];
scView.backgroundColor = [UIColor redColor];
scView.contentSize = CGSizeMake(self.view.frame.size.width, 1000);
//創(chuàng)建一個(gè)button
UIButton *but = [[UIButton alloc]initWithFrame:CGRectMake(100, 400, 100, 100)];
[scView addSubview:but];
but.backgroundColor = [UIColor greenColor];
//創(chuàng)建頂部的條
UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 50)];
topView.backgroundColor = [UIColor greenColor];
[scView addSubview:topView];
self.topView = topView;
self.scView = scView;
self.scView.delegate = self;
}
#pragma scrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (self.scView.contentOffset.y > 100) {
self.topView.frame = CGRectMake(0, 0, self.view.frame.size.width, 50) ;
[self.view addSubview:self.topView];
}else{
self.topView.frame = CGRectMake(0, 100, self.view.frame.size.width, 50) ;
[self.scView addSubview:self.topView];
}
}
@end
基本思路就是代理監(jiān)聽scrollview滑動時(shí)的偏移量向拆,如果偏移量大于懸浮控件到屏幕頂端的距離灭袁,那么就將懸浮控件添加到根控制的view,如果偏移量小于這個(gè)距離時(shí),再把懸浮控件添加到最表面的scrollView,注意:懸浮控件每次更改父控件時(shí)候都需要更改自己的坐標(biāo)。
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者