UIView的动画方法没有出现

时间:2018-02-19 17:22:02

标签: swift xcode uiview

我正在尝试创建一个利用某些搜索功能的应用程序,我正在努力使其在按下搜索按钮后,一个视图(包含搜索结果)从超级视图的底部向上移动并替换搜索视图。在故事板上,resultsView(类型为UIView)受到约束,因此其顶部等于superview的底部。按下搜索按钮后,我想为视图设置动画以向上移动并替换超视图底部的视图。问题是,在viewcontroller的类中,当我调用resultsView时,应该与UIView类关联的animateWithDuration(NSTimeInterval)不会出现在我面前。可能这是因为视图已经受到限制了吗?以下是为此帖简化的代码:

import UIKit
import MapKit

class MapViewController: UIViewController, CLLocationManagerDelegate, 
MKMapViewDelegate {
  @IBOutlet weak var mapView: MKMapView!

  @IBOutlet weak var slider: UISlider!

  @IBOutlet weak var distanceLabel: UILabel!

  @IBOutlet weak var searchButton: UIButton!

  @IBOutlet weak var searchView: UIView!

  @IBOutlet weak var resultView: UIView!

  @IBOutlet weak var resultNameLabel: UILabel!

  @IBOutlet weak var resultDistanceLabel: UILabel!

  override func viewDidLoad() {
    super.viewDidLoad()

    mapView.delegate = self

    self.resultView.isHidden = true
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }

  @IBAction func sliderAdjusted(_ sender: Any) {
    let int = Int(slider.value)
    switch int {
    case 1:
        distanceLabel.text = "1 mile"
    default:
        distanceLabel.text = "\(int) miles"
    }
  }


  @IBAction func searchButtonPressed(_ sender: Any) {
    /*This is where, after calling a search function which is omitted
    from this example, I would like to make the resultsView not
    hidden and then animate it to sort of eclipse the search view*/

    self.resultView.isHidden = false
    self.resultView.animate(withDuration: NSTimeInterval)

    /*The above line of code is not actually appearing for me. 
    After typing "self.resultView." the animate function is not being
    given to me as an option for my UIView*/
  }
}

我还会附上一些视图控制器的图像,这样你就可以得到这个想法了。结果视图在此图像中不可见,因为它的顶部被约束到superview的底部,因此它只是超出其superview的可见表示。

Pictured is the view controller with the searchView highlighted. This is the view that I would like to be eclipsed by my resultView after the searchButton is pressed

This is the same view controller with the resultView highlighted. As you can see, its top is constrained to be equal to the superview's bottom. This is the view that I would like to animate upwards into the superview and eclipse the searchView after the search button is pressed.

第一个图像是视图控制器,其中突出显示了searchView。这是我希望在按下searchButton后被我的resultView黯然失色的视图。

第二个图像是相同的视图控制器,其中突出显示了resultView。如您所见,它的顶部被限制为等于superview的底部。这是我希望在按下搜索按钮后向上动画到superview并对searchView进行遮蔽的视图。

1 个答案:

答案 0 :(得分:1)

所有animate系列的方法都是类方法。这意味着您在类对象上调用它们而不是实例。

您正在尝试致电

class func animate(withDuration: TimeInterval, animations: () -> Void)

因此您的代码需要看起来像

UIView.animate(withDuration: 0.5) {
   //the things you want to animate 
   //will animate with 0.5 seconds duration    
}

在特定情况下,您似乎试图设置resultView的高度动画,因此您需要IBOutlet到该约束。你可以称之为resultViewHeight

Create an outlet to the height constraint

UIView.animate(withDuration: 0.5) {
   self.resultViewHeight.constant = theDesiredHeight 
   self.layoutIfNeeded()    
}

在闭包内调用layoutIfNeeded()是动画自动布局的秘诀。没有它,动画就会跳转到并点。