2011-06-15 56 views
1

我已通過示例代碼嚮應用程序添加了自動滾動功能。 在這段代碼中,我從上到下和從下到上滾動。但我不想從上到下滾動。我總是隻想從下到上滾動。如果我刪除了topToBottom代碼,並根據該代碼無法正常工作對其餘代碼進行更改。誰能幫我。iPhone SDK中的自動滾動功能

-(void)viewdidLoad 
{ 
    infoScrollView = [[UIScrollView alloc]init]; 
    infoScrollView.frame = CGRectMake(0,0,330,270); 
    infoScrollView.contentSize = CGSizeMake(100,1000); 
    [myInfoView addSubview:infoScrollView]; 

    scrollingToTop=NO; 

    UIImageView *infoImgView = [[UIImageView alloc]initWithFrame:CGRectMake(70,0,200,1000)]; 
    infoImgView.image = [UIImage imageNamed:@"lepke-tar2.png"]; 
    [infoScrollView addSubview:infoImgView]; 

    infoScrollView.delegate = self; 
    if (infoScrollView.contentSize.height>infoScrollView.frame.size.height) 
    { 
     [infoScrollView scrollRectToVisible:CGRectMake(0, infoScrollView.contentSize.height-infoScrollView.frame.size.height, infoScrollView.frame.size.width, infoScrollView.frame.size.height) animated:NO]; 

    } 
    [self performSelector:@selector(scrollToTop) withObject:nil afterDelay:0.5]; 
    [myInfoView addSubview:infoScrollView]; 


    UIButton* infocancelBttn = [[UIButton alloc ]initWithFrame:CGRectMake(295,-8,45,45)]; 
    [infocancelBttn setImage:[UIImage imageNamed:@"cancel_icon.png"] forState:UIControlStateNormal]; 
    [infocancelBttn addTarget:self action:@selector(infoViewCloseAction) forControlEvents:UIControlEventTouchUpInside]; 
    [myInfoView addSubview:infocancelBttn]; 
    [infocancelBttn release]; 


} 

-(void)viewDidAppear:(BOOL)animated 
{ 
    [infoScrollView scrollRectToVisible:CGRectMake(0, 0, infoScrollView.frame.size.width, infoScrollView.frame.size.height+200) animated:YES]; 
} 


- (void) scrollToTop 
{ 
    scrollingToTop=YES; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:10.0]; 
    //[self.scrollView scrollRectToVisible:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height) animated:YES]; 
    [infoScrollView setContentOffset:CGPointMake(0,-300)]; 

    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(scroll)]; 

    [UIView commitAnimations]; 
} 

-(void) scroll 
{ 
    if (scrollingToTop==YES) 
    { 
     [self performSelector:@selector(scrollToBottom) withObject:nil afterDelay:1.0]; 
    } 
    else 
    { 
     [self performSelector:@selector(scrollToTop) withObject:nil afterDelay:1.0]; 
    } 

} 

- (void)scrollToBottom 
{ 
    scrollingToTop=NO; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:10.0]; 
    [infoScrollView setContentOffset:CGPointMake(0,infoScrollView.contentSize.height-infoScrollView.frame.size.height)animated:NO]; 

    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(scroll)]; 

    [UIView commitAnimations]; 
} 

回答

0

我覺得你的if語句是錯誤的:

-(void) scroll 
{ 
    if (scrollingToTop==YES) 
    { 
     [self performSelector:@selector(scrollToBottom) withObject:nil afterDelay:1.0]; 
    } 
    else... 

,如果你刪除scrollToBottom法代碼失敗。

您應該將其更改爲
[self performSelector:@selector(scrollToTop)withObject:nil afterDelay:1.0];
因爲您在詢問scrollingToTop是否爲真