检索collectionviewcell中按钮的中心cgpoint以添加子视图

时间:2019-06-25 00:39:26

标签: swift uicollectionview addsubview

我正在尝试从CollectionButton单元格Example中的UIButton(发送者)的中心创建一个子视图弹出窗口。

// Point Popup Functions
func buttonScorePopUp(_ sender: UIButton) {
    if var centerCellIndexPath: IndexPath = collectionView.centerCellIndexPath {

        print("Pop Up Score Called")

        let cell = self.collectionView?.cellForItem(at: centerCellIndexPath)

        // Get the postion of the button for popup starting point
        let centerPosition = sender.center

        // Get the card at indexPath
        let card = cards[centerCellIndexPath.row]

        // Create Score View
        let scoreView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 21))
        scoreView.image = UIImage(named:"100")
        scoreView.contentMode = .scaleAspectFit
        scoreView.center = centerPosition
        cell?.addSubview(scoreView)

        print("Called for Score \(card.cardPoints)")

        // Animate Score View
        UIView.animate(withDuration: 1.0, delay: 0.3, options: [.curveEaseOut], animations: {
            //scoreView.center.y -= 100
            scoreView.frame.size.height = 42
        }, completion: { _ in
            scoreView.removeFromSuperview()
        })

    }
}

该弹出窗口可以很好地工作,但是它在按钮[y轴](例如200像素)上方和左键[x轴]上方显示得更远。我不知道要在按下按钮的中心启动此弹出窗口吗?控制台中没有错误,并且弹出窗口显示了我在所附图像中圈出的位置。

0 个答案:

没有答案