Rotating UIButton

I am 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)]; }]; } 

It is assumed that the button will do some β€œshaking,” and then it will return to its original position, but all that it does is change the position of the button, move it only to one side and start the method on the other side, which the button does not respond at all.

What is the problem with my code?

Thanks!

EDIT 2: My turn is how can I make a button to shake / sway a bit, for example. wiggle application mode when editing sptingboard.

Using this code gives me a left turn, smooth animates from left to right, then right to left, and then rotates to its original position. Now I want this to not just rotate, but do it with animation, such as 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!

+3
ios objective-c uibutton
source share
4 answers

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"]; 
+6
source share

Try using CGAffineTransformRotate instead of CGAffineTransformMakeRotation . You can use `CGAffineTransformIdentity1 as the first argument in all calls, so the final conversion of the second argument will be applied to the original form (?) Of the Frame.

+1
source share

Until he answered the question, Charles replied that he was great for turning the button (2 full turns). 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 should match CAAnimationDelegate:

 class ViewController: UIViewController, CAAnimationDelegate { } 
+1
source share

Well, the docs say: "... the specified animations immediately start in another thread ...". Now all UIKit codes are NOT thread safe. Thus, perhaps this will help if you instead call UI settings (setTransform, setCenter) from the completion block (which runs in a separate thread) to invoke them using performSelectorOnMainThread:withObject:

0
source share

All Articles