更新 我有一个在 UIView
上绘制的网格 (SubViewGrid
)。我需要能够防止最左列(时间)水平滚动,以及顶行(员工姓名)垂直滚动。
数据在 SubViewData
中绘制,该数据位于 UIScrollView
内。我想这里需要两 (2) 个 UIScrollView
,一个用于 SubViewGrid
,另一个用于 SubViewData
。问题是:如何同步它们来完成用户的需求?
更新这是修订后的结构:
@property (nonatomic, weak) IBOutlet UIScrollView *schedScrollView;
@property (nonatomic, weak) IBOutlet UIView * topGridView;
@property (nonatomic, weak) IBOutlet UIView * leftGridView;
@property (nonatomic, weak) IBOutlet UIView * topLeftView; // TODO
最佳答案
参见 this answer到 similar question .
我认为这就是您需要解决此问题的方法,将固定标题放在数据的单独 subview 中。然后在 scrollViewDidScroll:
中设置固定 subview 的框架,以在 ScrollView 的顶部(或侧面)创建固定标题的外观。
例如:设置header子view的初始frame为(0, 0, width, height)
,然后在scrollViewDidScroll:
中设置frame为 >(0,contentOffset.y,宽度,高度)
。
编辑:这是一个示例。在下面的屏幕截图中,我在 UIScrollView
内设置了顶行(人员)、左列(时间)和左上角单元格(以隐藏标题的重叠)。然后在 scrollViewDidScroll:
中,我设置 subview 的框架,将它们分别固定在顶部、左侧和左上角。
ViewController.h:
@interface ViewController : UIViewController <UIScrollViewDelegate>
@property (nonatomic, weak) IBOutlet UIScrollView * theScrollView;
@property (nonatomic, weak) IBOutlet UIImageView * topView;
@property (nonatomic, weak) IBOutlet UIImageView * leftView;
@property (nonatomic, weak) IBOutlet UIView * topLeftView;
@end
ViewController.m:
#import "ViewController.h"
@implementation ViewController
@synthesize theScrollView, topView, leftView, topLeftView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.theScrollView.delegate = self;
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.theScrollView.contentSize = CGSizeMake(502, 401);
}
#pragma mark UIScrollViewDelegate methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect tempFrame = self.topView.frame;
tempFrame.origin.y = scrollView.contentOffset.y;
self.topView.frame = tempFrame;
tempFrame = self.leftView.frame;
tempFrame.origin.x = scrollView.contentOffset.x;
self.leftView.frame = tempFrame;
tempFrame = self.topLeftView.frame;
tempFrame.origin.x = scrollView.contentOffset.x;
tempFrame.origin.y = scrollView.contentOffset.y;
self.topLeftView.frame = tempFrame;
}
@end
这就是全部内容了!希望这对您有帮助。
关于ios - 如何控制网格类型 View 的滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17135079/