English 中文(简体)
为什么使用 UIPanGesture 识别器移动对象会延迟?
原标题:Why is there a delay when moving object using UIPanGestureRecognizer?

我将 UIUView 对象移动到 UIPanGestureRecognizer - 我将手指拖到屏幕上多少,我将视图移到同一个方向(仅在X - 左或右,Y没有改变) 。它工作良好,但(非常明显) 延缓

以下是处理 UIPanGestureRecognizer 事件的方法 :

-(void)movePages:(UIPanGestureRecognizer *)sender
{
    if (switchingMode == 1) {
        if ([sender state] == UIGestureRecognizerStateBegan) {
            fingerStartPosition = [sender locationInView:self.view].x;
            viewStartPosition = [[viewControllers objectAtIndex:activeViewControllerIndex] view].center;
        }
        [[[[viewControllers objectAtIndex:activeViewControllerIndex] view] layer] setPosition:CGPointMake(viewStartPosition.x - (fingerStartPosition - [sender locationInView:self.view].x) , viewStartPosition.y)];            
    }
}

我试图用它的层来设置视图的位置, 我也尝试设置框架, 使用不同长度的动画, 但一切都一样。 知道为什么会延迟吗?

最佳回答

使用 UILressPressGestureRecognize 并将 minimpressDuration 设置为 0.0 。 这会立即识别所有相同的更新, 包括更新位置的 UIGestureRecognize State Changed

问题回答

我发现,如果你只使用普通的触摸Began、移动和结束,反应会更快。我甚至将UIGESDERRecognizer分类,它仍然落后于跨行姿态。即使UIGESDERRECOTIZ内部的触摸Began会及时触发,但国家变化需要半秒时间才能改变现状。 使用普通的触摸Began似乎更快,特别是如果你重新做一些事情的话。

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
    if touches.count == 1
    {
        initialTouchLocation = (touches.first?.locationInView(self).x)!
    }
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)
{
    if touches.count == 1
    {
        let locationInView = touches.first?.locationInView(self)
        if !thresholdHit
        {
            //this is the threshold for x movement in order to trigger the panning...
            if abs(initialTouchLocation - locationInView!.x) > 1
            {
                thresholdHit = true
            }
        }
        else
        {
            if (self.frame.width != CGFloat(screenSize))
            {
                let panDelta = initialTouchLocation - locationInView!.x
            }
        }
    }
}

手势识别器不能确定,如果这是平板手势的话, 在你动动手指之前, 有些像素。 我不知道确切的容积值, 但这就是为什么你觉得延迟。

文件:

跨越的手势是连续的。它开始于允许的最小手指数 已经足够移动, 足以被视为一个锅。

如果您想要即时移动, 可能需要使用 touchesMoved: 构建自己的逻辑 。

Another approach could be, to animate to the first recognized point. But that doesn t remove the delay. For that approach you could have a look at my JDDroppableView on github.





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签