gpt4 book ai didi

swift - UIView 的动画方法没有出现

转载 作者:行者123 更新时间:2023-11-28 15:01:29 27 4
gpt4 key购买 nike

我正在尝试制作一个利用某些搜索功能的应用程序,并且我正在尝试制作它以便在按下搜索按钮后,一个 View (包含搜索结果)从 super View 的底部向上移动并替换搜索 View 。在 Storyboard 上,resultView(UIView 类型)受到约束,因此它的顶部等于父 View 的底部。按下搜索按钮后,我想为 View 设置动画以向上移动并替换已经位于 super View 底部的 View 。问题是,在 viewcontroller 的类中,当我调用 resultsView 时,应该与 UIView 类相关联的 animateWithDuration(NSTimeInterval) 没有出现。这可能是因为 View 已经被限制在适当的位置了吗?这是为这篇文章简化的代码:

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*/
}
}

我还会附上一些 View Controller 的图片,这样您就可以大致了解一下。结果 View 在此图像中不可见,因为它的顶部被限制在父 View 的底部,因此它刚好在其父 View 的可见表示之外。

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 的 View Controller 。这是我希望在按下 searchButton 后被我的 resultView 遮住的 View 。

第二张图片是同一个 View Controller ,突出显示了 resultView。如您所见,它的顶部被限制为等于父 View 的底部。这是我想在按下搜索按钮后向上动画到 super View 并使 searchView 黯然失色的 View 。

最佳答案

所有 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() 是激活自动布局的秘诀。如果没有它,动画将直接跳转到该点。

关于swift - UIView 的动画方法没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48871149/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com