3

I've been trying to rotate a button using the following method:

-(IBAction)rotate:(id)sender{
    CGPoint pencilCenter = pencil.center;
    [pencil setCenter:pencilCenter];
    CGFloat floater = 1.0;
    [UIView animateWithDuration:0.7 animations:^(void){
        [pencil setTransform:CGAffineTransformMakeRotation(floater)];
    }];
    [UIView animateWithDuration:0.7 animations:^(void){
        [pencil setTransform:CGAffineTransformMakeRotation(floater)];
    }];
}

This is supposed to make the button do some kind of "shake", then it's supposed to be back in its original position- yet all it does is changing the button's location, moving it to only one side and on another run of the method the button doesn't react at all.

What's the problem with my code?

Thanks!

EDIT 2: My que is- how do I make a button to do a little shake/wiggle ,e.g. the wiggle app mode when editing sptingboard.

Using this code is giving me rotation to left, smooth animates from left to right then right to left, then rotates to original position. Now, I want this not to just rotate, but do this with an animation, like a wiggle.

[UIView animateWithDuration:0.25
                      delay:0.0
                    options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveLinear | UIViewAnimationOptionAutoreverse)
                 animations:^ {
                     pencil.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(30));
                     pencil.transform = CGAffineTransformIdentity;
                 }
                 completion:^(BOOL finished){
                 }
 ];

Thanks!

Lior Pollak
  • 3,362
  • 5
  • 27
  • 48
  • 1
    why are you starting 2 animations with the same rotation amount at the same time? – Wain Apr 25 '13 at 17:01
  • 1
    Please clarify exactly what you mean by "moving it to only one side". Maybe post some screenshots of the simulator? – Liftoff Apr 25 '13 at 17:04
  • 1
    You can try following post, its for UIView but you can apply it on UIButton http://stackoverflow.com/questions/929364/how-to-create-iphones-wobbling-icon-effect – Deepesh Gairola Apr 25 '13 at 17:10
  • http://stackoverflow.com/questions/5405315/rotating-circled-view-with-user-interaction – rptwsthi Apr 27 '13 at 12:57
  • I read the CoreAnimation documentation again, and the problem was (almost for sure) the anchor point... – Lior Pollak Jul 25 '13 at 11:06

4 Answers4

7

Import "QuartzCore/QuartzCore.h" and try this,

CABasicAnimation *fullRotation;
fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.delegate = self;
fullRotation.fromValue = [NSNumber numberWithFloat:0];
fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
fullRotation.duration = 1.7;
fullRotation.repeatCount = 2;
[btnTemp.layer addAnimation:fullRotation forKey:@"360"];
Shardul
  • 4,266
  • 3
  • 32
  • 50
1

Try using CGAffineTransformRotate instead of CGAffineTransformMakeRotation. You can use `CGAffineTransformIdentity1 as the first argument in all calls, so the final transform according to second argument will be applied on the original shape(?) of the frame.

neeraj
  • 1,191
  • 4
  • 19
  • 47
1

While it didn't perfectly answer the question, Shardul's answer is great to rotate the button (2 full rotations). So here it is, converted to Swift 3:

let fullRotation = CABasicAnimation(keyPath: "transform.rotation")
fullRotation.delegate = self
fullRotation.fromValue = NSNumber(floatLiteral: 0)
fullRotation.toValue = NSNumber(floatLiteral: Double(CGFloat.pi * 2))
fullRotation.duration = 0.5
fullRotation.repeatCount = 2
button.layer.add(fullRotation, forKey: "360")

You will need to import QuartzCore:

import QuartzCore

And your ViewController needs to conform to CAAnimationDelegate:

class ViewController: UIViewController, CAAnimationDelegate {

}
Kqtr
  • 5,824
  • 3
  • 25
  • 32
0

Well, the docs say: "...the specified animations are started immediately on another thread ...". Now, all UIKit code is NOT thread safe. So, maybe it helps if instead calling your UI setters (setTransform, setCenter) from the completion block (which runs in a separate thread) to call them using performSelectorOnMainThread:withObject:.

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
  • 1
    Setting the view properties to be animated within the animation block is the whole point of the animation blocks. You have taken the wrong conclusion from the documentation. – jrturton Apr 26 '13 at 06:42