I'm attempting to build an app using SwiftUI, and having getting stuck on a few things that I can't figure out how to implement.
The current challenge can be described as follows:
1. I am displaying a 9x9 grid
2. when a user taps one of the 'cell's I want to present an input view that only requires a portion of the screen. (I'm currently picturing it being non-modal, but feel free to convince me it should be otherwise)
3. I want the input view to animate its frame starting from the cell the user taps, and ending being a square in the middle of the screen (either a fixed final size, or a fixed final portion of the smaller screen dimension.. or perhaps an intrinsic size?)
Any/all help appreciated.
here is some UIViewController-based code that implements what I want to do
class ViewController: UIViewController {
override func loadView() {
let result = UIView()
result.backgroundColor = UIColor.blue
addButton(superView: result, title: "button1", yMultiplier: 0.8)
addButton(superView: result, title: "button2", yMultiplier: 1.4)
view = result
}
func addButton(superView: UIView, title: String, yMultiplier: CGFloat) {
let button = UIButton()
button.setTitle(title, for: .normal)
button.backgroundColor = UIColor.green
button.addTarget(self, action: #selector(buttonTapped(sender:)), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
superView.addSubview(button)
let centerX = NSLayoutConstraint(item: button, attribute: .centerX, relatedBy: .equal, toItem: superView, attribute: .centerX, multiplier: 1.0, constant: 0)
let centerY = NSLayoutConstraint(item: button, attribute: .centerY, relatedBy: .equal, toItem: superView, attribute: .centerY, multiplier: yMultiplier, constant: 0)
superView.addConstraints([centerX, centerY])
}
@objc func buttonTapped(sender: UIButton) {
let tempView = UIView(frame: sender.frame)
tempView.backgroundColor = UIColor.red
tempView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
tempView.translatesAutoresizingMaskIntoConstraints = true
view.addSubview(tempView)
UIView.animate(withDuration: 1.0, animations: {
tempView.frame.size = CGSize(width: 200, height: 200)
tempView.center = self.view.center
}) { (success) in
tempView.removeFromSuperview()
}
}
}