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 小时前
iOS风格计算器 - 鸿蒙PC Electron框架上的技术实现详解
游戏·ios·华为·electron·harmonyos·鸿蒙
人月神话Lee7 小时前
【图像处理】一文带你窥探近期火热图像App的主要实现原理:主色提取——从图像到调色板
ios·ai编程·图像识别
2601_955767421 天前
iPhone 17 全系屏幕光学特性与保护膜选型技术分析
ios·ar·iphone·圆偏振光护眼·iphone17护眼钢化膜·#观复盾护景贴
2501_915909061 天前
深入解析Mock.js:功能、应用及实战案例,提升前端开发效率
android·ios·小程序·https·uni-app·iphone·webview
pop_xiaoli1 天前
【iOS】通知传值实现
macos·ios·xcode
2601_955767421 天前
iPhone 17 屏幕偏振光学解析与保护贴技术选择——悟赫德观复盾护景贴
人工智能·科技·ios·ar·iphone·圆偏振光
qq3621967051 天前
手机App下载安装完全指南:2026最新教程(Android & iOS)
android·ios·智能手机
核电机组1 天前
IOS原生APP集成Flutter
flutter·ios