小编jje*_*ton的帖子

如何在继续使用segue之前等待另一个线程完成?

我有两个UITableViewControllers,我在它们之间使用故事板进行区分.

prepareForSegue:sender:,第一个VC从Web服务获取NSArray,并使用下载的数据设置目标VC的模型.我把下载部分放入一个dispatch_queue_t所以它将异步运行而不是阻止UI线程.我想点击一个表格单元格,让UIActivityIndi​​catorView开始旋转,下载照片,下载照片时,停止微调器并继续使用segue.

在第一个UITableViewController我有一个prepareForSegue:sender:方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"SelectPlace"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];

        UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]
                                            initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [spinner startAnimating];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:spinner];

        __block NSArray *photos = [[NSMutableArray alloc] init];
        dispatch_queue_t downloadQueue = dispatch_queue_create("flickr download", NULL);
        dispatch_async(downloadQueue, ^{
            photos = [FlickrFetcher photosInPlace:[self.places objectAtIndex:indexPath.row] maxResults:50];
        });
        dispatch_release(downloadQueue);
        [spinner stopAnimating];
        [segue.destinationViewController setPhotos:photos withTitle:[[sender textLabel] text]];
    }

}
Run Code Online (Sandbox Code Playgroud)

现在它立即执行segue而不显示微调器或等待下载完成.

如何在不阻塞主线程的情况下异步下载数据以准备目标视图,还能不立即进行segueing?

asynchronous ios ios5 uistoryboardsegue

7
推荐指数
2
解决办法
5162
查看次数

如何用JavaScript获得CSS变换旋转值

我正在使用CSS-Tricks中的代码来获取当前使用JavaScript的旋转变换(在CSS中).

JavaScript函数:

function getCurrentRotation( elid ) {
  var el = document.getElementById(elid);
  var st = window.getComputedStyle(el, null);
  var tr = st.getPropertyValue("-webkit-transform") ||
       st.getPropertyValue("-moz-transform") ||
       st.getPropertyValue("-ms-transform") ||
       st.getPropertyValue("-o-transform") ||
       st.getPropertyValue("transform") ||
       "fail...";

  if( tr !== "none") {
    console.log('Matrix: ' + tr);

    var values = tr.split('(')[1];
      values = values.split(')')[0];
      values = values.split(',');
    var a = values[0];
    var b = values[1];
    var c = values[2];
    var d = values[3];

    var scale = Math.sqrt(a*a + b*b);

    // arc sin, convert from radians …
Run Code Online (Sandbox Code Playgroud)

javascript css transform css3

7
推荐指数
2
解决办法
6904
查看次数

使用preg_replace匹配重复的空格

我正在编写一个WordPress插件,其中一个功能是删除重复的空格.

我的代码看起来像这样:

return preg_replace('/\s\s+/u', ' ', $text, -1, $count);
Run Code Online (Sandbox Code Playgroud)
  • 我不明白为什么我需要u 修饰符.我见过其他使用preg_replace但不需要为Unicode修改它的插件.我相信我有一个WordPress的默认安装.

  • 如果没有修饰符,代码将使用Unicode替换字形而不是空格替换所有空格.

  • 使用u修饰符,我没有得到字形,并且它不会替换所有空格.

下面的每个空间都有1-10个空格.正则表达式仅从每个组中移除空间.

之前:

This sentence  has extra space.  This doesn’t.  Extra  space, Lots          of extra space.
Run Code Online (Sandbox Code Playgroud)

后:

This sentence has extra space. This doesn’t. Extra space, Lots         of extra space.
Run Code Online (Sandbox Code Playgroud)

$count = 9

如何让正则表达式替换整个匹配的空间?


更新:如果我尝试使用常规php,它可以正常工作

$new_text = preg_replace('/\s\s+/', ' ', $text, -1, $count);
Run Code Online (Sandbox Code Playgroud)

它只在我在wordpress插件中使用它时才会中断.我在过滤器中使用此功能:

function jje_test( $text ) {
    $new_text = preg_replace('/\s\s+/', ' ', $text, -1, $count);
    echo "Count: $count";
    return $new_text;
} …
Run Code Online (Sandbox Code Playgroud)

php regex wordpress

3
推荐指数
1
解决办法
2963
查看次数