setNeedsDisplay 在块内不起作用

发布于 2024-12-19 00:05:07 字数 660 浏览 3 评论 0原文

我正在使用 CMMotionManager 来检索加速度计数据。问题是加速度计数据会定期打印,视图中的实例变量会更改,但视图不会重新绘制。我已经检查过 hv 不为零并且所有内容都已挂钩。在块内调用 setNeedsDisplay 是否有问题?

-(void) viewDidAppear:(BOOL) animated
{
    [super viewDidAppear: animated];

    [motionManager startAccelerometerUpdatesToQueue:motionQueue withHandler:
     ^(CMAccelerometerData *accelerometerData, NSError *error)
    {

        NSLog(@"%@",accelerometerData);

        HypnosisView *hv = (HypnosisView *) [self view];

        hv.xShift = 10.0 * accelerometerData.acceleration.x;
        hv.yShift = -10.0 * accelerometerData.acceleration.y;

        [hv setNeedsDisplay];

    }];    
}

I'm using CMMotionManager for retrieving accelerometer data. The thing is that the accelerometer data gets printed periodically, the instance variables are changed in the view, but the view doesn't get redrawn. I have checked that hv is not nil and that everything is hooked. Is there a problem with calling setNeedsDisplay within a block?

-(void) viewDidAppear:(BOOL) animated
{
    [super viewDidAppear: animated];

    [motionManager startAccelerometerUpdatesToQueue:motionQueue withHandler:
     ^(CMAccelerometerData *accelerometerData, NSError *error)
    {

        NSLog(@"%@",accelerometerData);

        HypnosisView *hv = (HypnosisView *) [self view];

        hv.xShift = 10.0 * accelerometerData.acceleration.x;
        hv.yShift = -10.0 * accelerometerData.acceleration.y;

        [hv setNeedsDisplay];

    }];    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔少女心 2024-12-26 00:05:07

这是因为您在与主线程不同的线程上调用 UI 方法。

将其添加到您的块中:

dispatch_async(dispatch_get_main_queue(), ^{
    [hv setNeedsDisplay];
});

请记住,任何处理用户界面元素的方法都必须从主线程调用。

It's because your calling a UI method on a thread different from the main thread.

Add this to your block:

dispatch_async(dispatch_get_main_queue(), ^{
    [hv setNeedsDisplay];
});

Remember that any method dealing with user interface elements must be called from the main thread.

回首观望 2024-12-26 00:05:07

我在其他块中做了同样的事情,它确实有效,尽管不是与您在这里使用的回调一起使用。也许该块没有在主线程上执行?您可以通过以下方式进行检查:

NSLog(@"Main thread? %d", [NSThread isMainThread]);

如果不是,您可以强制 setNeedsDisplay 在主线程上运行。

I have done the same in other blocks and it did work, though not with the callback you use here. Maybe the block is not executed on the main thread? You can check that with:

NSLog(@"Main thread? %d", [NSThread isMainThread]);

If it is not, you could force setNeedsDisplay to run on the main thread.

囍笑 2024-12-26 00:05:07

只是想测试@cocoahero 的答案。我尝试在非主线程中为 UIButton 调用 setTitle 。标题也确实改变了。我尝试使用 Xcode 5.0.1 针对 iOS 5.1 和 7.0。我还从非主线程调用了setNeedsDisplay,它也起作用了。

[NSThread isMainThread] 是我确保我的调用不是来自主线程的方法。我不确定从非主线程调用是导致您的问题的原因,但至少还有其他可能性。您可以查看我的回答来了解另一个问题。

Just wanted to test @cocoahero's answer. I tried to call setTitle for a UIButton in a non main thread. And the title did changed. I tried this both targeting iOS 5.1 and 7.0 using Xcode 5.0.1. Also I called setNeedsDisplay from a non main thread and it worked too.

[NSThread isMainThread] was how I made sure that my calls were not from main thread. I am not sure calling from non main thread was the cause to your problem but at least there are other possibilities there. You can take a look at my answer for another question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文