请大侠指教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];
    
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值