Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIView帧的CASpringAnimation_Ios_Uiview_Swift3_Caanimation - Fatal编程技术网

Ios UIView帧的CASpringAnimation

Ios UIView帧的CASpringAnimation,ios,uiview,swift3,caanimation,Ios,Uiview,Swift3,Caanimation,我正在使用这段代码用spring动画为视图设置动画,但它似乎没有这样做。视图捕捉到帧,不带任何“弹簧”动画 之前您建议使用: UIView.animate(持续时间:0.65,延迟:0,使用SpringWithDamping:0.6,初始SpringVelocity:3.0,选项:.curveEaseOut,动画:{…} 我只想告诉你们,我的视图使用了cornerRadius,所以我不能用上面的设置cornerRadius的动画 magnifyView (layer: CALayer, siz

我正在使用这段代码用spring动画为视图设置动画,但它似乎没有这样做。视图捕捉到帧,不带任何“弹簧”动画

之前您建议使用:

UIView.animate(持续时间:0.65,延迟:0,使用SpringWithDamping:0.6,初始SpringVelocity:3.0,选项:.curveEaseOut,动画:{…}

我只想告诉你们,我的视图使用了cornerRadius,所以我不能用上面的设置cornerRadius的动画

 magnifyView (layer: CALayer, size: CGSize) {

     let oldBounds = layer.bounds
        var newBounds = oldBounds
        newBounds.size = size

        let animateFrame = CASpringAnimation(keyPath: "transform.scale")

        animateFrame.fromValue = NSValue(cgRect: oldBounds)
        animateFrame.toValue = NSValue(cgRect: newBounds)
        animateFrame.damping = 0.65
        animateFrame.initialVelocity = 3.0
        animateFrame.duration = animateFrame.settlingDuration


            layer.add(animateFrame, forKey: "transform.scale")
             layer.bounds = newBounds

}

您的动画错误。您将关键点路径设置为transform.scale,这将是一个CGFloat,导致整个视图的大小发生变化

然后插入fromValue和toValue NSValues,它们是CGRECT。我怀疑运行该代码时控制台中会出现错误

如果要设置帧动画,请设置帧动画。(不是边界,帧。)使用

编辑: 使用问题开头提到的UIView动画显然更容易做到这一点。请尝试以下代码:

class ViewController: UIViewController {

  var isLarge: Bool = false
  @IBOutlet weak var roundLabel: UILabel!

  @IBAction func handleResizeButton(_ sender: UIButton) {
    isLarge = !isLarge
    let size: CGFloat = isLarge ? 1.5 : 1.0
    UIView.animate(withDuration: 0.65, delay: 0, 
      usingSpringWithDamping: 0.6, 
      initialSpringVelocity: 3.0, 
      options: .curveEaseOut, 
      animations: {
        self.roundLabel.transform = CGAffineTransform(scaleX: size, y: size)
      }
    )
  }
}

在我的控制台中没有错误。将边界更改为帧对我的动画没有影响(仍然是快照而不是弹簧)请注意,您还需要更改用于创建动画的关键点路径。如果使用“自动布局”,则更改帧将无法按预期工作。视图上的约束将接管并将帧更改为最终值,从而覆盖动画。您应该使用UIView动画,并在动画的x和7个位置上设置约束动画你的视图。你说“我只是想告诉你我的视图使用了cornerRadius,所以我不能用上面的设置cornerRadius的动画!”我不知道这意味着什么。你发布的代码不会更改视图的cornerRadius,因此我不确定你的意思。你可以使用UIView动画来设置非零角半径视图的动画,效果很好。不,你是对的。这不是完成的代码。我只是想明确我想要设置cornerRadius的动画以及框架。。。
let animateFrame = CASpringAnimation(keyPath: "frame")
animateFrame.fromValue = NSValue(cgRect: oldFrame)
animateFrame.toValue = NSValue(cgRect: newFrame)
layer.frame = newFrame
class ViewController: UIViewController {

  var isLarge: Bool = false
  @IBOutlet weak var roundLabel: UILabel!

  @IBAction func handleResizeButton(_ sender: UIButton) {
    isLarge = !isLarge
    let size: CGFloat = isLarge ? 1.5 : 1.0
    UIView.animate(withDuration: 0.65, delay: 0, 
      usingSpringWithDamping: 0.6, 
      initialSpringVelocity: 3.0, 
      options: .curveEaseOut, 
      animations: {
        self.roundLabel.transform = CGAffineTransform(scaleX: size, y: size)
      }
    )
  }
}