【问题标题】:Trying to Annotate a MapView and Load a second View Controller Simultaneously尝试注释 MapView 并同时加载第二个视图控制器
【发布时间】:2015-07-24 03:52:15
【问题描述】:

在我的主视图控制器中,我有一个 MKMapView 和 TableView。 tableview 使用 AlamoFire 调用用户信息 API 来填充其单元格。

我希望发生的事情: 当您选择一个单元格时,带有更详细用户信息的第二个容器视图将进入 tableview 所在的屏幕部分。同时,地图视图将使用该用户的位置进行注释。

问题: 当单元格被选中时,第二个容器视图滑到屏幕上(好),地图注释(好),但随后信息容器视图再次消失(坏)。再次点击最初选择的单元格,信息容器视图会滑回屏幕上以保持不变。我需要摆脱这种双击。

地图注释似乎出于某种原因否定了这个过程。当我注释掉地图注释时,VC 转换工作正常……只需轻轻一按。

任何想法都会有所帮助。

这是有问题的代码:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.localTableView.deselectRowAtIndexPath(indexPath, animated: true)


    dispatch_async(dispatch_get_main_queue(),{
        self.populateInfoContainerView(indexPath.row)

        self.mapView.selectAnnotation(self.users[indexPath.row], animated: true)

        if(self.userInfoContainerView.frame.origin.x == UIScreen.mainScreen().bounds.size.width){
            self.showInfoView(true)

        } else {
            self.hideInfoView(true)
        }
    })

}

func showInfoView(animated: Bool){

    UIView.animateWithDuration(0.33, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 1.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: ({
        self.userInfoContainerView.frame.origin.x = 0
        self.localTableView.frame.origin.x = -UIScreen.mainScreen().bounds.size.width
    }), completion: { finished in
        //animation complete
    })
    self.infoVisible = true
}

func hideInfoView(animated: Bool){
    let xPos = UIScreen.mainScreen().bounds.size.width

    if(animated == true){
        UIView.animateWithDuration(0.25, animations: ({
            self.userInfoContainerView.frame.origin.x = xPos
            self.localTableView.frame.origin.x = 0
        }), completion: { finished in
            //animation complete
        })
    } else {
        self.userInfoContainerView.frame.origin.x = xPos
    }
    self.infoVisible = false
}

谢谢。

【问题讨论】:

    标签: ios swift uitableview mkmapview segue


    【解决方案1】:

    我相信我已经找到了答案。我不知道为什么这行得通而上面的版本不行,但我会告诉你我做了什么不同。

    我现在正在为 TableView 和 InfoView 的约束设置动画。

    首先,我 ctrl+单击+将约束从我的 main.storyboard 拖到我的代码中,并创建了一个 IBOutlet 来制作动画。我还添加了一个 let screenWidth 作为常量,我会将它们移动。

    @IBOutlet weak var infoViewLeadingEdge: NSLayoutConstraint!
    @IBOutlet weak var tableViewXCenter: NSLayoutConstraint!
    let screenWidth = UIScreen.mainScreen().bounds.size.width
    

    然后我把上面的代码替换成了showInfo():

    self.userInfoContainerView.frame.origin.x = 0
    self.localTableView.frame.origin.x = -UIScreen.mainScreen().bounds.size.width
    

    使用这个新代码:

    self.infoViewLeadingEdge.constant -= self.screenWidth
    self.tableViewXCenter.constant += self.screenWidth
    self.view.layoutIfNeeded()
    

    在我上面的hideInfo() 中,我替换了这个:

    self.userInfoContainerView.frame.origin.x = xPos
    self.localTableView.frame.origin.x = 0
    

    使用这个新代码:

    self.infoViewLeadingEdge.constant += self.screenWidth
    self.tableViewXCenter.constant -= self.screenWidth
    self.view.layoutIfNeeded()
    

    不要忘记self.view.layoutIfNeeded()。如果您不使用该代码,则动画将不起作用。此外,tableViewXCenter 上的 += 和 -= 似乎与我希望表格执行的操作相反,但是,这是我需要放置它们以使表格按照我想要的方向移动的方式。不过这看起来确实违反直觉。

    就是这样。我希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多