Rotating UIButton

后端 未结 4 1051
攒了一身酷
攒了一身酷 2021-01-20 11:33

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

-(IBAction)rotate:(id)sender{
    CGPoint pencilCenter = pencil.center;
    [pencil setCente         


        
相关标签:
4条回答
  • 2021-01-20 12:14

    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 {
    
    }
    
    0 讨论(0)
  • 2021-01-20 12:20

    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"];
    
    0 讨论(0)
  • 2021-01-20 12:22

    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:.

    0 讨论(0)
  • 2021-01-20 12:31

    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.

    0 讨论(0)
提交回复
热议问题