iPhone SDK中的自动滚动功能
问题描述:
我已通过示例代码向应用程序添加了自动滚动功能。 在这段代码中,我从上到下和从下到上滚动。但我不想从上到下滚动。我总是只想从下到上滚动。如果我删除了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];
}
答
我觉得你的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是否为真