UIScrollView 滚动到特定页面?

Posted

技术标签:

【中文标题】UIScrollView 滚动到特定页面?【英文标题】:UIScrollView scroll upto particular page? 【发布时间】:2014-04-07 09:52:45 【问题描述】:

我制作了一个 UIScrollView,我正在向它添加子视图。

但是当我水平滚动它时,它会在 scollview 时传递一些对象。

我的代码是

#pragma mark - profileDataParsingFinish
-(void)profileDataParsingFinish:(MainUserDAO *)mainUserDAO


    NSLog(@"Profile Data Parsing Finish ...  ");

    currentDao=mainUserDAO;

    [headerScroll setContentSize:CGSizeMake(3200.0f, 110.0)];
    NSString *newStr = [mainUserDAO.statusThumb substringWithRange:NSMakeRange(1, [mainUserDAO.statusThumb length]-1)];
    NSURL *imageURL=[[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@%@",serverPart,newStr]];

    if([mainUserDAO.CityName isEqualToString:@"(null)"])
    
    else 

    [livesTitle setText:mainUserDAO.CityName];
    [javaTitle setText:mainUserDAO.JobTitle];

    if([currentDao.maritialStatusPrivacyId isEqualToString:@"1"])
        [maritialIcon setHidden:YES];
        [maritialLabel setHidden:YES];

    
    else if([currentDao.maritialStatusPrivacyId isEqualToString:@"2"])
    /*
        <MaritalStatus><MaritalStatusID>1</MaritalStatusID><Title>Single</Title></MaritalStatus><MaritalStatus><MaritalStatusID>2</MaritalStatusID><Title>Engaged</Title></MaritalStatus><MaritalStatus><MaritalStatusID>3</MaritalStatusID><Title>In a relation</Title></MaritalStatus><MaritalStatus><MaritalStatusID>4</MaritalStatusID><Title>Married</Title></MaritalStatus><MaritalStatus><MaritalStatusID>5</MaritalStatusID><Title>Divorced</Title></MaritalStatus><MaritalStatus><MaritalStatusID>6</MaritalStatusID><Title>Widowed</Title></MaritalStatus><MaritalStatus><MaritalStatusID>7</MaritalStatusID><Title>Not interested</Title></MaritalStatus></ArrayOfMaritalStatus>
     */
        if([currentDao.maritialStatusId isEqualToString:@"1"])
            [maritialLabel setText:@"Single"];
        
        else
            if([currentDao.maritialStatusId isEqualToString:@"2"])
                [maritialLabel setText:@"Engaged"];

            
            else
                if([currentDao.maritialStatusId isEqualToString:@"3"])
                  [maritialLabel setText:@"In a relation"]; 
                
                else
                    if([currentDao.maritialStatusId isEqualToString:@"4"])
                           [maritialLabel setText:@"Married"]; 
                    

                    else
                        if([currentDao.maritialStatusId isEqualToString:@"5"])
                            [maritialLabel setText:@"Divorced"];

                        
                        else
                            if([currentDao.maritialStatusId isEqualToString:@"6"])
                                   [maritialLabel setText:@"Widowed"];
                            

                            else
                                if([currentDao.maritialStatusId isEqualToString:@"7"])
                                    [maritialLabel setText:@"Not Interested"];
                                



     [maritialIcon setHidden:NO];
        [maritialLabel setHidden:NO];
       // [maritialLabel setText:currentDao.]

    

   // NSData *data=[[NSData alloc]initWithContentsOfURL:imageURL];

    //[statusImage setImage:[UIImage imageWithData:data]];
    if(mainUserDAO.statusThumb==nil || [mainUserDAO.statusThumb isEqualToString:@""])
    
    else
        FavouritImageDownloader *downloder=[[FavouritImageDownloader alloc]init];
        [downloder startDownloading:mainUserDAO.statusThumb withButton:statusButton];


    



    if([mainUserDAO.statusMessage isEqualToString:@""] || mainUserDAO.statusMessage==NULL )
        [statusLine setText:@"No Status Message Found"];
    else 
        [statusLine setText:mainUserDAO.statusMessage];

    
    int imagex=650;
    int imagey=28;






    if([mainUserDAO.horoscopeThumb length]<=5 || mainUserDAO.horoscopeThumb==nil || [mainUserDAO.horoscopeThumb isEqualToString:@""] || [mainUserDAO.horoscopeThumb rangeOfString:@"no-image"].length > 0);
    else
    
        // imagex=imagex+90;

        UILabel *favTite=[[UILabel alloc]initWithFrame:CGRectMake(imagex+5, imagey-20, 100.0f, 15.0f)];
        [favTite setTextColor:[UIColor blackColor]];
        [favTite setBackgroundColor:[UIColor clearColor]];
        [favTite setText:@"Horoscope"];
        [headerScroll addSubview:favTite];
        [favTite setFont:[UIFont boldSystemFontOfSize:12.0f]];


        //UIImageView *horoscopeThumb=[[UIImageView alloc]initWithFrame:CGRectMake(imagex, imagey,77.0f , 77.0f)];
        UIButton *horoscopeThumb=[UIButton buttonWithType:UIButtonTypeCustom];
        [horoscopeThumb setFrame:CGRectMake(imagex, imagey,77.0f , 77.0f)];
        [horoscopeThumb.imageView setContentMode:UIViewContentModeScaleAspectFit];
        horoscopeThumb.layer.borderWidth=0.8f;
        horoscopeThumb.layer.borderColor=[[UIColor whiteColor]CGColor];

        if([currentDao.BirthPrvID isEqualToString:@"1"])
        
        

        else if([currentDao.BirthPrvID isEqualToString:@"2"])
        
            UILabel *ageLabel=[[UILabel alloc]initWithFrame:CGRectMake(55.0f, 55.0f, 20, 20)];
           if(currentDao.Birthdate!=NULL)
            [ageLabel setText:[self findDateDifference:currentDao.Birthdate]];
            [ageLabel setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"textcolor.png"]]];
            [ageLabel setBackgroundColor:[UIColor clearColor]];

            //[horoscopeThumb addSubview:ageLabel];
        


        [horoscopeThumb setImage:[UIImage imageNamed:@"Placeholder.png"] forState:UIControlStateNormal];
        horoscopeThumb.imageView.layer.borderWidth=0.8f;
        horoscopeThumb.imageView.layer.borderColor=[[UIColor whiteColor] CGColor];
        [favTite setFont:[UIFont boldSystemFontOfSize:12.0f]];
        [headerScroll addSubview:horoscopeThumb];



        FavouritImageDownloader *favDownload=[[FavouritImageDownloader alloc]init];
        [favDownload startDownloading:currentDao.horoscopeThumb withButton:horoscopeThumb];
