UIScrollView 和 UIPageControl,我做错了啥?

Posted

技术标签:

【中文标题】UIScrollView 和 UIPageControl,我做错了啥?【英文标题】:UIScrollView and UIPageControl, what am I doing wrong?UIScrollView 和 UIPageControl,我做错了什么? 【发布时间】:2012-11-23 15:31:36 【问题描述】:

我有一个预定义一些标签并将它们的值绑定到 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

请看一下!

【问题讨论】:

【参考方案1】:

尝试实现此方法可能会对您有所帮助:

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

【讨论】:

感谢您抽出宝贵时间回答,但我认为这不是解决方案。 (我试过了,但不是)。我想我可能忽略了与框架相关的一些东西,但无论是标签放置不正确还是框架不够大(后者在逻辑上是不可能的,因为它被初始化为滚动视图宽度的两倍,但是你永远不知道)。干杯

以上是关于UIScrollView 和 UIPageControl,我做错了啥?的主要内容,如果未能解决你的问题,请参考以下文章

UIScrollView

UIScrollView 内 UIScrollView 内 UIScrollView

ios: uiscrollview 和 uiview 自动布局

如何使在Swift中使用UIScrollView进行滚动和缩放

UICollectionViewCell 和 UIScrollView

UIScrollView和UIPageControl结合使用