检测没有块的ScrollView动画结束

时间:2013-05-31 14:18:35

标签: ios animation uigesturerecognizer

我有一个滚动视图,可以在加载时自动向下滚动webview,但遗憾的是,由于需要在用户触摸屏幕时暂停动画,我无法将动画放在滚动视图中。

我在检测动画结束时遇到问题而没有使用块。这是我的代码,你可以看到我试图命中的所有委托方法:

-(void)viewDidLoad{
    [super viewDidLoad];


    repeatCount = 0;

    self.view.backgroundColor = [UIColor lightGrayColor];
    self.webView.backgroundColor = [UIColor lightGrayColor];
    self.scrollView.backgroundColor = [UIColor lightGrayColor];

    self.scrollView.delegate=self;


    NSString *htmlMessage =@"";

    //Get text from local file and sent it to webview
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"creditsMessage" ofType:@"txt"];

    if (filePath) {
        NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        if (myText) {
            htmlMessage = [htmlMessage stringByAppendingString:myText];
        }
    } else {
        htmlMessage = _defaultErrorDocumentFileMissing;
    }

    //Load the HTML Message:
    [self.webView loadHTMLString:htmlMessage baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
    [self.webView sizeToFit];

}

 -(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];

    setTimer = FALSE;
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pauseLayer:)];
    [self.scrollView addGestureRecognizer:recognizer];
    recognizer.delegate = self;
    [recognizer release];


    //Start the scrolling
    [self animateScroll];
}


- (void) animateScroll{
    CGFloat scrollHeight = 710;
    [UIScrollView beginAnimations:@"scrollAnimation" context:nil];
    [UIScrollView setAnimationDuration:18.0f];
    [self.scrollView setContentOffset:CGPointMake(0,scrollHeight)];
    [UIScrollView commitAnimations];
    [UIScrollView setAnimationDelegate:self];
    [UIScrollView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
}


- (void)animationDidStop:(NSString*)animationID finished:(BOOL)finished context:(void *)context {
    [self.scrollView setContentOffset:CGPointMake(0,0) animated:NO];
    [self.scrollView setContentSize:CGSizeMake(320, 1500)];
    self.scrollView.scrollEnabled = YES;
    self.webView.scrollView.scrollEnabled = TRUE;
}



    - (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;
}

-(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;
}



//Works on start of scroll
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

}


//None of the below work
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{

}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{

}




-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

}

1 个答案:

答案 0 :(得分:0)

尝试

– scrollViewDidEndScrollingAnimation: