使用 UITapGestureRecognizer 点击后如何保护 UIView
how to safe UIView after tapped with UITapGestureRecognizer
我尝试为屏幕上的每次点击添加 UIView,但是一旦我点击了 UIView,但我无法在屏幕上保护他,当我取消点击时他就消失了。
我应该怎么做才能在屏幕上安全地显示 UIView,然后点击一次以添加第二个 UIView 并保存我的每个 UIView?
import UIKit
class ViewController: UIViewController {
// MARK: - Property
let circle: UIView = {
let circle = UIView()
circle.frame.size.height = 100
circle.frame.size.width = 100
circle.layer.borderWidth = 10
circle.layer.borderColor = UIColor.white.cgColor
circle.layer.cornerRadius = 50
return circle
}()
// MARK: - LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
addRecognizer()
view.backgroundColor = .red
}
// MARK: - Methods
@IBAction func tappedRecognizer(_ recognizer: UITapGestureRecognizer) {
let tapLocation = recognizer.location(in: view)
let circleHeight = circle.frame.size.height
let circleWidth = circle.frame.size.width
print(tapLocation)
circle.frame.origin = .init(x: tapLocation.x - circleWidth/2, y: tapLocation.y - circleHeight/2)
view.addSubview(circle)
}
func addRecognizer() {
let recognizer = UITapGestureRecognizer(target: self, action: #selector(tappedRecognizer(_:)))
self.view.addGestureRecognizer(recognizer)
}
}
在您的 class 中添加数组和创建新圈子的方法:
class ViewController: UIViewController {
// MARK: - Property
var circles = [UIView]()
func newCircle(atLocation tapLocation: CGPoint) -> UIView {
let circle = UIView()
circle.frame.size.height = 100
circle.frame.size.width = 100
circle.layer.borderWidth = 10
circle.layer.borderColor = UIColor.white.cgColor
circle.layer.cornerRadius = 50
circle.frame.origin = .init(x: tapLocation.x - circle.frame.size.width/2,
y: tapLocation.y - circle.frame.size.height/2)
circles.append(circle)
return circle
}
@IBAction func tappedRecognizer(_ recognizer: UITapGestureRecognizer) {
let tapLocation = recognizer.location(in: view)
print(tapLocation)
let circle = newCircle(atLocation: tapLocation)
view.addSubview(circle)
}
}
另一种方法是使用计算 属性(这可能是您尝试实现的)。为此,只需从 属性 定义中删除 = :
let circle: UIView { /: no =
get {
let circle = UIView()
circle.frame.size.height = 100
circle.frame.size.width = 100
circle.layer.borderWidth = 10
circle.layer.borderColor = UIColor.white.cgColor
circle.layer.cornerRadius = 50
return circle
}
} // no ()
我尝试为屏幕上的每次点击添加 UIView,但是一旦我点击了 UIView,但我无法在屏幕上保护他,当我取消点击时他就消失了。 我应该怎么做才能在屏幕上安全地显示 UIView,然后点击一次以添加第二个 UIView 并保存我的每个 UIView?
import UIKit
class ViewController: UIViewController {
// MARK: - Property
let circle: UIView = {
let circle = UIView()
circle.frame.size.height = 100
circle.frame.size.width = 100
circle.layer.borderWidth = 10
circle.layer.borderColor = UIColor.white.cgColor
circle.layer.cornerRadius = 50
return circle
}()
// MARK: - LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
addRecognizer()
view.backgroundColor = .red
}
// MARK: - Methods
@IBAction func tappedRecognizer(_ recognizer: UITapGestureRecognizer) {
let tapLocation = recognizer.location(in: view)
let circleHeight = circle.frame.size.height
let circleWidth = circle.frame.size.width
print(tapLocation)
circle.frame.origin = .init(x: tapLocation.x - circleWidth/2, y: tapLocation.y - circleHeight/2)
view.addSubview(circle)
}
func addRecognizer() {
let recognizer = UITapGestureRecognizer(target: self, action: #selector(tappedRecognizer(_:)))
self.view.addGestureRecognizer(recognizer)
}
}
在您的 class 中添加数组和创建新圈子的方法:
class ViewController: UIViewController {
// MARK: - Property
var circles = [UIView]()
func newCircle(atLocation tapLocation: CGPoint) -> UIView {
let circle = UIView()
circle.frame.size.height = 100
circle.frame.size.width = 100
circle.layer.borderWidth = 10
circle.layer.borderColor = UIColor.white.cgColor
circle.layer.cornerRadius = 50
circle.frame.origin = .init(x: tapLocation.x - circle.frame.size.width/2,
y: tapLocation.y - circle.frame.size.height/2)
circles.append(circle)
return circle
}
@IBAction func tappedRecognizer(_ recognizer: UITapGestureRecognizer) {
let tapLocation = recognizer.location(in: view)
print(tapLocation)
let circle = newCircle(atLocation: tapLocation)
view.addSubview(circle)
}
}
另一种方法是使用计算 属性(这可能是您尝试实现的)。为此,只需从 属性 定义中删除 = :
let circle: UIView { /: no =
get {
let circle = UIView()
circle.frame.size.height = 100
circle.frame.size.width = 100
circle.layer.borderWidth = 10
circle.layer.borderColor = UIColor.white.cgColor
circle.layer.cornerRadius = 50
return circle
}
} // no ()