1. 程式人生 > >請大俠指教uiviewcontroller中添加了scrollview來實現多屏顯示的的問題

請大俠指教uiviewcontroller中添加了scrollview來實現多屏顯示的的問題

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClick)];
        self.navigationItem.leftBarButtonItem = leftButton;
        [leftButton release];
        
        isFreshInitSelf = TRUE;
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            scrollPagesCount = 4;
        }
        else
        {
            scrollPagesCount = 7;
        }
    }
    return self;
}

- (void)viewDidLoad

{
    [super viewDidLoad];
    scrollView.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = [UIColor clearColor];    
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        leftTableView  = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, LEFTMENU_VIEW_WIDTH, CGRectGetHeight(self.view.bounds))];
        leftTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        leftTableView.backgroundColor = [UIColor clearColor];
        leftTableView.rowHeight = 120;
        NSString *imageName = @"bg_left_menunav.png";
        if (CGRectGetWidth(self.view.bounds) > 768) {
            imageName = @"bg_left_menunav_landscape.png";
        }
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, LEFTMENU_VIEW_WIDTH, CGRectGetHeight(self.view.bounds))];    
        imageView.image = [UIImage imageNamed:imageName];
        imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
        [leftTableView addSubview:imageView];
        
        
        [imageView release];
        [self.view addSubview:leftTableView];
    }
 
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (isFreshInitSelf == TRUE) {
        CGRect rect;
        if (scrollPagesCount == 1) {
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
                rect = self.view.frame;
                rect.origin.x += 200;
                rect.size.width -= 200;                        
                scrollView.frame=rect;
            }
        }
            
        [self setupScheduleScrollPage];//分屏顯示,不知是否有問題,請大俠指教~~

        isFreshInitSelf = FALSE;
    
    }else {
        
        [self setScheduleScrollViewSubViewsLayoutForRotation];
        
    }
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}


- (void)viewDidUnload
{
    [scrollView release];
    [leftTableView release];
    [rightTableView release];
    
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
     return [[MiraclePushClientAppDelegate getApp] allowAutoScreen];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    
    [self setScheduleScrollViewSubViewsLayoutForRotation];
    
}


#pragma UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
    if (pageControlIsChangingPage) {
        return;
    }
    
    /*
     *    We switch page at 50% across
     */
    CGFloat pageWidth = _scrollView.frame.size.width;
    
    int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

//由於最左側去掉了一個200寬度的一個空間,scrollview只佔用的568的寬度,可是此處的_scrollView.contentOffset.x 一直是768(實際是否為568)為什麼

??

    currentPageNum = page;
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
    pageControlIsChangingPage = NO;
}



- (void) backButtonClick
{
   
    [MiraclePushClientAppDelegate getApp].rootController.scheduleViewScroller = nil;
    [[self navigationController] popViewControllerAnimated:YES];
    
    //add 09-13
    [[MiraclePushClientAppDelegate getApp].rootController.mainViewScrollerController sendReqToGetMenuInfoData];
}

//分屏顯示以及設定scrollView有問題????

-(void)setupScheduleScrollPage
{
   
    if (scrollView.delegate == nil) {
        
        scrollView.delegate = self;
        
        [scrollView setBackgroundColor:[UIColor clearColor]];
        [scrollView setCanCancelContentTouches:NO];
        
        scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
        scrollView.clipsToBounds = YES;
        scrollView.scrollEnabled = YES;
        scrollView.pagingEnabled = YES;
    }
 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        CGFloat tableViewWidth = self.view.bounds.size.width - 200;
        CGFloat tableViewHeight = self.view.bounds.size.height;
        
        NSInteger cx = 200;
        for (NSInteger i=0; i< scrollPagesCount ; i++) {
            CGRect frame = CGRectMake(cx, 0, tableViewWidth, tableViewHeight);
            UIView *tv = [[UIView alloc] initWithFrame:frame];
           
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
            NSString *str = [[NSString alloc] initWithFormat:@"View%d", i];
            label.text = str;
            [tv addSubview:label];
            [label release];
            
            [scrollView addSubview:tv];
            [tv release];        
            tv = nil;
     
            cx += frame.size.width;
        }
        scrollView.frame = self.view.frame;
       
        [scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
    }
    else
    {
        CGFloat tableViewWidth = self.view.bounds.size.width;
        CGFloat tableViewHeight = self.view.bounds.size.height;
        
        NSInteger cx = 0;
        for (NSInteger i=0; i<scrollPagesCount ; i++) {
            
            CGRect rect = CGRectMake(cx, 0, tableViewWidth, tableViewHeight);
            UIView *tv = [[UIView alloc] initWithFrame:rect];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 100)];
            NSString *str = [[NSString alloc] initWithFormat:@"View%d", i];
            label.text = str;
            [tv addSubview:label];
            [label release];
            
   
            [scrollView addSubview:tv];
            [tv release];        
            tv = nil;
            
            cx += rect.size.width;
        }
        scrollView.frame = self.view.frame;
        [scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
        
    }

}


- (void)setScheduleScrollViewSubViewsLayoutForRotation {
    
    for(UIView *subview in [scrollView subviews]) {
        [subview removeFromSuperview];
    }
        
    [self setupScheduleScrollPage];

    
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * currentPageNum;

    frame.origin.y = 0;
    [scrollView scrollRectToVisible:frame animated:YES];
    
}