当用户点按按钮或按住按钮时,我尝试创建按钮 我的期望是当按下按钮时,创建新的UIView,它成为平移手势的第一响应者,并且当用户的触摸被释放时,UIView将从其超级视图中移除。 /> 此外,我的观点集中在用户点击按钮的位置。
现在,我已完成平移并添加视图并将其删除。但我有两个问题:
这是我的代码:
private let myButton: UIButton = {
let button = UIButton(type: UIButtonType.system)
button.layer.cornerRadius = 4
button.addTarget(self, action: #selector(addView), for: UIControlEvents.touchDown)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
private let myCircularUIView: UIView = {
let imageView = UIView()
imageView.backgroundColor = UIColor.blue
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
@objc private func addView() {
view.addSubview(myCircularUIView)
view.becomeFirstResponder()
myCircularUIView.frame = CGRect(origin: translation, size: CGSize(width: 100, height: 100))
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panGesture(recognizer:)))
self.view.addGestureRecognizer(gestureRecognizer)
}
private var translation = CGPoint(x: 0, y: 0)
@objc private func panGesture(recognizer: UIPanGestureRecognizer) {
translation = recognizer.translation(in: self.view)
myCircularUIView.transform = CGAffineTransform(translationX: translation.x, y: translation.y)
}
如果您需要任何额外信息,请告诉我而不是低估。