我创建了一个时间轴视图,以便在一天的不同时间显示某些事件。
例如,我有一个包含以下事件的数组:
@[A,B,C,D,E,F,G,H]
这些事件在不同的时间开始。
A在上午9点,B在上午9:30,C在上午10点等...
根据时间,我计算Y值。
以下是向时间线视图添加事件的方法:
-(void)addEvents{
NSArray *tempArr = //dataarray;
__block NSInteger day = idx;
_dayeventViewArray = [NSMutableArray array];
[tempArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//If there are two events at the same time, then they should share the width equally
int eventX = [self getEventXForDay://];
int eventY = [self getEventYFor://];
float timeLineWdith = 60;
//Add TitleLabel
UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(eventX, eventY, timeLineWdith, eventViewHeight)];
[self addSubview:titleLab];
[_dayeventViewArray addObject:titleLab];
}];
[self reorderConflictingViews];
}
在添加事件后,我尝试调整其框架,以便事件不会重叠,用户可以查看所有事件。
-(void)reorderConflictingViews{
float timeLineWdith = 60;
NSMutableArray *yArray = [NSMutableArray array];
NSArray *dayArr = //dataarray;
__block int labelY = 0;
[dayArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UILabel *conflictView = (UILabel *)obj;
//if array contains 5 events, share the space equally among 5 events, so divide timeline width by 5
int conflictViewWidth = timeLineWdith/dayArr.count;
int originalX = conflictView.frame.origin.x;
int newX = (int)(originalX + idx*conflictViewWidth);
NSLog(@"orig X: %d, new X:%d",originalX,newX);
conflictView.frame = CGRectMake(newX, conflictView.frame.origin.y, conflictViewWidth, conflictView.frame.size.height);
}];
}
结果是:
左视图是我得到的,但我希望获得正确的结果。