0

我有一个UIView animateWithDuration(在由按钮调用的方法中)为UIImageViews. 动画代码前的重要代码:(标题贴切,继续看下去)

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

基本上,动画代码只允许用户交互并以随机速度将图像动画到屏幕下方。

但是,是完成块杀死了我(不用担心aand b):

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

如果按下它,值将_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;
}

gameOverImagePutter如果squareOne未按下 ( squareOneNumber=0) 和 ,则完成块应调用a==b。但它总是在squareOne 按下( squareOneNumber=1) 时调用。对我来说,代码应该可以正常工作。但我认为问题在于,squareOneNumber即使它的价值发生了变化,它也没有得到更新。

所以基本上这是我的问题:

  • 如何让代码工作?
  • 为什么没有squareOneNumber意识到它的价值已经改变?
4

1 回答 1

0

我根据您发布的内容重新创建了我认为您拥有的代码,它确实按预期工作。 squareOneNumber在完成块执行时更新。图像在屏幕上,当按下按钮时图像开始移动。当您按下图像时,它被隐藏并squareOneNumber设置为 1。然后在几秒钟内使用更新值执行完成块。

如果您再次按下按钮并且动画正在使用隐藏图像运行并且 squareOneNumber 重置为 0 将反映在完成块中,那么唯一的方法将不起作用。这是我的代码。让我知道我是否正确地重新创建了您的部分代码。

#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

这是按下 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
于 2014-11-29T04:34:56.853 回答