簡體   English   中英

為什么我的UIButton有時在按下時有時無法立即反應?

[英]Why does my UIButton sometimes not react instantly when pressed?

我開發了一個有兩個UIButton的iOS應用,但是當我按下它們時,這些按鈕有時顯得滯后。 有時滯后真的很嚴重(有時需要10秒)。 我幾乎肯定,這與我使用的NSTimer有關。 我只是想做到這一點,所以只要按下按鈕就可以切換UIViewControllers,我根本不希望有任何延遲。 這是我的代碼:

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樣式彼此連接。 也許我應該將按鈕放在主線程中? 我對線程不是很熟悉。 謝謝。

如果我不得不猜測,您在UpdateTime:方法中做的事情很耗時。 因為它在主線程上並且每1秒運行一次,所以您可能會減慢其他所有速度。 UIButton事件在主線程上,因此很可能因為您在主線程上執行了所有操作,所以它被“阻塞”了。

對於不太精確但不會掛起主線程的計時器實現,請參見以下答案: https : //stackoverflow.com/a/8304825/3708242

如果必須具有NSTimer實現的一致性,請嘗試減少UpdateTime UpdateTime:所做的工作。

我以前有這個問題。 您應該使用UIActivityIndicatorView

按鈕延遲可能是由UpdateTime方法引起的。 使用UIActivityIndicatorViewViewWillAppearViewDidLoad再執行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
}

評論並告訴我這是否有幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM