2013-05-31 1 views
0

나는 내부에 HTML이있는 scrollview 및 uiwebview가있는보기를 가지고 있습니다. 스크롤 뷰를 천천히 자동으로 아래쪽으로 스크롤하려고 할 때, 터치 할 때 일시 중지 한 다음 현재 위치에서 애니메이션을 다시 시작하려고합니다.UIScrollView 애니메이션 있음 제스처 터치에서 일시 중지 및 다시 시작

지금 당장은 html 표시, scrollview 스크롤 및 스크롤을 멈추는 터치가 있지만 터치 한 후 일정 시간이 지나면 현재 위치에서 다시 스크롤 애니메이션을 시작할 수 없습니다. .

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

답변

0

다시 애니메이션을 시작하는 코드를 누락 : 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; 
} 
+0

코드를 구현했는데 대리자 메서드가 아직 선택되지 않았습니다. – user1079052

+0

위임 방법은 무엇입니까? –

관련 문제