0

我有一个视图,其中包含一个滚动视图和一个带有 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);
}
4

1 回答 1

0

您缺少再次启动动画的代码:如何调用具有延迟的多个参数的方法

而且,当您调用 removeAllAnimations 时,滚动视图将跳转到预期的最终位置,因此它不会像您想要的那样真正暂停。

您可以在这里查看如何暂停和恢复动画:Is there a way to pause a CABasicAnimation?

因此,将它们放在一起(如果再次触摸滚动视图,则需要额外检查以再次恢复动画):

-(void)pauseLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}

-(void)resumeLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
       shouldReceiveTouch:(UITouch *)touch {
    if (self.scrollView.layer.timeOffset>0) {
        [self resumeLayer:self.scrollView.layer]; //resume the animation on the subsequent touch
    }
    else {
        //first we pause the animation
        [self pauseLayer:self.scrollView.layer];

        //after certain seconds we will start again the animation
        double delayInSeconds = 2.0; //adjust this value
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [self resumeLayer:self.scrollView.layer];
        });
    }
    return YES;
}
于 2013-05-31T01:53:40.907 回答