【问题标题】:Why does my UIButton sometimes not react instantly when pressed?为什么我的 UIButton 在按下时有时不会立即做出反应?
【发布时间】:2014-08-09 23:20:33
【问题描述】:

我开发了一个具有两个 UIButton 的 iOS 应用程序,但是当我按下它们时,这些按钮有时似乎会滞后。有时滞后非常糟糕(有时需要 10 秒)。我几乎可以肯定它与我正在使用的 NSTimer 有关。我只想做到这一点,以便在按下按钮后立即切换 UIViewController,我根本不希望有任何延迟。这是我的代码:

RealTimeModeViewController.m

#import "RealTimeModeViewController.h"

@interface RealTimeModeViewController ()

@end

@implementation RealTimeModeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [NSTimer scheduledTimerWithTimeInterval:1.0
                                     target:self
                                   selector:@selector(UpdateTime:)
                                   userInfo:nil
                                    repeats:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)UpdateTime:(id)sender
{
    // This is where I do everything in my app
}

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [NSTimer scheduledTimerWithTimeInterval:1.0
                                     target:self
                                   selector:@selector(UpdateTime:)
                                   userInfo:nil
                                    repeats:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)UpdateTime:(id)sender
{
    // Most of code goes here
}

@end

两个 UIButton 通过 Modal 样式连接到彼此的 ViewController。也许我应该把按钮放在主线程中?我对线程不是很熟悉。谢谢。

【问题讨论】:

  • 你在 - (void)UpdateTime:(id)sender 做什么?你能展示在视图控制器之间切换的代码吗?

标签: ios uiviewcontroller uibutton


【解决方案1】:

如果我不得不猜测,您正在 UpdateTime: 方法中做一些耗时的事情。因为它在主线程上并且每 1 秒运行一次,所以您可能会减慢其他所有操作的速度。 UIButton 事件在主线程上,所以很可能因为你在主线程上做所有事情,它被“阻塞”了。

对于不太准确但不会挂起主线程的计时器实现,请参阅此答案:https://stackoverflow.com/a/8304825/3708242

如果您必须具有 NSTimer 实现的一致性,请尝试减少 UpdateTime: 所做的工作量。

【讨论】:

    【解决方案2】:

    我以前遇到过这个问题。你应该使用UIActivityIndicatorView

    按钮延迟可能是UpdateTime方法引起的。在ViewWillAppearViewDidLoad 中使用UIActivityIndicatorView 然后执行UpdateTime 方法。

    这就是我的意思:

    -(void) ViewDidLoad
    {
    UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
        [act setFrame:CGRectMake(320/2-50, 130, 100, 100)];
        act.layer.cornerRadius = 10;
        [act.layer setBackgroundColor:[[UIColor colorWithWhite: 0.0 alpha:0.30] CGColor]];
    
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, act.frame.size.width, 20)];
        [lable setText:[NSString stringWithFormat:@"%@", string]];
        [lable setFont:[UIFont fontWithName:@"Helvetica-Light" size:12.0f]];
        [lable setTextAlignment:NSTextAlignmentCenter];
        [lable setTextColor:[UIColor whiteColor]];
        [act addSubview:lable];
        [self.view addSubview: act];
        [act startAnimating];
    
        // perform the method here, and set your delay time..
        [self performSelector:@selector(UpdateTime:) withObject:nil afterDelay:1.0];
    
    }
    

    那么……

    - (void)UpdateTime:(id)sender
    {
        // Most of your code
    }
    

    评论并告诉我这是否有帮助:)

    【讨论】:

      猜你喜欢
      • 2015-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-27
      • 1970-01-01
      • 2023-03-19
      相关资源
      最近更新 更多