小编Bla*_*hid的帖子

如何执行等待动画完成的锁?

我正在实现一个通过用户输入动画的健康栏.

这些动画使其上下移动一定量(比如50个单位)并且是按下按钮的结果.有两个按钮.增加和减少.

我想在运行状况栏上执行锁定,以便一次只能有一个线程更改它.问题是我遇到了僵局.

我猜是因为一个单独的线程运行另一个线程持有的锁.但是当动画完成时,该锁将会让位.如何实现在[UIView AnimateWithDuration]完成时结束的锁定?

我想知道是否NSConditionLock可行,但我想尽可能使用NSLocks以避免不必要的复杂性.您有什么推荐的吗?

(最终我希望动画"排队",同时让用户输入继续,但是现在我只想让锁定工作,即使它首先阻止输入.)

(嗯,想到它只有一个[UIView AnimateWithDuration]一次运行同一个UIView.第二个调用将中断第一个,导致完成处理程序立即运行第一个.也许第二个锁运行之前首先有机会解锁.在这种情况下处理锁定的最佳方法是什么?也许我应该重新审视Grand Central Dispatch但是我想看看是否有更简单的方法.)

在ViewController.h中我声明:

NSLock *_lock;
Run Code Online (Sandbox Code Playgroud)

在ViewController.m中我有:

在loadView中:

_lock = [[NSLock alloc] init];
Run Code Online (Sandbox Code Playgroud)

ViewController.m的其余部分(相关部分):

-(void)tryTheLockWithStr:(NSString *)str
{
    LLog(@"\n");
    LLog(@" tryTheLock %@..", str);

    if ([_lock tryLock] == NO)
    {
         NSLog(@"LOCKED.");
    }
          else
    {
          NSLog(@"free.");
          [_lock unlock];
    }
}

// TOUCH DECREASE BUTTON
-(void)touchThreadButton1
{
    LLog(@" touchThreadButton1..");

    [self tryTheLockWithStr:@"beforeLock"];
    [_lock lock];
    [self tryTheLockWithStr:@"afterLock"];

    int changeAmtInt = ((-1) * FILLBAR_CHANGE_AMT); 
    [self updateFillBar1Value:changeAmtInt];

    [UIView animateWithDuration:1.0
                      delay:0.0
                    options:(UIViewAnimationOptionTransitionNone|UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionAllowUserInteraction)
                 animations:
     ^{

         LLog(@" BEGIN animationBlock - val: …
Run Code Online (Sandbox Code Playgroud)

multithreading nslock ios animatewithduration

6
推荐指数
1
解决办法
1408
查看次数

标签 统计

animatewithduration ×1

ios ×1

multithreading ×1

nslock ×1