ios - UIImageView 框架没有按预期进行动画处理(大小和原点)

标签 ios swift animation uiview uiviewanimation

遇到 UIView 帧动画问题。 View 应该在原点和大小上都具有动画效果,随着大小增加和原点线性移动以保持 View 在同一位置。但是发生的事情是, View 减小到大小 (0,0),然后增大到仍然不正确的大小。见附件视频。

问题视频:https://media.pairby.com/I/u/a/IualExcJXn7CqLsGkcNZfwyEw5MKi3SV/v.mp4

func animateIn() {
  // Make _iconView large
  let w = bounds.width
  _iconView.frame = CGRect(
    x: frame.midX - w/2,
    y: frame.midY - w/2,
    width: w, height: w)

  isHidden = false

  UIView.animate(withDuration: 0.2, animations: {
    self.alpha = 1

    // Animate it smaller
    let w = self.bounds.width * 0.5
    self._iconView.frame = CGRect(
      x: self.frame.midX - w/2,
      y: self.frame.midY - w/2,
      width: w, height: w)
  })
}

func animateOut() {
  UIView.animate(withDuration: 3, delay: 0, options: .beginFromCurrentState, animations: {
    self.alpha = 0

    // Make it large again
    let w = self.bounds.width
    self._iconView.frame = CGRect(
      x: self.frame.midX - w/2,
      y: self.frame.midY - w/2,
      width: w, height: w)

  }, completion: { _ in self.isHidden = true })
}

更多详情:

self 是 UIView 的子类,受限于父 View 。

_iconView 是一个 UIImageView

animateIn 保证在 animateOut

之前运行

animateOut 是未按预期工作的函数,animateIn 工作

最佳答案

您还不完全清楚您需要做什么,但是...

  1. 开始时您不需要更改大小 - 您可以简单地将 _iconView 框架从其当前状态设置为动画,并且

  2. 因为 _iconView 是 self subview ,您需要相对于边界而不是框架定位它。

像这样尝试:

func doAnim() -> Void {     

    UIView.animate(withDuration: 3, delay: 0, options: .beginFromCurrentState, animations: {
        self.alpha = 0

        let s = self.bounds.width
        let halfS = s / 2

        self._iconView.frame = CGRect(
            x: self.bounds.midX - halfS,
            y: self.bounds.midY - halfS,
            width: s,
            height: s)

    })

}

关于ios - UIImageView 框架没有按预期进行动画处理(大小和原点),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46078196/

相关文章:

ios - Swift 3 解析 JSON 嵌套问题

ios - 定位在屏幕外与隐藏

ios - 如何使用按钮点击启动动画?

android - 向上滑动动画覆盖 fragment 而不是同时动画两个 fragment

ios - Apple 的语音处理音频单元 ( kAudioUnitSubType_VoiceProcessingIO ) 在 iOS 5.1 上损坏

ios - @IBDesignable 错误 : IB Designables: Failed to update auto layout status: Interface Builder Cocoa Touch Tool crashed

iphone - UITableView 中的第二个 View 将不会显示从 JSON 解析的数据

ios - UIBarButtonItem 在 < iOS 11 上的位置不正确

swift - 失败的 NSManagedObject 仍在保存

ios - 为方便起见,不能使用#define