2009-07-09 2 views
4

CALayers를 사용하는 거품이 맨 아래로 흐르게하는 코드가 있습니다. 사용자가 화면을 터치하면 현재 실행중인 애니메이션을 손가락이 닿은 toPoint가있는 코드로 대체하는 코드가 생깁니다. 애니메이션이 전환되면 시뮬레이터가 아닌 장치에서 깜박임이 발생합니다. 깜박임 제거에 대한 모든 정보는 높이 평가 될 것입니다! 감사. 레이어 자체 내부에 흐르는 거품에 대한위치 속성 중반 애니메이션에서 CABasicAnimation을 전환하면 깜박임이 발생합니다.

코드 :

CABasicAnimation *animation = [CABasicAnimationanimationWithKeyPath:@"position"]; 
[animation setDelegate:self]; 
CGPoint position = [self position]; 
NSValue *prevVal = [NSValue valueWithCGPoint:position]; 
[animation setFromValue:prevVal]; 
CGPoint toPoint = CGPointMake(position.x,-100); 
[animation setToValue:[NSValue valueWithCGPoint:toPoint]]; 
[animation setDuration:animationDuration]; 
[self addAnimation:animation forKey:@"flow"]; 

슈퍼 레이어에 기록 된 터치 점 근처에 거품을 유치 코드 :

int count = [self.layer.sublayers count]; 
for(int i = 0; i < count ; i++) { 
    CALayer *layer= [self.layer.sublayers objectAtIndex:i]; 
    CALayer *p = (CALayer*)[layer presentationLayer]; 
    CGPoint position = [p position]; 

    if(abs(position.x - touchPoint.x) < 100 && abs(position.y - touchPoint.y) < 100) { 

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; 
    [animation setDelegate:self]; 
    NSValue *prevVal = [NSValue valueWithCGPoint:position]; 
    [animation setFromValue:prevVal]; 
    [animation setToValue:[NSValue valueWithCGPoint:touchPoint]]; 
    [animation setDuration:2.0]; 
    [animation setTimingFunction:[CAMediaTimingFunction 
      functionWithName:kCAMediaTimingFunctionEaseOut]]; 
    [layer addAnimation:animation forKey:@"flow"]; 
    }   

}

+0

당신이 얼마나 많은 서브 레이어가 있나요 : 터치 처리 방법에

, 트랜잭션 및 잠금에서 애니메이션을 포장? 시뮬레이터는 거의 항상 장치보다 빠릅니다. 또한,보다 부드러운 애니메이션 전환을 위해 EaseInEaseOut을 시도하십시오. – amattn

+0

이 답변을 확인할 수 있습니다. http://stackoverflow.com/a/25611323/1953178 –

답변

5

업데이 트를 둘러싼 CATransaction 잠금을 사용하여 도움이되는지 확인하십시오. 이렇게하면 이전 애니메이션이 새로운 애니메이션으로 업데이트하는 동안 레이어의 위치가 변경되는 것을 방지 할 수 있습니다.

[CATransaction lock]; 
[CATransaction begin]; 

// update the sublayers with new animations 

[CATransaction commit]; 
[CATransaction unlock]; 
관련 문제