//        [horoscopeThumb addTarget:self action:@selector(showHoroscopeDetail) forControlEvents:UIControlEventTouchUpInside];

        [favDownload release];
    





    if([mainUserDAO.friendThumb length]<=5 || mainUserDAO.friendThumb==nil || [mainUserDAO.friendThumb isEqualToString:@""] || [mainUserDAO.friendThumb rangeOfString:@"no-image"].length > 0);
    else 
        imagex=imagex+112;//2

        UILabel *favTite=[[UILabel alloc]initWithFrame:CGRectMake(imagex+10, imagey-20, 70.0f, 15.0f)];
        [favTite setTextColor:[UIColor blackColor]];
        [favTite setBackgroundColor:[UIColor clearColor]];
        [favTite setText:@"F.Friend"];
        [favTite setTextAlignment:UITextAlignmentCenter];
        [favTite setFont:[UIFont boldSystemFontOfSize:12.0f]];
        [headerScroll addSubview:favTite];



        UIButton *friendThumb=[UIButton buttonWithType:UIButtonTypeCustom];
        [friendThumb setFrame:CGRectMake(imagex, imagey,77.0f , 77.0f)];
        friendThumb.imageView.layer.borderWidth=0.8f;
        friendThumb.imageView.layer.borderColor=[[UIColor whiteColor] CGColor];
        [headerScroll addSubview:friendThumb];


        favTite.center = friendThumb.center;
        CGRect f = favTite.frame;
        f.origin.y = imagey-20; // new y
        favTite.frame = f;



        FavouritImageDownloader *favdownloader=[[FavouritImageDownloader alloc]init];
        [favdownloader startDownloading:currentDao.friendThumb withButton:friendThumb];
        [friendThumb addTarget:self action:@selector(showFriendDetail) forControlEvents:UIControlEventTouchUpInside];
    

    ...............................// More Code here, I can't paste due to limit of Stack characters

    [self.headerScroll scrollRectToVisible:CGRectMake(321,
                                                      self.headerScroll.frame.origin.y,
                                                      self.headerScroll.frame.size.width,
                                                      self.headerScroll.frame.size.height) animated:YES];

当我滚动时,它会省略一些组件。

那么我怎样才能让它滚动到特定位置,它必须滚动。

提前致谢

【问题讨论】:

【参考方案1】:

有两种滚动视图方法可以帮助您滚动到特定点或帧。

[scrollview scrollRectToVisible:<#CGRect#>YOURRECT animated:YESORNO];

[scrollview setContentOffset:<#(CGPoint)#>YOURPOINT];

希望这会有所帮助。

【讨论】:

滚动时我需要把它们放在哪里。你能再解释一下吗? 添加完所有视图后,目前为止不滚动不在我的手中,除了在Delegate方法中处理之外,你有什么建议? 我猜你正在寻找快速滚动,当你离开滚动它应该捕捉到一个视图,对吧? 是的.....有三个按钮和图像,我希望它们在用户停止时显示,然后再下三个,再下三个,我也可以计算它们。但我会处理滚动? 不,你不能处理滚动,但是你可以在滚动结束时捕捉,当视图到达scrollViewDidEndDecelerating委托函数时捕捉视图。

以上是关于UIScrollView 滚动到特定页面?的主要内容,如果未能解决你的问题,请参考以下文章

分页uiscrollview自动滚动到第一页

UIScrollView 后面如何与 UIView 交互

如何保持 UIscrollview 框架固定但动态范围

UIScrollView 内的可选 UITableViewCells

UIScrollView 帮助

无法单击 UIScrollView 的任何部分来滚动图像。我只能点击一个特定的部分