我想在长时间的压力手势后创建圆圈,并使其在屏幕上仍按手指的同时以平移手势移动。当手指从屏幕上移开时,圆圈应该从屏幕上移开。用长按手势创建圆圈效果很好,只是它不动。代码如下:
@IBAction func spaceLongPressed(_ guesture: UILongPressGestureRecognizer) {
let location = guesture.location(in: space)
var panGesture = UIPanGestureRecognizer()
circleView = UIView(frame: CGRect(x: location.x - space.bounds.height/4, y: location.y - space.bounds.height/4, width: space.bounds.height/2, height: space.bounds.height/2))
if guesture.state == .began {
circleView.backgroundColor = UIColor.red
circleView.isUserInteractionEnabled = true
circleView.tag = 100
circleView.layer.cornerRadius = circleView.bounds.height/2
space.addSubview(circleView)
panGesture = UIPanGestureRecognizer(target: space, action: #selector(didPan(sender:)))
space.addGestureRecognizer(panGesture)
}
if guesture.state == .ended {
if let viewWithTag = self.view.viewWithTag(100) {
viewWithTag.removeFromSuperview()
}
}
}
@objc func didPan(sender: UIPanGestureRecognizer) {
if sender.state == .began {
let translation = sender.translation(in: sender.view)
let changeX = (sender.view?.center.x)! + translation.x
let changeY = (sender.view?.center.y)! + translation.y
sender.view?.center = CGPoint(x: changeX, y: changeY)
sender.setTranslation(CGPoint.zero, in: sender.view)
}
}
对象“空间”属于 UIButton 类型 - 我想在按钮中创建圆圈。