0

I am trying to make a button shrink/expand upon touch using the code below.

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

Finally I want the keep button in the shrunk state after touch ended.

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

2. How should I do that? (see "<=== Is this correct?" line)
Fogmeister
  • 76,236
  • 42
  • 207
  • 306

1 Answers1

0

I wouldn't use any of the code you have written.

It looks like you're trying to "bounce" the scale of the button so it looks like the user has affected it physically.

To do this you can use the modern block based animation and specifically the new (to iOS7) version that uses spring damping to create the animation curve.

This will give the effect of it bouncing (but you need to get the numbers right).

[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
                 }];

You can read more about this function here...

Apple docs for UIView.

My blog about this function and what the parameters mean.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • This is the perfect solution for getting the button to oscillate. But how do I get the final scale and set the button size accordingly to that? Right now it is popping back to the original size. I tried setting the transform to 0.5 in the completion block but that does not work --thanks .. –  Oct 14 '14 at 18:09
  • @iphone-noob it should keep the scale of 0.7 using this method. This changes the scale just like doing `view.transform = ...` it just animates it. What happens if you remove the animate stuff and just set the transform on it? – Fogmeister Oct 15 '14 at 07:49
  • I had about 3 animations for the same button and as soon as I disabled the other two, it worked as expected. The interactions between the two animations is causing this issue. But that is a separate question. :-) Thanks for this easy solution. I am going to accept your answer. –  Oct 15 '14 at 18:37
  • If you set a new transform on the button in the other animations then yes it will remove the scale transform. – Fogmeister Oct 15 '14 at 23:38