簡體   English   中英

iOS:多個相交的視圖

[英]iOS: Multiple intersecting views

我正在制作日視圖日歷,就像本地iPhone日歷一樣。 我正在嘗試並排放置與本機日歷中相同的圖塊,如果它們的大小和時間相同。

但是,我只能弄清楚如何對2個圖塊而不是多個圖塊進行處理。 在所附的圖像中,我有4個圖塊。 一個略微擴展到另一個3。然后,我將第一個圖塊放在最左側,將第二個圖塊緊隨第一個圖塊。 現在,我需要弄清楚如何添加額外的圖塊?

如果超過2個磁貼,我該怎么做?

關於圖像:如果看不到它,則第三個圖塊位於第二個圖塊的頂部(您會看到它有點暗,因為它們彼此重疊。

屏幕截圖

- (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;
}

看來您的測試

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

好,

我排序采用了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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM