类似美团详情页scrollview 实现悬浮效果
类似于美团详情页效果,价格那一行不随着底部滚动
当底部滚动时有一部分并未滚动
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UIImageView * topImage;
@property (nonatomic,strong) UIView * redView;
@property (nonatomic,strong) UIView * blueView;
@property (nonatomic,strong) UIScrollView * scrollView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
self.scrollView.delegate = self;
[self.view addSubview:self.scrollView];
_topImage =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 600)];
_topImage.image=[UIImage imageNamed:@"签到_PxCook"];
[self.scrollView addSubview:_topImage];
self.redView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.topImage.frame), self.view.frame.size.width, 30)];
self.redView.backgroundColor = [UIColor redColor];
[self.scrollView addSubview:self.redView];
self.blueView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.redView.frame), self.view.frame.size.width, 800)];
self.blueView.backgroundColor = [UIColor blueColor];
[self.scrollView addSubview:self.blueView];
self.scrollView.contentSize = CGSizeMake(0,CGRectGetMaxY(self.blueView.frame));
[self.view addSubview:self.scrollView];
}
//核心代码
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat imageH = self.topImage.frame.size.height;
CGFloat offsetY = scrollView.contentOffset.y;
if (offsetY >= imageH ) {
CGRect redF = self.redView.frame;
redF.origin.y = 0;
self.redView.frame = redF;
[self.view addSubview:self.redView];
}
else
{
CGRect redF = self.redView.frame;
redF.origin.y = imageH;
self.redView.frame = redF;
[self.scrollView addSubview:self.redView];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end