UIPanGestureRecognizer - 立即移动视图 [英] UIPanGestureRecognizer - move view instantly

查看:104
本文介绍了UIPanGestureRecognizer - 立即移动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要根据平移手势移动一些视图。
问题是它不会立即发生,就像苹果地图一样,如果我将手指放在一个点上,并将其快速移动到任何方向,视图将会延迟移动,这意味着我将会能够看到我把手指放在上面的点,这就是我不想要的。

All I need it to move some view around based on pan gesture. The problem is that it doesn't happen instantly, just like Apple Maps, if I put down my finger on one point, and move it fast to any direction, the view will move with a delay, meaning I will be able to see the point that I put my finger on, and that is what I don't want.

由于硬件限制可能不可能,因为在模拟器中它工作正常。
或许还有更好的方法,我不知道。

Maybe it is impossible due to hardware limitations since in the simulator it works fine. Or maybe there is a better way that I don't know.

谢谢。

- (IBAction)pan:(UIPanGestureRecognizer *)sender 
{
    CGPoint center = [sender locationInView:sender.view];
    self.myView.center = center; 
}


推荐答案

当我第一次回答时,我假设被拖动的视图有些复杂(例如,一个大的 CALayer 阴影,这可能在计算上很昂贵)。因此我的原始答案如下。但是在随后的评论交流中,已经发现其中一个按钮具有脉冲动画,这更可能是问题。如果拖动性能不是,则在拖动视图期间应暂停动画可以接受的。因此:

When I first answered, I was presuming that there was something complicated about the view being dragged (e.g. a large CALayer shadow, which can be computationally expensive). Hence my original answer below. But in a subsequent exchange of comments, it has been discovered that one of the buttons had an pulsing animation, which is more likely to be the problem. The animation should be suspended during the dragging of the view if the dragging performance is not acceptable. Thus:

#import <QuartzCore/QuartzCore.h>

- (IBAction)pan:(UIPanGestureRecognizer *)gesture
{
    static CGPoint originalCenter;

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        originalCenter = gesture.view.center;
        [self pauseLayer:gesture.view.layer];
    }
    else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        CGPoint translate = [gesture translationInView:gesture.view.superview];
        gesture.view.center = CGPointMake(originalCenter.x + translate.x, originalCenter.y + translate.y);
    }
    else if (gesture.state == UIGestureRecognizerStateEnded ||
             gesture.state == UIGestureRecognizerStateFailed ||
             gesture.state == UIGestureRecognizerStateCancelled)
    {
        [self resumeLayer:gesture.view.layer];
    }
}

-(void)pauseLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}

-(void)resumeLayer:(CALayer*)layer
{
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;
}

显然,这可以与图像的光栅化相结合,如下所述,但我会先尝试暂停动画,如上所述。

Clearly, this can be combined with the rasterization of the image, discussed below, but I would try suspending the animation, like above, first.

原始答案:

如果您拖动的视图非常复杂,您可以尝试栅格化它。

If your view being dragged is at all complicated, you could try rasterizing it.


  1. QuartzCore.framework 添加到目标的带库的二进制文件设置。

然后相应地将源调整为栅格化。

And then adjust the source to rasterize accordingly.

例如:

#import <QuartzCore/QuartzCore.h>

- (IBAction)pan:(UIPanGestureRecognizer *)gesture
{
    static CGPoint originalCenter;

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        originalCenter = gesture.view.center;
        gesture.view.layer.shouldRasterize = YES;
    }
    if (gesture.state == UIGestureRecognizerStateChanged)
    {
        CGPoint translate = [gesture translationInView:gesture.view.superview];
        gesture.view.center = CGPointMake(originalCenter.x + translate.x, originalCenter.y + translate.y);
    }
    if (gesture.state == UIGestureRecognizerStateEnded ||
        gesture.state == UIGestureRecognizerStateFailed ||
        gesture.state == UIGestureRecognizerStateCancelled)
    {
        gesture.view.layer.shouldRasterize = NO;
    }
}

顺便说一句,我建议保存 originalCenter 就像这段代码那样,所以如果你把视图拖得偏离中心一点点,它就不会在你身上猛烈地跳跃。

As an aside, I'd suggest saving the originalCenter like this code does, so that if you grab the view being dragged a little off center, it won't jump jarringly on you.

这篇关于UIPanGestureRecognizer - 立即移动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