IOS仿网易云音乐账号界面滑动效果

#define topHeight 100 //红色部分的高度
#define topSpace  47 //会员卡片露出来部分的高度
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIImageView *cardImageView;
@property (nonatomic) CGFloat distance;//可以向下滑动的距离
@property (nonatomic) CGFloat normalY;//正常状态下的纵坐标

UI部分

 UIScrollView *scrollView=[[UIScrollView alloc] init];
    scrollView.frame=CGRectMake(0,64,WinSize.width,WinSize.height-64);
    scrollView.contentInset=UIEdgeInsetsZero;
    scrollView.delegate=self;
    scrollView.showsVerticalScrollIndicator=NO;
    scrollView.showsHorizontalScrollIndicator=NO;
    scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [self.view addSubview:scrollView];
    UIView *contentView=[[UIView alloc] initWithFrame:CGRectMake(0,0,WinSize.width,WinSize.height+200)];
    [scrollView addSubview:contentView];
    contentView.backgroundColor=[UIColor blueColor];
    scrollView.contentSize=contentView.frame.size;
    UIView *topView=[[UIView alloc] init];
    [contentView addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(contentView);
        make.height.mas_equalTo(topHeight);
    }];
    topView.backgroundColor=[UIColor redColor];
    UIImageView *headImageView=[[UIImageView alloc] init];
    [topView addSubview:headImageView];
    [headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60*kScreenWidthRatio,60*kScreenWidthRatio));
        make.left.top.mas_equalTo(20*kScreenWidthRatio);
    }];
    headImageView.layer.masksToBounds=YES;
    headImageView.layer.cornerRadius=30*kScreenWidthRatio;
    headImageView.backgroundColor=[UIColor blackColor];
    self.topView=topView;
    UIImageView *cardImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mine_buy_vip"]];//这里是会员卡图片
    [contentView addSubview:cardImageView];
    [cardImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(cardImageView.superview);
        make.size.mas_equalTo(CGSizeMake(cardImageView.image.size.width*kScreenWidthRatio, cardImageView.image.size.height*kScreenWidthRatio));
        make.top.equalTo(topView.mas_bottom);
    }];
    self.cardImageView=cardImageView;
    UIView *bottomView=[[UIView alloc] init];
    bottomView.backgroundColor=[UIColor yellowColor];
    [contentView addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(contentView);
        make.top.equalTo(cardImageView).offset(topSpace);
    }];
    [scrollView layoutIfNeeded];
    self.normalY=topView.bottom;    
    self.distance=(cardImageView.height-topSpace)*2;
//开始的小动画
        [scrollView setContentOffset:CGPointMake(0,-self.distance)];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [scrollView setContentOffset:CGPointZero animated:YES];    
        });

滑动代理部分关键代码

#pragma mark - scrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"%f",scrollView.contentOffset.y);
    CGFloat contentOffY=scrollView.contentOffset.y;
    if(contentOffY<0&&fabs(contentOffY)>self.distance)
    {
        [scrollView setContentOffset:CGPointMake(0,-self.distance)];
    }
    if(contentOffY<=0&&fabs(contentOffY)<=self.distance)
    {
     //这里仿网易云,滑动过程会有稍微的间距变化,所以设置成0.55
        self.topView.y=contentOffY*0.55f;
        self.cardImageView.y=self.normalY+contentOffY*0.5f;
    } 
}

最后效果图


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

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 15,705评论 4 61
  • 这是我的第一篇博客,做Android 开发2年多以来,曾经也有过写一些技术博客的想法,但因为有时候因为懒,有时候工...
    依然范特稀西阅读 6,202评论 8 48
  • 小的时候,包饺子是我们家过年的“保留节目”。今天又从外甥女那里得来一个新词汇:“交际工具”。确实,很久以前每逢过年...
    冷悲秋阅读 868评论 0 50
  • 秋天来了。还没来得及很好地品一品它的味道,它又走了。 秋天来了。还没来得及看一看它的美景,它又走了。 秋天来了。还...
    小石潭记录阅读 551评论 6 6
  • 2018年2月5日 星期一 晴 今天事情多,早上一早先送儿子去上象棋课,没时间陪他跑步了,中午也...
    00e766263c1b阅读 335评论 0 1

友情链接更多精彩内容