gpt4 book ai didi

ios - UIPanGestureRecognizer - 立即移动 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:08:52 25 4
gpt4 key购买 nike

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

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

谢谢。


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

最佳答案

当我第一次回答时,我假设被拖动的 View 有些复杂(例如,一个大的 CALayer 阴影,这在计算上可能很昂贵)。因此,我的原始答案如下。但在随后的评论交流中,发现其中一个按钮有一个脉冲动画,这更有可能是问题所在。 animation should be suspended如果拖动性能 Not Acceptable ,则在拖动 View 期间。因此:

#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;
}

显然,这可以与下面讨论的图像光栅化相结合,但我会先像上面那样尝试暂停动画。


原答案:

如果被拖动的 View 非常复杂,您可以尝试对其进行栅格化。

  1. QuartzCore.framework 添加到目标的 "Link binary with libraries"设置。

  2. 然后相应地调整源以进行光栅化。

如:

#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,这样如果您抓取被拖离中心一点的 View ,它就不会突然跳到您身上。

关于ios - UIPanGestureRecognizer - 立即移动 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14264804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com