Re: What is the best strategy for this?


Ben Kennedy
 


On 3 Apr 2021, at 8:46 am, Ben Kennedy <ben-groups@...> wrote:

On Apr 3, 2021, at 8:04 AM, Rick Aurbach via groups.io <rlaurb@...> wrote:

Each of these objects is a cell in a UICollectionView (with a custom Layout). I rather fear that without overriding pieces of touch processing, the collection view will not treat the rotated cell geometry properly.

Yes, so why not transform the collection view as a whole (not the cells)?

This piqued my curiosity, so I couldn't help myself and built a test case. This seems to work perfectly -- and no collection view required. The code is short enough that I've copy/pasted it all below.

-ben


class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let dimension = 4
        let spacing = CGFloat(2.0)
        
        let containerStackView = UIStackView()
        containerStackView.axis = .vertical
        containerStackView.spacing = spacing
        containerStackView.alignment = .center
        containerStackView.distribution = .equalSpacing
        containerStackView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(containerStackView)
        NSLayoutConstraint.activate([
            containerStackView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            containerStackView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
        ])
        
        for y in 1...(dimension * 2) {
            let rowStackView = UIStackView()
            rowStackView.axis = .horizontal
            rowStackView.spacing = spacing
            rowStackView.alignment = .center
            rowStackView.distribution = .equalSpacing
            rowStackView.translatesAutoresizingMaskIntoConstraints = false
            containerStackView.addArrangedSubview(rowStackView)
            
            for _ in 1...(y <= dimension ? y * 2 - 1 : (dimension * 2 - y + 1) * 2 - 1) {
                let cubbyHoleView = CubbyHoleView()
                cubbyHoleView.translatesAutoresizingMaskIntoConstraints = false
                rowStackView.addArrangedSubview(cubbyHoleView)
                NSLayoutConstraint.activate([
                    cubbyHoleView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.5 / CGFloat(dimension)),
                    cubbyHoleView.heightAnchor.constraint(equalTo: cubbyHoleView.widthAnchor)
                ])
            }
        }
        
        containerStackView.transform = CGAffineTransform.init(rotationAngle: -45.0 * CGFloat.pi / 180.0)
    }
}

class CubbyHoleView: UIView {
    let defaultBackgroundColor = UIColor.systemBlue.withAlphaComponent(0.1)
    init() {
        super.init(frame: .zero)
        backgroundColor = defaultBackgroundColor
        layer.borderColor = UIColor.black.cgColor
        layer.borderWidth = 2.0
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        backgroundColor = .systemYellow
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        backgroundColor = defaultBackgroundColor
    }
}



Join cocoa@apple-dev.groups.io to automatically receive all group messages.