我想知道如何才能做到这一点,我非常希望全屏成为 4 个巨大的按钮。
例如,
(左下角将向左行走)和(右下角将向右行走)
(左上角将向左跳转)和(右上角将向右跳转)
我目前有两个巨大的按钮,左边是向左跳转,右边是向右跳转。 谢谢!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if(location.x > self.frame.size.width/2){
jumpRight()
}
else if(location.x < self.frame.size.width/2){
jumpLeft()
}
}
}
最佳答案
所以我整理了一个小类(class)来帮助你。我添加了一些文档来帮助您了解正在发生的事情。
/// The cases of this enum correspond to the four cartesian quadrants.
enum Quadrant: CaseIterable {
case one
case two
case three
case four
}
/// Includes four methods corresponding to each quadrant. Methods are called when touches are registerd by the `FourButtonView`.
protocol FourButtonViewDelegate {
func quadrantITouched(sender: UITapGestureRecognizer)
func quadrantIITouched(sender: UITapGestureRecognizer)
func quadrantIIITouched(sender: UITapGestureRecognizer)
func quadrantIVTouched(sender: UITapGestureRecognizer)
}
/// This view has four quadrants corresponding to the four quadrants of the cartesian plane.
class FourButtonView: UIView {
var delegate: FourButtonViewDelegate?
/// Calls the `FourButtonViewDelegate`'s `quadrantITouched(sender: UITapGestureRecognizer)` method.
/// - Parameter sender: Sender is the `UITapGestureRecognizer` from the button that was touched.
@objc private func iTouched(sender: UITapGestureRecognizer) { delegate?.quadrantITouched(sender: sender) }
/// Calls the `FourButtonViewDelegate`'s `quadrantITouched(sender: UITapGestureRecognizer)` method.
/// - Parameter sender: Sender is the `UITapGestureRecognizer` from the button that was touched.
@objc private func iiTouched(sender: UITapGestureRecognizer) { delegate?.quadrantIITouched(sender: sender) }
/// Calls the `FourButtonViewDelegate`'s `quadrantITouched(sender: UITapGestureRecognizer)` method.
/// - Parameter sender: Sender is the `UITapGestureRecognizer` from the button that was touched.
@objc private func iiiTapped(sender: UITapGestureRecognizer) { delegate?.quadrantIIITouched(sender: sender) }
/// Calls the `FourButtonViewDelegate`'s `quadrantITouched(sender: UITapGestureRecognizer)` method.
/// - Parameter sender: Sender is the `UITapGestureRecognizer` from the button that was touched.
@objc private func ivTouched(sender: UITapGestureRecognizer) { delegate?.quadrantIVTouched(sender: sender) }
// MARK: Sets up a button for every quadrant.
override init(frame: CGRect) {
super.init(frame: frame)
for quadrant in Quadrant.allCases {
setupButtons(quadrant: quadrant)
}
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/// `setupButtons` sets up a button for a given quadrant.
/// - Parameter quadrant: `quadrant` is a case of the `Quadrant` enum. It determines which button to set up.
private func setupButtons(quadrant: Quadrant) {
/// The button to be set up.
let button: UIButton = getButton()
/// The `backgroundColor` of the button.
let backgroundColor: UIColor
/// The `topAnchor` of the button.
let top: NSLayoutYAxisAnchor
/// The `leftAnchor` of the button.
let left: NSLayoutXAxisAnchor
/// The `bottomAnchor` of the button.
let bottom: NSLayoutYAxisAnchor
/// The `rightAnchor` of the button.
let right: NSLayoutXAxisAnchor
// MARK: Uses `quadrant` to determine top and bottom anchors.
if quadrant == .one || quadrant == .two {
top = topAnchor
bottom = centerYAnchor
} else {
top = centerYAnchor
bottom = bottomAnchor
}
// MARK: Uses `quadrant` to determine left and right anchors.
if quadrant == .one || quadrant == .four {
left = centerXAnchor
right = rightAnchor
} else {
left = leftAnchor
right = centerXAnchor
}
// MARK: Uses `quadrant` to determine color and `#selector(@objc method)` method.
switch quadrant {
case .one:
backgroundColor = .red
button.addTarget(self, action: #selector(iTouched), for: .allTouchEvents)
case .two:
backgroundColor = .blue
button.addTarget(self, action: #selector(iTouched), for: .allTouchEvents)
case .three:
backgroundColor = .yellow
button.addTarget(self, action: #selector(iTouched), for: .allTouchEvents)
case .four:
backgroundColor = .green
button.addTarget(self, action: #selector(iTouched), for: .allTouchEvents)
}
// MARK: Sets `backgroundColor` of button.
button.backgroundColor = backgroundColor
// MARK: Activates layout constraints.
NSLayoutConstraint.activate([
button.topAnchor.constraint(equalTo: top),
button.leftAnchor.constraint(equalTo: left),
button.bottomAnchor.constraint(equalTo: bottom),
button.rightAnchor.constraint(equalTo: right)
])
}
/// This function returns a `UIButton` that is prepared for setup with auto layout.
private func getButton() -> UIButton {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
return button
}
}
关于swift - 我正在制作一个 ios 应用程序,我想将屏幕分成 4 个象限/按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59367164/