1. 程式人生 > >【iOS設計模式】---- 介面卡模式,觀察者模式

【iOS設計模式】---- 介面卡模式,觀察者模式

- (void)reload  
  
{  
  
    // 1 - nothing to load if there's no delegate  
  
    if (self.delegate == nil) return;  
  
   
  
    // 2 - remove all subviews  
  
    [scroller.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {  
  
        [obj removeFromSuperview];  
  
    }];  
  
   
  
    // 3 - xValue is the starting point of the views inside the scroller  
  
    CGFloat xValue = VIEWS_OFFSET;  
  
    for (int i=0; i<[self.delegate numberOfViewsForHorizontalScroller:self]; i++)  
  
    {  
  
        // 4 - add a view at the right position  
  
        xValue += VIEW_PADDING;  
  
        UIView *view = [self.delegate horizontalScroller:self viewAtIndex:i];  
  
        view.frame = CGRectMake(xValue, VIEW_PADDING, VIEW_DIMENSIONS, VIEW_DIMENSIONS);  
  
        [scroller addSubview:view];  
  
        xValue += VIEW_DIMENSIONS+VIEW_PADDING;  
  
    }  
  
   
  
    // 5  
  
    [scroller setContentSize:CGSizeMake(xValue+VIEWS_OFFSET, self.frame.size.height)];  
  
   
  
    // 6 - if an initial view is defined, center the scroller on it  
  
    if ([self.delegate respondsToSelector:@selector(initialViewIndexForHorizontalScroller:)])  
  
    {  
  
        int initialView = [self.delegate initialViewIndexForHorizontalScroller:self];  
  
        [scroller setContentOffset:CGPointMake(initialView*(VIEW_DIMENSIONS+(2*VIEW_PADDING)), 0) animated:YES];  
  
    }  
  
}