2015-09-28 78 views
1

我创建了屏幕上的圆圈手指时的touchesBegan():创建具有PanGestureRecognizer一个UIView并激活它没有通过手指的位置创建一个UIView提升

在视图控制器:

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) 

} 

这就造成了CIR但是,当我移动手指时,不会立即移动它。相反,我必须拿起手指放回到handlePan()开始前的圆上。

是否有办法开始跟踪平移手势,而不会抬起产生其父视图的手指,同时考虑到可能有多个手指触摸并在屏幕上移动?

回答

0

如果您已经创建的圈子视图成功方法touchesBegan:withEvent:

可以

  1. 让这个圈子查看属性

  2. 此举直接方法圈鉴于touchesMoved:withEvent:

这不会要求你先拿起你的手指

+0

如果有多于一个手指接触怎么办? – marsman12019

1

这里的问题是你使用touchesBegan和UIPanGestureRecognizer。为获得最佳效果,请使用其中一种。如果你打算只用平移手势来做到这一点(这是我会做什么),请执行下列操作:

func handlePan(gesture: UIPanGestureRecognizer) { 
    if gesture.state == UIGestureRecognizerState.Began { 
     //handle creating circle 
    } else if gesture.state == UIGestureRecognizerState.Changed { 
     //handle movement 
    } 
} 

希望这有助于!

+0

这个方法有两个问题/问题:它能够处理多个独立的触摸;它是否能够在屏幕被触摸时立即创建圆形视图,而不是当手指从其初始位置移动时? 我无法做最初的尝试。 – marsman12019

+0

为了使用像这样的手势识别器,您需要将手势直接添加到圆视图本身。也许你可以创建一个轻敲手势(或者一个很短时间的长按手势)来创建圆圈并添加手势,但是你仍然必须将一个手势交给另一个手势。嗯,你可能不得不使用touchesBegan和touchesMoved,或者编写你自己的自定义手势。 –

0

我能够完成多个独立的触摸,通过保持所有活动触摸的字典并在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() 
    } 
}