Ios 使用PangestureRecognitor创建UIView并在不抬起手指的情况下激活它
我在触摸开始()时在手指位置创建UIView,从而在屏幕上创建了一个圆圈: 在ViewController中: 这将创建圆,但在我移动手指时不会立即移动它。相反,在handlePan()开始工作之前,我必须抬起手指并将其放回圆圈上Ios 使用PangestureRecognitor创建UIView并在不抬起手指的情况下激活它,ios,iphone,swift,uiview,uigesturerecognizer,Ios,Iphone,Swift,Uiview,Uigesturerecognizer,我在触摸开始()时在手指位置创建UIView,从而在屏幕上创建了一个圆圈: 在ViewController中: 这将创建圆,但在我移动手指时不会立即移动它。相反,在handlePan()开始工作之前,我必须抬起手指并将其放回圆圈上 考虑到可能有多个手指在屏幕上触摸和移动,有没有一种方法可以在不抬起生成其父视图的手指的情况下开始跟踪平移手势?如果您已经在方法touchsbegind:withEvent: 你可以 使该圆视图成为属性 直接在方法触摸移动:withEvent: 这不需要您先拿起手指这里
考虑到可能有多个手指在屏幕上触摸和移动,有没有一种方法可以在不抬起生成其父视图的手指的情况下开始跟踪平移手势?如果您已经在方法
touchsbegind:withEvent:
你可以
触摸移动:withEvent:
这不需要您先拿起手指这里的问题是您同时使用ToucheSStart和UIPangestureRecognitor。为获得最佳效果,请使用其中一种。如果你只想做一个平移手势(我也会这么做),请执行以下操作:
func handlePan(gesture: UIPanGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.Began {
//handle creating circle
} else if gesture.state == UIGestureRecognizerState.Changed {
//handle movement
}
}
希望这有帮助 通过保留所有活动触摸的字典,在touchesbearth()中创建圆圈视图,并通过查询字典更新它们在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()
}
}
var fingerTouchs=[UITouch:CircleView]()
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
接触{
let pos=触摸位置查看(视图)
让半径:CGFloat=touch.majorRadius*2
让圆=圆形视图(框架:CGRectMake(位置x-半径/2,位置y-半径/2,半径,半径))
视图。添加子视图(圆形)
手指触摸[触摸]=圆圈
}
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
接触{
let pos=触摸位置查看(视图)
让半径:CGFloat=touch.majorRadius*2
FingerTouchs[touch]!.frame=CGRectMake(位置x-半径/2,位置y-半径/2,半径,半径)
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
接触{
FingerTouchs[触摸]!.removeFromSuperview()
}
}
如果有多个手指触摸怎么办?此方法有两个问题:是否能够处理多个独立触摸;它是否能够在触摸屏幕时立即创建圆形视图,而不是在手指从初始位置移动时创建圆形视图?我一开始也做不到。为了像这样使用手势识别器,你需要将手势直接添加到圆圈视图本身。也许您可以创建一个轻触手势(或一个时间很短的长按手势)来创建圆圈并添加手势,但是您仍然需要将一个手势交给另一个。嗯,您可能需要使用TouchsBegind和TouchsMoved,或者编写自己的自定义手势。
func handlePan(gesture: UIPanGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.Began {
//handle creating circle
} else if gesture.state == UIGestureRecognizerState.Changed {
//handle movement
}
}
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()
}
}