I have a UIView animateWithDuration
(in a method being called by a button) that animates a 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
):
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.
//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
. But it is always called when squareOne
is pressed (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.
So basically this is my question:
- how to I get the code to work?
- why isn't
squareOneNumber
realizing it's value has changed?
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. The image is on screen, when the button is pressed image starts moving. When you press on the image it is hidden andsquareOneNumber
is set to 1. Then in few seconds completion block is being executed with the update value.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. Here is my code. Let me know if i correctly recreated your part of the code.
Here is the NSLog output when imageView is pressed