按钮图像变化缓慢 [英] Button image change slow

查看:32
本文介绍了按钮图像变化缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个奇怪的错误,我创建了一个由uibutton组成的自定义分段控件(该控制器本身是UIView的子类.我更改了所选图像和正常控件状态的按钮图像以指示选择.

在IB的父视图中,我已将视图的标识设置为我的自定义类.问题是我有一个tableView,当我滚动它和滚动动画时,自定义视图中的按钮不会立即更改其图像,而是等待表完成滚动然后更新.有任何想法吗?

解决方案

您并没有提供太多细节,但它听起来听起来,就好像您正在尝试在该页面中做太多工作一样当前运行循环.如果是这样,那么答案将是相关的:

iOS会等到您的代码执行完毕后再进行任何显示更新.因此,所有更新实际上都会排队,直到您当前的代码块完成为止.为了解决这个问题,最常见的技巧是让当前的运行循环结束,并在很短的延迟后再次执行执行.

因此,在您的情况下,请调用代码以更新您的自定义细分控件.然后,不要调用该代码来更新表,而是将该代码停放在另一个方法中,然后使用 [self performSelector:@selector(delayedUpdate)withObject:nil afterDelay:0.1]; 调用该方法.>

为说明如何更改代码:

之前

  [self updateSegmentController];[self updateTableScrollPosition];返回; 

之后

  [self updateSegmentController];[自我performSelector:@selector(updateTableScrollPosition)withObject:零后延迟:0.1];返回; 

通常延迟为0.0可以很好地工作;它仍然可以达到在调用指定方法之前完成当前运行循环和更新显示的效果.有时添加额外的延迟可以改善动画外观.

I am dealing with and odd bug, I have created a custom segmented control that comprises of uibuttons (the controller itself is a subclass of UIView. I change the button's images for the selected and normal control state to indicate selection.

In my parent view in IB I have set the identity of the view to my custom class. The issue is I have a tableView, when I scroll it and the scroll animation the button inside my custom view does not change its image immediately, instead it waits for the table to finish scrolling and then it updates. Any ideas?

解决方案

You're not giving a lot of detail, but it sounds as if you're trying to do too much work within the current run loop. If so then the answer will be relevant:

iOS waits until your code has finished executing before it does any display updating. So any updates are, in effect, queued until your current chunk of code is completed. To get around this, the most common trick is to allow the current run loop to end and the pick up execution again after a very short delay.

So, in your case, call the code to update your custom segment control. And then, instead of calling the code to update your table, park that code in another method and call that method using [self performSelector: @selector(delayedUpdate) withObject: nil afterDelay:0.1];

To illustrate how you might change code:

BEFORE

 [self updateSegmentController];
 [self updateTableScrollPosition];
 return;

AFTER

 [self updateSegmentController];
 [self performSelector: @selector(updateTableScrollPosition) withObject: nil afterDelay: 0.1];
 return;

Often a delay of 0.0 works just fine; it still achieves the effect of letting the current run loop complete and the display update before calling the nominated method. Sometimes adding an extra delay improves your animation appearance.

这篇关于按钮图像变化缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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