ios - 如何在 View Controller 内为 xib 约束设置动画?

标签 ios swift xib uiviewanimation

我构建了一个计数器,当您向上或向下滑动时它会显示动画。

所以基本上它是我制作动画的 View Controller 内的一个 Circular UIView

通过在用户向上或向下滑动时增加偏移量。

它工作得很好,但现在我想把它放在一个 .Xib 文件中,然后

在 View Controller 上加载 Nib。

而且它不起作用,只有标签正在动画但不是

圆形 View 。

如何在 View 中设置 xib 的动画

这是我的代码:

````

func handleSwipes(sender:UISwipeGestureRecognizer) {

   // up or down
   if sender.direction == .Up {
    increment = 1
    offset = 10
   } else {
    increment = -1
    offset = -10
   }

  // animate stuff with constraints
  inc(increment)

  UIView.animateWithDuration(0.18, animations: { _ in

    self.labelYConstraint.constant = self.offset
    self.view.layoutIfNeeded()
    self.label.alpha = 1
    self.label.textColor = UIColor(red: 52/255.0, green: 52/255.0, blue: 88/255.0, alpha: 1)
    self.circleView.filledColor = UIColor(red: 167/255.0, green: 246/255.0, blue: 67/255.0, alpha: 1)
  }
  }) { _ in

    UIView.animateWithDuration(0.18, animations: { _ in
      self.labelYConstraint.constant = 0
      self.view.layoutIfNeeded()
      self.circleView.layer.backgroundColor = UIColor(red: 211/255.0, green: 211/255.0, blue: 211/255.0, alpha: 0.3).CGColor
      self.label.textColor = UIColor.whiteColor()

    })
 }
}

````

第一个按钮是一个 UIView 动画

第二个按钮是嵌入在 View Controller 中的.Xib 文件

enter image description here

最佳答案

您似乎在为标签的 Y 约束设置动画。您必须确保为设置整个组位置的约束设置动画。

一个简单的方法是,在 xib 中,创建一个透明的 subview 并将所有元素放入其中,并创建一个 Y 约束以将该 subview 定位在主 xib View 中。当您为新约束设置动画时,它应该将所有元素移动到一起。

如果这有帮助,请告诉我。

关于ios - 如何在 View Controller 内为 xib 约束设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33833446/

相关文章:

iOS:切换到横向模式时 Webview 不缩放

ios - 带自动换行的多行标签

ios - 具有多个文本字段的 UiTableview 的 ScrollView 显示从一个文本字段到另一个文本字段的复制值

ios - 在多个 Storyboard 中使用相同的 UIViewController

ios - 用于 iPhone 应用程序开发的 Storyboard 或 XIB,我应该选择哪一个?

iphone - 实现视频通话的问题?

ios - 扩展中的向下协议(protocol)一致性

ios - swift 音频套件 : How to build an VCO who's pitch is controlled by an AR-envelope?

swift - 如何在框架中使用 XCGLogger

ios - 使用相同 xib 的多个 UITableViewCell 子类?