0

我有一个类,它预定义了一些标签并将它们的值绑定在 UIScrollView 中。

我已经设法显示这些标签,但现在我坚持在 ScrollView 的第二部分放置标签。

我已将我的项目推送到gitHub

我可以在已经可见的部分更改标签的位置,但我必须忽略某些东西。

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = _detail.name;
    UIColor *bgColor = [UIColor blackColor];
    UIColor *txtColor = [UIColor grayColor];
    CGRect frame;
    frame.origin.x = 0;
    frame.origin.y = 0;

    frame.size.width = _scrollView.frame.size.width *2;



    NSString *phoneNr = (_detail.phoneNr == nil) ? @"Not specified" : _detail.phoneNr;

    _telLabel = [self prepareLabel:phoneNr textColor:txtColor bgColor:bgColor page:0 y:telNrYAxis];


    _webLabel = [self prepareLabel:@"Visit website" textColor:txtColor bgColor:bgColor page:0 y:websiteYAxis];

    _detail.address = [_detail.address stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n\t "]];
    NSArray *addressArrComponents = [_detail.address componentsSeparatedByString:@","] ;



    _addressLabel =  [self prepareLabel:[addressArrComponents componentsJoinedByString:@"\n"] textColor:txtColor bgColor:bgColor page:0 y:addressYAxis];


    UILabel *lbl = [self prepareLabel:@"Derp" textColor:txtColor bgColor:bgColor page:1 y:0];


    _detailView = [[UIView alloc] initWithFrame:frame];
    _detailView.backgroundColor = [UIColor blackColor];

    [_detailView addSubview:_webLabel];
    [_detailView addSubview:_addressLabel];
    [_detailView addSubview:_telLabel];
    [_detailView addSubview:lbl];

    [_scrollView addSubview:_detailView];
    NSLog(@"%f",self.view.frame.size.height - (_scrollView.frame.origin.y + _scrollView.frame.size.height) );

    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height - 250 , self.view.frame.size.width/4, 120)];
    _pageControl.numberOfPages=2;
    _pageControl.currentPage=0;


    [_pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventTouchDown];
    _scrollView.contentSize = CGSizeMake(800,800);
    _scrollView.delegate=self;
    _scrollView.backgroundColor = [UIColor blackColor];
    _scrollView.pagingEnabled=YES;

    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.scrollsToTop = NO;
    [self pageChange:0];
    [self.view addSubview:_pageControl];
    // Do any additional setup after loading the view.
}

-(UILabel*)prepareLabel:(NSString*) text textColor:(UIColor*)textColor bgColor:(UIColor*)backgroundColor page:(int)page y:(int) yPos{
    int lines = [[text componentsSeparatedByString:@"\n"] count];

    CGRect labelFrame = CGRectMake(_detailView.frame.size.width * page +20,yPos,self.view.frame.size.width, [UIFont systemFontSize]*lines);

    UILabel *returnLabel = [[UILabel alloc] initWithFrame:labelFrame];
    returnLabel.text = text;
    returnLabel.backgroundColor = backgroundColor;
    returnLabel.textColor = textColor;


    [returnLabel setNumberOfLines:lines];
    [returnLabel sizeToFit];

    return returnLabel;
}
- (void)loadScrollViewWithPage:(int)page {
    NSLog(@"Derped");
}

-(IBAction)pageChange:(id)sender{

    int page=_pageControl.currentPage;
    CGRect frame = _scrollView.frame;
    frame.origin.x = _scrollView.frame.size.width * page;
    frame.origin.y = 0;

    //CGRect frame= (page == 0) ? _detailFrame : _reviewFrame;
    NSLog(@"%f",frame.origin.x);
    [_scrollView scrollRectToVisible:frame animated:YES];
}

代表-(IBAction)pageChange:(id)sender被解雇了,但我一定在某处对框架做错了什么:s

请看一下!

4

1 回答 1

0

Try to implement this method may help you :

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat pageWidth = self.scrollView.frame.size.width; 
    float fractionalPage = self.scrollView.contentOffset.x / pageWidth;
    NSInteger page = lround(fractionalPage);
    self.pageControl.currentPage = page; 
}
于 2012-11-23T16:46:06.433 回答