简体   繁体   English

如果在另一个线程中更改了值,是否会在块/完成块内部更新值?

[英]Does a value update inside a block/completion block, if changed in a different thread?

I have a UIView animateWithDuration (in a method being called by a button) that animates a UIImageViews . 我有一个UIView animateWithDuration (在通过按钮调用的方法中),它对UIImageViews了动画处理。 The important code before the animation code: (the title is appropriate, just keep reading) 动画代码之前的重要代码:(标题合适,请继续阅读)

//Sets _squareOneNumber to 0 (this is going to be the changing value)
_squareOneNumber = 0;

Basically the animation code just allows user interaction and animates the image to down the screen at a random pace. 基本上,动画代码仅允许用户交互,并以随机的速度将图像动画显示在屏幕上。

But, it's the completion block that is killing me (don't worry about a and b ): 但是,是完成块杀死了我(不用担心ab ):

if (self.squareOneNumber==0) {
    if (a==b) {
        [self gameOverImagePutter];
        NSLog(@"One wasn't pressed");
    }
}

The value of _squareOneNumber changes to 1 if it is pressed. 如果按下_squareOneNumber的值, _squareOneNumber更改为1。

//In touchesBegan method
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];

if ([self.squareOne.layer.presentationLayer hitTest:touchLocation]) {
    [_squareOne setHidden:YES];
    _squareOneNumber = 1;
}

The completion block should call gameOverImagePutter if squareOne wasn't pressed ( squareOneNumber=0 ) and a==b . 如果squareOnesquareOneNumber=0 )和a==b则完成块应调用gameOverImagePutter But it is always called when squareOne is pressed ( squareOneNumber=1 ). 但是总是在按下 squareOneNumber=1squareOne squareOneNumber=1 )。 To me, the code should work fine. 对我来说,代码应该可以正常工作。 But I think the issue is that squareOneNumber isn't getting updated even though its value has changed. 但是我认为问题在于,即使squareOneNumber的值已更改, squareOneNumber也不会得到更新。

So basically this is my question: 所以基本上这是我的问题:

  • how to I get the code to work? 如何使代码正常工作?
  • why isn't squareOneNumber realizing it's value has changed? squareOneNumber为什么没有意识到它的值已经改变?

i recreated the code that i think you have from what you posted and it does work as expected. 我从发布的内容中重新创建了我认为您拥有的代码,它确实按预期工作。 squareOneNumber is updated when completion block executes. 当完成块执行时, squareOneNumber将更新。 The image is on screen, when the button is pressed image starts moving. 图像在屏幕上,按下按钮时图像开始移动。 When you press on the image it is hidden and squareOneNumber is set to 1. Then in few seconds completion block is being executed with the update value. 当您在图像上squareOneNumber它是隐藏的, squareOneNumber设置为1。然后在几秒钟内使用更新值执行完成块。

The only way that won't work if you press the button again and the animation is running with the hidden image and squareOneNumber resets to 0 which will be reflected in the completion block. 如果再次按下该按钮并且动画以隐藏的图像运行,并且squareOneNumber重置为0,则该方法将不起作用,这将反映在完成块中。 Here is my code. 这是我的代码。 Let me know if i correctly recreated your part of the code. 让我知道我是否正确地创建了代码的一部分。

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *squareOne;
@property (nonatomic) NSUInteger squareOneNumber;
@end

@implementation ViewController
- (IBAction)animateImage:(id)sender
{
    [UIView animateWithDuration:4.0 animations:^{
        self.squareOneNumber = 0;
        self.squareOne.center = CGPointMake(300, 300);

    } completion:^(BOOL finished) {
        NSLog(@"square one in completion %lu",self.squareOneNumber);
        if (self.squareOneNumber == 0) {
            NSLog(@"0");
        }else{
            NSLog(@"1");
        }

    }];

}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if ([self.squareOne.layer.presentationLayer hitTest:touchLocation]) {
        [self.squareOne setHidden:YES];
        self.squareOneNumber = 1;
        NSLog(@"square one in touches began %lu",self.squareOneNumber);
    }
}

@end

Here is the NSLog output when imageView is pressed 这是按下imageView时的NSLog输出

2014-11-30 23:14:30.541 StackOveflowAnimation[3885:1092103] square one in touches began 1
2014-11-30 23:14:33.356 StackOveflowAnimation[3885:1092103] square one in completion 1
2014-11-30 23:14:33.356 StackOveflowAnimation[3885:1092103] 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM