发生 uiscrollview 事件时未触发 NSTimer

编程入门 行业动态 更新时间:2024-10-27 09:32:51
本文介绍了发生 uiscrollview 事件时未触发 NSTimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 UIImageView 放置在 UIScrollView 中,基本上这个 UIImageView 拥有非常大的地图,并在带有箭头"指向导航方向的预定义路径上创建动画.

I have a UIImageView placed in UIScrollView, Basicly this UIImageView holds very big map, and created animation on a predefined path with "arrows" pointed navigation direction.

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

But, whenever uiscrollevents occurs, I think MainLoop freezes and NSTimer being not fired, and animation stopped.

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

Are there any existing property, which solves this problem, on UIScrollView, CAKeyFrameAnimation or 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,如下所示:

iOS Applications run on an NSRunLoop. Each NSRunLoop has different modes of execution for different tasks. For example, the default nstimer is scheduled to run under the NSDefaultRunMode on the NSRunLoop. What this means however is that certain UIEvents, scrollviewing being one, will interrupt the timer, and place it on a queue to be run as soon as the event stops updating. In your case, in order to get the timer to not be interrupted, you need to schedule it for a different mode, namely NSRunLoopCommonModes, like so:

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

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

This mode will allow your timer to not be interrupted by scrolling. You can find more about this info here: developer.apple/documentation/foundation/nsrunloop At the bottom you will see the definitions of the modes you can choose from. Also, legend has it, you can write your own custom modes, but few have ever lived to tell the tale im afraid.

更多推荐

发生 uiscrollview 事件时未触发 NSTimer

本文发布于:2023-11-06 16:19:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564193.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发生   事件   uiscrollview   NSTimer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!