gpt4 book ai didi

ios - uiscrollview 事件发生时 NSTimer 未触发

转载 作者:IT王子 更新时间:2023-10-29 08:21:41 25 4
gpt4 key购买 nike

我在 UIScrollView 中放置了一个 UIImageView,基本上这个 UIImageView 包含非常大的 map ,并在带有“箭头”指向导航方向的预定义路径上创建动画。

但是,每当发生 uiscrollevents 时,我认为 MainLoop 会卡住并且 NSTimer 不会被触发,并且动画会停止。

在 UIScrollView、CAKeyFrameAnimation 或 NSTimer 上是否有解决此问题的现有属性?

//viewDidLoad
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(drawLines:) userInfo:nil repeats:YES];

- (void)drawLines:(NSTimer *)timer {

CALayer *arrow = [CALayer layer];
arrow.bounds = CGRectMake(0, 0, 5, 5);
arrow.position = CGPointMake(line.x1, line.y1);
arrow.contents = (id)([UIImage imageNamed:@"arrow.png"].CGImage);

[self.contentView.layer addSublayer:arrow];

CAKeyframeAnimation* animation = [CAKeyframeAnimation animation];
animation.path = path;
animation.duration = 1.0;
animation.rotationMode = kCAAnimationRotateAuto; // object auto rotates to follow the path
animation.repeatCount = 1;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.fillMode = kCAFillModeForwards;

[arrow addAnimation:animation forKey:@"position"];
}

最佳答案

iOS 应用程序在 NSRunLoop 上运行。每个 NSRunLoop 对于不同的任务都有不同的执行模式。例如,默认 nstimer 被安排在 NSRunLoop 上的 NSDefaultRunMode 下运行。然而,这意味着某些 UIEvents(scrollviewing 是其中之一)将中断计时器,并将其置于队列中,以便在事件停止更新时立即运行。在您的情况下,为了让计时器不被中断,您需要将其安排为不同的模式,即 NSRunLoopCommonModes,如下所示:

  self.myTimer =  [NSTimer scheduledTimerWithTimeInterval:280
target:self
selector:@selector(doStuff)
userInfo:nil
repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:self.myTimer forMode:NSRunLoopCommonModes];

此模式将使您的计时器不会因滚动而中断。您可以在此处找到有关此信息的更多信息:https://developer.apple.com/documentation/foundation/nsrunloop在底部,您将看到可以选择的模式的定义。此外,传说有它,您可以编写自己的自定义模式,但恐怕很少有人能活着讲述这个故事。

关于ios - uiscrollview 事件发生时 NSTimer 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9090658/

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