2012-03-14 59 views
0

我正在制作日历视图日历,就像本机iPhone日历一样。如果它们的大小和时间相同,我试图将它们放在本地日历中并排放置。iOS:多个相交视图

但是,我只能弄清楚如何做到2瓦,而不是多瓦。在附图中我有4块瓷砖。一个稍微扩展到另一个3.然后,我有第一块瓷砖在最左边,第二块瓷砖在第一块瓷砖旁边。现在我需要弄清楚如何添加额外的瓷砖?

我该怎么做超过2个瓷砖?

关于形象:如果你不能看到它的第三瓷砖是ontop的第2瓦(你可以看到它是有点暗,因为他们是在彼此顶部

screenshot

- (void)layoutSubviews 
{ 
    // Set the main 
    for (UIView *view in self.subviews) { 
     APCalendarDayTile *tile = (APCalendarDayTile *)view; 
     CGFloat startPos = [APCalendarCurrentDayView yAxisForTime:[APCalendarCurrentDayView minutesToTime:tile.appointment.startDate]]; 
     CGFloat endPos = [APCalendarCurrentDayView yAxisForTime:[APCalendarCurrentDayView minutesToTime:tile.appointment.endDate]]; 
     tile.frame = CGRectMake(kLeftSideBuffer, startPos, (self.bounds.size.width - kLeftSideBuffer) , endPos - startPos); 
     tile.backgroundColor = [UIColor colorWithHexString:tile.appointment.appointmentColor]; 
    } 

    for (UIView *view in self.subviews) { 
     APCalendarDayTile *tile = (APCalendarDayTile *)view; 

     if ([self viewIntersectsWithAnotherView:tile]) { 

     } 
    } 
} 

- (BOOL)viewIntersectsWithAnotherView:(UIView*)selectedView{ 
    NSArray *subViewsInView=[self subviews];// I assume self is a subclass 
    // of UIViewController but the view can be 
    //any UIView that'd act as a container 
    //for all other views. 
    for (UIView *theView in subViewsInView){ 
     if (![selectedView isEqual:theView]) { 
      if(CGRectIntersectsRect(selectedView.frame, theView.frame)) { 
       if ((selectedView.frame.origin.y == theView.frame.origin.y) && (selectedView.frame.size.height == theView.frame.size.height)) { 
        if (theView.frame.size.width == self.bounds.size.width - kLeftSideBuffer) { 
         theView.frame = CGRectMake(theView.frame.origin.x, selectedView.frame.origin.y, theView.frame.size.width/2, selectedView.frame.size.height); 
        } 
        selectedView.frame = CGRectMake(theView.frame.origin.x + theView.frame.size.width, selectedView.frame.origin.y, theView.frame.size.width, selectedView.frame.size.height); 
        return YES; 
       } 
      } 
     } 
    } 
    return NO; 
} 

回答

0

好吧,

我八九不离十了SaltyMule的做法然而,他的伪代码并没有在的if/else意义。

- (void)layoutSubviews 
{ 
    // Set the main 
    for (UIView *view in self.subviews) { 
     APCalendarDayTile *tile = (APCalendarDayTile *)view; 
     CGFloat startPos = [APCalendarCurrentDayView yAxisForTime:[APCalendarCurrentDayView minutesToTime:tile.appointment.startDate]]; 
     CGFloat endPos = [APCalendarCurrentDayView yAxisForTime:[APCalendarCurrentDayView minutesToTime:tile.appointment.endDate]]; 
     tile.frame = CGRectMake(kLeftSideBuffer, startPos, (self.bounds.size.width - kLeftSideBuffer) , endPos - startPos); 
     tile.backgroundColor = [UIColor colorWithHexString:tile.appointment.appointmentColor]; 
    } 

    [sameTimeAppointments removeAllObjects]; 

    for (UIView *view in self.subviews) { 
     APCalendarDayTile *tile = (APCalendarDayTile *)view; 

     if ([self viewIntersectsWithAnotherView:tile]) { 
      if ([sameTimeAppointments objectForKey:[NSString stringWithFormat:@"%f", tile.frame.origin.y]] != nil) { 
       NSMutableArray *tempArray = [[sameTimeAppointments objectForKey:[NSString stringWithFormat:@"%f", tile.frame.origin.y]] mutableCopy]; 
       [tempArray addObject:tile]; 
       [sameTimeAppointments setValue:tempArray forKey:[NSString stringWithFormat:@"%f", tile.frame.origin.y]]; 
      } else { 
       [sameTimeAppointments setValue:[NSMutableArray arrayWithObject:tile] forKey:[NSString stringWithFormat:@"%f", tile.frame.origin.y]]; 
      } 
     } 
    } 
    for (NSString *currentDict in sameTimeAppointments) { 
     NSArray *currentAppointments = [sameTimeAppointments objectForKey:currentDict]; 
     float tileWidth = ((self.frame.size.width - kLeftSideBuffer)/[currentAppointments count]); 
     for (int i = 0; i < [currentAppointments count]; i++) { 
      APCalendarDayTile *tile = [currentAppointments objectAtIndex:i]; 
      float xPos = 0.0 + kLeftSideBuffer; 
      if (i != 0) { 
       xPos = (((APCalendarDayTile *)[currentAppointments objectAtIndex:i - 1]).frame.origin.x + tileWidth); 
      } 

      tile.frame = CGRectMake(xPos, tile.frame.origin.y, tileWidth, tile.frame.size.height); 
      [self bringSubviewToFront:tile]; 
     } 
    } 
} 

- (BOOL)viewIntersectsWithAnotherView:(UIView*)selectedView{ 
    NSArray *subViewsInView=[self subviews];// I assume self is a subclass 
    // of UIViewController but the view can be 
    //any UIView that'd act as a container 
    //for all other views. 
    for (UIView *theView in subViewsInView){ 
     if (![selectedView isEqual:theView]) { 
      if(CGRectIntersectsRect(selectedView.frame, theView.frame)) { 
       if ((selectedView.frame.origin.y == theView.frame.origin.y) && (selectedView.frame.size.height == theView.frame.size.height)) { 
        return YES; 
       } 
      } 
     } 
    } 
    return NO; 
} 
1

看来,你的测试

if ((selectedView.frame.origin.y == theView.frame.origin.y) && (selectedView.frame.size.height == theView.frame.size.height)) 

仅适用于相同的看法y原点和高度的我会用下面的伪代码解决这个问题:

initialize an empty arranged subviews array 
initialize a nil previous subview 
for every subview 
    if the subview intersects with the previous subview 
     ensure the subview and the previous subview are added to the arranged subviews array 
    else if the arranged subviews array is not empty 
     arrange the subviews in the array across the width of their superview 
     empty the arranged subview array 
+0

不,我说得对。我只想要同时开始并且长度相同的并排 – Bot 2012-03-14 22:20:21

+0

您的if/else似乎有点混乱。 – Bot 2012-03-15 16:33:57