0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

タップしたUICollectionViewのCellの上だけにViewを表示する

Posted at

こんな感じです

962F3E1A-6D0B-4D06-9E7C-A3C3E2FBE89C_1_201_a.jpeg [UISliderとCollectionViewの簡単復習App](https://qiita.com/HiroUrata/items/ae190ddbecfd929e6b09)で作ったアプリに思いつきで機能を追加しただけです。

 コードと簡単解説

  • タップしたCellの上に表示するViewの作成
CellOnView
import Foundation
import UIKit

class CellOnView{
    
    let cellOnView = UIView()
    
}

extension CellOnView{
    
    func createCellOnView(cellPointX:CGFloat,cellPointY:CGFloat,cellWidth:CGFloat,cellHeight:CGFloat,targetView:UIView){
        
        cellOnView.frame = CGRect(x: cellPointX, y: cellPointY, width: cellWidth, height:cellHeight)
        
        cellOnView.backgroundColor = .systemGreen
        
        targetView.addSubview(cellOnView)
    
    }
}
  • cellOnView.createCellOnView(~~~省略~~~,targetView:UIView)でUIViewを必要としているので,タップしたCellをUIViewとしてselectCellに入れています
ViewController
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            
        let selectCell = collectionView.cellForItem(at: indexPath)! as UIView
        
        cellOnView.createCellOnView(cellPointX: selectCell.bounds.minX, cellPointY: selectCell.bounds.minY, cellWidth: selectCell.frame.size.width, cellHeight: selectCell.frame.size.height, targetView: selectCell)
            
    }

終わり

休憩中に思いつきでやってみたことについて書きました。(役立つかは微妙ですね)
ご指摘や質問などありましたら、喜んで受け付けてます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?