2012-03-14 49 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