按下按钮时IBAction滞后

时间:2012-08-07 15:55:12

标签: iphone

我有一个UIViewController,其中包含UITableView。我在其上添加了UIView作为子视图。当我按下子视图中的UIButton之一时,会出现明显的滞后现象。如何让它更快?

观看视频:http://www.youtube.com/watch?v=KWy6NrZUeqA&feature=youtu.be

- (IBAction)tweetThat:(id)sender {
    [MBProgressHUD showHUDAddedTo:self.socialMediaView animated:YES];

    dispatch_queue_t queueOne=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_sync(queueOne, ^{
        NSString *tweetBody=@"BLABLABLATweet";

        if ([TWTweetComposeViewController canSendTweet])
        {
            TWTweetComposeViewController *tweetSheet = 
            [[TWTweetComposeViewController alloc] init];
            [tweetSheet setInitialText:tweetBody];
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:self.controllerView animated:YES];
                [self presentModalViewController:tweetSheet animated:YES];
            });
        }
        });
}

3 个答案:

答案 0 :(得分:1)

我已经使用了您在上面尝试过的调度方法,但进行了一些更改。它确实使体验变得更好。 “loadingView”是命名的,只是在推文视图出现之前显示的加载视图。

    loadingView.hidden = NO;
    [loadingView setNeedsDisplay];
    if ([TWTweetComposeViewController canSendTweet])
    {
        double delayInSeconds = 0.5;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            TWTweetComposeViewController *tweet = [[TWTweetComposeViewController alloc]init];
            [tweet setInitialText:@"I'm using a new app called TickTalk to help my speaking cadence. Check it out!"];
            [tweet addURL:[NSURL URLWithString:@"http://www.ticktalkapp.com"]];
            TWTweetComposeViewControllerCompletionHandler
            completionHandler =
            ^(TWTweetComposeViewControllerResult result) {
                switch (result)
                {
                    case TWTweetComposeViewControllerResultCancelled:
                        NSLog(@"Twitter Result: canceled");
                        break;
                    case TWTweetComposeViewControllerResultDone:
                        NSLog(@"Twitter Result: sent");
                        break;
                    default:
                        NSLog(@"Twitter Result: default");
                        break;
                }
                loadingView.hidden = YES;
                [self dismissModalViewControllerAnimated:YES];
            };
            [tweet setCompletionHandler:completionHandler];
            [self presentModalViewController:tweet animated:YES];
        });
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Sorry" message:@"Your device is not setup to tweet" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        loadingView.hidden = YES;
    }

答案 1 :(得分:0)

滞后来自TWTweetComposeViewController。你无能为力,因为它是一个系统优化。您可能会注意到第二次和第三次按下按钮时它会消失。

答案 2 :(得分:0)

将dispatch_sync更改为dispatch_async。如果要在UI返回之前锁定UI,那么调度到后台的重点是什么。