首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >safeAreaLayoutGuide动画(Swift 5)

safeAreaLayoutGuide动画(Swift 5)
EN

Stack Overflow用户
提问于 2022-02-01 04:25:10
回答 1查看 130关注 0票数 1

当搜索栏被选中和取消选择时,我想要一个平滑的动画这个视图。现在是波涛汹涌的:

下面是我在searchResultsUpdater中的代码。据我所知,这些函数应该处理动画,我不知道这里有什么问题:

代码语言:javascript
复制
func updateSearchResults(for searchController: UISearchController) {
    
    //MapView moves up when search bar is selected
    if searchController.isActive == true{
        UIView.animateKeyframes(withDuration: 0.25, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: 7), animations: {
            self.mapView.frame.origin.y=self.view.safeAreaLayoutGuide.layoutFrame.origin.y

        },completion: nil)
    }
    
    //MapView moves down when cancel button is selected
    if searchController.isActive == false{
        UIView.animateKeyframes(withDuration: 0.25, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: 7), animations: {
            self.mapView.frame.origin.y=self.view.safeAreaLayoutGuide.layoutFrame.origin.y
            
        },completion: nil)
    }
}

任何帮助都是感激的,谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-01 20:24:47

我找到了解决办法。最初,我在CGRect ()中使用了mapView框架的viewDidLayoutSubviews:

代码语言:javascript
复制
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
     //layout to fit frame of view
    mapView.frame = CGRect(x: 0, y: view.safeAreaInsets.top, width: 
    view.frame.size.width, height: view.frame.size.height - 
    view.safeAreaInsets.top)
  
}

我把它移到了viewDidLoad(),现在它工作了:

代码语言:javascript
复制
override func viewDidLoad() {
    super.viewDidLoad()
    
    //add gesture recognizer for mapView
    mapView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(mapViewAnimation)))
    //layout to fit frame of view
    mapView.frame = CGRect(x: 0, y: view.safeAreaInsets.top, width: 
    view.frame.size.width, height: view.frame.size.height - 
    view.safeAreaInsets.top)

}

我还添加了一个手势识别器,这样searchResultsUpdater就不会那么混乱了:

代码语言:javascript
复制
//handle the animation for mapview
@objc func mapViewAnimation(){
    UIView.animateKeyframes(withDuration: 0.25, delay: 0.0, options: 
    UIView.KeyframeAnimationOptions(rawValue: 7), animations: {
        
    self.mapView.frame.origin.y = 
    self.view.safeAreaLayoutGuide.layoutFrame.origin.y
        
    },completion: nil)
    
}

如果有人知道为什么在我使用CGRect in didLayoutSubViews()时它没有工作,那么可以让我知道。谢谢!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70935549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档