使用 PanGestureRecognizer 创建 UIView 并在不抬起手指的情况下激活它
Creating a UIView with PanGestureRecognizer and activating it without lifting the finger
我在 touchesBegan():
时在手指位置创建了一个 UIView,从而在屏幕上创建了一个圆圈
在ViewController中:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let pos = touch.locationInView(view)
let radius: CGFloat = touch.majorRadius * 2
let circleView = CircleView(frame: CGRectMake(pos.x - radius / 2, pos.y - radius / 2, radius, radius))
view.addSubview(circleView)
}
}
在 CircleView 中:
override init(frame: CGRect) {
super.init(frame: frame)
let recognizer = UIPanGestureRecognizer(target: self, action: Selector("handlePan:"))
recognizer.delegate = self
self.addGestureRecognizer(recognizer)
}
这创建了圆圈,但当我移动手指时它并没有立即移动。相反,在 handlePan() 启动之前,我必须抬起手指并将其放回圆圈上。
考虑到可能有多个手指在屏幕上触摸和移动,是否可以在不抬起生成其父视图的手指的情况下开始跟踪平移手势?
如果您已经在方法touchesBegan:withEvent:
中成功创建了圆视图
你可以
使该圆视图成为 属性
直接在方法中移动那个圆视图touchesMoved:withEvent:
这不需要您先拿起手指
这里的问题是您同时使用了 touchesBegan 和 UIPanGestureRecognizer。为获得最佳效果,请使用其中一种。如果您只想使用平移手势(我会这样做),请执行以下操作:
func handlePan(gesture: UIPanGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.Began {
//handle creating circle
} else if gesture.state == UIGestureRecognizerState.Changed {
//handle movement
}
}
希望对您有所帮助!
通过保留所有活动触摸的字典并在 touchesBegan() 中创建圆视图并通过查询该字典更新它们在 touchesMoved() 中的位置,我能够完成多个独立的触摸。
var fingerTouches = [UITouch: CircleView]()
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let pos = touch.locationInView(view)
let radius: CGFloat = touch.majorRadius * 2
let circle = CircleView(frame: CGRectMake(pos.x - radius / 2, pos.y - radius / 2, radius, radius))
view.addSubview(circle)
fingerTouches[touch] = circle
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let pos = touch.locationInView(view)
let radius: CGFloat = touch.majorRadius * 2
fingerTouches[touch]!.frame = CGRectMake(pos.x - radius / 2, pos.y - radius / 2, radius, radius)
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
fingerTouches[touch]!.removeFromSuperview()
}
}
我在 touchesBegan():
时在手指位置创建了一个 UIView,从而在屏幕上创建了一个圆圈在ViewController中:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let pos = touch.locationInView(view)
let radius: CGFloat = touch.majorRadius * 2
let circleView = CircleView(frame: CGRectMake(pos.x - radius / 2, pos.y - radius / 2, radius, radius))
view.addSubview(circleView)
}
}
在 CircleView 中:
override init(frame: CGRect) {
super.init(frame: frame)
let recognizer = UIPanGestureRecognizer(target: self, action: Selector("handlePan:"))
recognizer.delegate = self
self.addGestureRecognizer(recognizer)
}
这创建了圆圈,但当我移动手指时它并没有立即移动。相反,在 handlePan() 启动之前,我必须抬起手指并将其放回圆圈上。
考虑到可能有多个手指在屏幕上触摸和移动,是否可以在不抬起生成其父视图的手指的情况下开始跟踪平移手势?
如果您已经在方法touchesBegan:withEvent:
你可以
使该圆视图成为 属性
直接在方法中移动那个圆视图
touchesMoved:withEvent:
这不需要您先拿起手指
这里的问题是您同时使用了 touchesBegan 和 UIPanGestureRecognizer。为获得最佳效果,请使用其中一种。如果您只想使用平移手势(我会这样做),请执行以下操作:
func handlePan(gesture: UIPanGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.Began {
//handle creating circle
} else if gesture.state == UIGestureRecognizerState.Changed {
//handle movement
}
}
希望对您有所帮助!
通过保留所有活动触摸的字典并在 touchesBegan() 中创建圆视图并通过查询该字典更新它们在 touchesMoved() 中的位置,我能够完成多个独立的触摸。
var fingerTouches = [UITouch: CircleView]()
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let pos = touch.locationInView(view)
let radius: CGFloat = touch.majorRadius * 2
let circle = CircleView(frame: CGRectMake(pos.x - radius / 2, pos.y - radius / 2, radius, radius))
view.addSubview(circle)
fingerTouches[touch] = circle
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let pos = touch.locationInView(view)
let radius: CGFloat = touch.majorRadius * 2
fingerTouches[touch]!.frame = CGRectMake(pos.x - radius / 2, pos.y - radius / 2, radius, radius)
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
fingerTouches[touch]!.removeFromSuperview()
}
}