【问题标题】:Rotating UIButton旋转 UIButton
【发布时间】:2013-04-25 16:56:14
【问题描述】:

我一直在尝试使用以下方法旋转按钮:

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

这应该使按钮进行某种“摇动”,然后它应该回到原来的位置 - 但它所做的只是改变按钮的位置,将其仅移动到一侧并在另一次运行按钮没有反应的方法根本

我的代码有什么问题?

谢谢!

编辑 2: 我的问题是 - 我如何制作一个按钮来做一点摇晃/摆动,例如编辑 sptingboard 时的 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){
                 }
 ];

谢谢!

【问题讨论】:

  • 为什么要同时启动 2 个旋转量相同的动画?
  • 请明确“将其移至一侧”的确切含义。也许贴一些模拟器的截图?
  • 您可以尝试以下帖子,它适用于 UIView 但您可以将其应用于 UIButton stackoverflow.com/questions/929364/…
  • 我再次阅读了 CoreAnimation 文档,问题是(几乎可以肯定)锚点...

标签: ios objective-c uibutton


【解决方案1】:

导入“QuartzCore/QuartzCore.h”并试试这个,

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

【讨论】:

    【解决方案2】:

    尝试使用CGAffineTransformRotate 而不是CGAffineTransformMakeRotation。您可以在所有调用中使用 `CGAffineTransformIdentity1 作为第一个参数,因此根据第二个参数的最终变换将应用于框架的原始形状(?)。

    【讨论】:

      【解决方案3】:

      虽然它没有完美地回答这个问题,但 Shardul 的回答非常适合旋转按钮(2 个完整旋转)。就是这样,转换为 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")
      

      您需要导入 QuartzCore:

      import QuartzCore
      

      而且你的 ViewController 需要符合 CAAnimationDelegate:

      class ViewController: UIViewController, CAAnimationDelegate {
      
      }
      

      【讨论】:

        【解决方案4】:

        好吧,docs 说:“...指定的动画立即在另一个线程上启动...”。现在,所有 UIKit 代码都不是线程安全的。因此,如果改为从完成块(在单独的线程中运行)调用您的 UI 设置器(setTransform、setCenter)以使用performSelectorOnMainThread:withObject: 调用它们,也许会有所帮助。

        【讨论】:

        • 在动画块中设置要动画的视图属性是动画块的重点。您从文档中得出了错误的结论。
        猜你喜欢
        • 1970-01-01
        • 2016-03-10
        • 1970-01-01
        • 2021-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-02
        • 2016-08-24
        相关资源
        最近更新 更多