视图无法动画

时间:2015-05-21 20:28:46

标签: swift animation uiview

我有一个视图,它会像屏幕底部的抽屉一样向上移动。但它没有做任何事情。它只是坐在那里=) 任何人都可以告诉我,为什么会这样做?

这是我的代码:

import UIKit

class InfoPopUpVC: UIViewController {
  var superView: UIView!
  var labelText: String!
  let textLabel =  UILabel()
  let height = CGFloat(80)


  override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    UIView.animateWithDuration(0.4, animations: { () -> Void in
      self.view.center.y = 50
    })
  }

override func viewDidLoad() {
  super.viewDidLoad()
  setupTextLabel()
  view.frame = CGRectMake(0, superView.frame.maxY-height, superView.frame.width, height)
  AnimationHelper.blurBackgroundForView(view)
  view.backgroundColor = .greenColor()
  }

func setupTextLabel(){
  textLabel.text = labelText
  textLabel.frame = CGRectMake(0, 0, view.frame.width, view.frame.height)
  textLabel.numberOfLines = 3
  textLabel.textAlignment = .Center

  textLabel.frame.inset(dx: 10, dy: 8)
  textLabel.sizeToFit()
  textLabel.font = UIFont(name: "HelveticaNeue-Light", size: 17)
  textLabel.textColor = .whiteColor()
  view.addSubview(textLabel)
  }
}

2 个答案:

答案 0 :(得分:1)

尝试将您的代码放在viewDidAppear或viewWillAppear中,并使用dispatch async。否则你的动画可能无效。

 override func viewDidAppear(animated: Bool) {
dispatch_async(dispatch_get_main_queue(), {
            UIView.animateWithDuration(0.4, animations: { () -> Void in
      self.view.center.y = 50
    })
        })
}

答案 1 :(得分:0)

如果使用自动布局约束UIView,则无法为框架或类似属性设置动画。

您有两种选择:

  1. 摆脱自动布局并为框架目录设置动画
  2. 使用自动布局和动画限制(例如通过插座)
  3. 请参阅以下链接以获取示例:

    How do I animate constraint-changes

    IOS: ANIMATING AUTOLAYOUT CONSTRAINTS