【问题标题】:Have UIScrollView Animation Pause and Restart on Gesture Touch让 UIScrollView 动画暂停并在手势触摸上重新启动
【发布时间】:2013-05-31 01:03:56
【问题描述】:

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

【问题讨论】:

    标签: iphone ipad uiscrollview uigesturerecognizer


    【解决方案1】:

    您缺少重新启动动画的代码:how to call a method of multiple arguments with delay

    当你调用 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;
    }
    

    【讨论】:

    • 我实现了代码,但委托方法仍然没有接起来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    相关资源
    最近更新 更多