iOS 实现悬浮跟手滚动效果

思路: 透视图不是放在tableView上面,而是放在控制器的view面,当tablView滚动的时候

头视图就跟着tablView滚动,(通过记录上次偏移量和当前偏移量来实现, tableView滚动多少,头视图就滚动多少),然后限制一下头视图的两个边界位置即可,通过MIN和MAX实现,同时,为了实现性能的优化,在超出临界值之后,就不再设置frame

复制代码
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y > self.lastOffset && CGRectGetMinY(self.headerView.frame) <= 50) {
        self.lastOffset = scrollView.contentOffset.y;
        return;
    }
    if (scrollView.contentOffset.y < self.lastOffset && CGRectGetMinY(self.headerView.frame) >= 100) {
        self.lastOffset = scrollView.contentOffset.y;
        return;
    }
    CGRect rect = self.headerView.frame;
    CGPoint orign = rect.origin;
    orign.y -=(scrollView.contentOffset.y - self.lastOffset);
    orign.y = MIN(100, MAX(50, orign.y));
    rect.origin = orign;
    self.headerView.frame = rect;
    self.lastOffset = scrollView.contentOffset.y;
}
相关推荐
编程范式5 小时前
SwiftUI 中图片如何适配可用空间
ios
songgeb2 天前
启发式 UI 自动化:从线性剧本到每步读屏决策
ios·测试
壹方秘境6 天前
我用Go语言开发了一个跨平台的HTTPS抓包和调试工具
前端·后端·ios
初级代码游戏11 天前
easy Photo Clean公测版:快速清理iPhone照片 邀请公测
ios·iphone
库奇噜啦呼11 天前
【iOS】RunLoop学习
学习·ios
黑科技iOS上架11 天前
iOS应用周末提交什么情况算卡审
经验分享·ios
zzb158011 天前
ios基础-MVC-UIView
ios·mvc·cocoa
kingbal11 天前
Flutter:Flutter SDK版本管理工具FVM
android·flutter·ios·android-studio·window
他们都不看好你,偏偏你最不争气12 天前
【iOS】Runtime - Part 2 && 消息发送:缓存、查找与转发
macos·ios·objective-c·cocoa