2014-09-26 5 views
1

이 간단한 트릭을 알아 내려고 몇 주일이 걸렸으므로이 Q & 형식을 사용해 보도록하겠습니다.UIScrollView 애니메이션하기

CoreAnimation으로 UIScrollView의 스크롤을 어떻게 움직이게합니까? This answer은 지나치게 복잡하지만 올바른 방향으로 나아갈 수 있습니다.

답변

0

간단!

Core Animation을 사용하려면 UIView의 기본 CALayer 속성을 수정해야합니다. UIScrollView를 스크롤하면 CALayer의 bounds 속성 (특히 origin.y 구성 요소)을 수정하면됩니다. 따라서이 코드는 트릭을 수행합니다.

NSString *keyPath = @"bounds.origin.y"; 
[self.scrollView.layer setValue:[NSNumber numberWithFloat:0] forKeyPath:keyPath]; 

CABasicAnimation *bounceAnimation = [CABasicAnimation animationWithKeyPath:keyPath]; 
bounceAnimation.fromValue = [NSNumber numberWithFloat:100]; 
bounceAnimation.toValue = [NSNumber numberWithFloat:0]; 
bounceAnimation.duration = 2.0f; 

[self.scrollView.layer addAnimation:bounceAnimation forKey:@"someKey"];