2014-10-14 3 views
0

아래 코드를 사용하여 터치시 축소/확대 버튼을 만들려고합니다.CAKeyFrameAnimation : 최종 위치/스케일을 어디에/어떻게 설정합니까?

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *myTouch = [touches anyObject]; 
    UIView *view = [myTouch view];  
    view.transform = CGAffineTransformMakeScale(0.7, 0.7);<--IS THIS correct?? 
    CAKeyframeAnimation *scale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"]; 
    scale.duration = 0.8; 
    scale.values = @[@1.0, @0.7, @0.9, @0.7, @0.8, @0.7]; 
    scale.keyTimes = @[@0.13, @0.26, @0.39, @0.52, @0.65, @0.8]; 
    scale.repeatCount = 1; 
    scale.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
    [view.layer addAnimation:scale forKey:@"scaleAnimation"]; 
} 

마지막으로 터치가 끝난 후에 축소 버튼이 유지되도록합니다.

1. Where should I try to permanently change the layers scale? TouchesBegan? TouchesEnded? 

2. How should I do that? (see "<=== Is this correct?" line) 

답변

0

나는 작성한 코드를 사용하지 않을 것입니다.

사용자가 물리적으로 영향을받은 것처럼 보이도록 단추의 눈금을 "바운스"하려고하는 것처럼 보입니다.

이렇게하려면 최신 블록 기반 애니메이션과 특히 스프링 댐핑을 사용하여 애니메이션 커브를 만드는 새로운 (iOS7에) 버전을 사용할 수 있습니다.

이렇게하면 튀는 효과를 얻을 수 있습니다 (단, 숫자를 올바르게 입력해야 함).

[UIView animateWithDuration:0.8 
         delay:0.0 
    usingSpringWithDamping:0.0 // mess around with this 
     initialSpringVelocity:0.0 // and this 
        options:0 
       animations:^{ 
        view.transform = CGAffineTransformMakeScale(0.7, 0.7); 
       } 
       completion:^(BOOL finished){ 
        // if you want to reset the button do it here 
       }]; 
현재이 기능에 대한 자세한 내용을보실 수 있습니다

...

Apple docs for UIView.

My blog about this function and what the parameters mean.

+0

감사합니다! 나는 그것을 바로 시도 할 것이다! –

+0

이것은 버튼을 진동시키는 완벽한 솔루션입니다. 그러나 최종 스케일을 얻고 그에 따라 버튼 크기를 어떻게 설정합니까? 지금은 원래 크기로 다시 터져 나옵니다. 완료 블록에서 0.5로 변환을 시도했지만 작동하지 않습니다 - 감사합니다 .. –

+0

@ iphone-noob이 방법을 사용하여 0.7의 눈금을 유지해야합니다. 이것은'view.transform = ... '처럼 스케일을 변경합니다. 애니메이션 항목을 제거하고 여기에 변형을 설정하면 어떻게됩니까? – Fogmeister