我有一个视图,其中包含一个滚动视图和一个带有 html 的 uiwebview。我试图让滚动视图慢慢自动向下滚动到底部,触摸时暂停,然后在当前位置重新启动动画。
截至目前,我已经显示了 html,滚动视图滚动和触摸停止滚动,但我无法让滚动动画在触摸后一定时间后在当前位置再次启动。这是代码:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *myHTML = @"<html><body><h1>Hello, world!</h1><h1>Hello, world!</h1><h1>Hello, world!</h1><h1>Hello, world!</h1><h1>Hello, world!</h1><h1>Hello, world!</h1><h1>Hello, world!</h1><h1>Hello, world!</h1><h1>Hello, world!</h1><h1>Hello, world!</h1><h1>Hello, world!</h1><h1>Hello, world!</h1><h1>Hello, world!</h1><h1>Hello, world!</h1></body></html>";
[self.webView loadHTMLString:myHTML baseURL:nil];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userDidTapWebView)];
[self.webView addGestureRecognizer:recognizer];
recognizer.delegate = self;
[recognizer release];
//Start the scrolling
[self startAnimation];
}
#pragma mark - Gesture Delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch {
//Stop the scrollview from scrolling when the screen is touched
[self.scrollView.layer removeAllAnimations];
return YES;
}
#pragma mark - Animate Methods
-(void)startAnimation{
[UIScrollView beginAnimations:@"scrollAnimation" context:nil];
[UIScrollView setAnimationDuration:8.0f];
[self.scrollView setContentOffset:CGPointMake(0 , 100)];
[UIScrollView commitAnimations];
}
-(void)dealloc{
[super dealloc];
[self.webView release];
[self.scrollView release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}