alpha 属性 重置为 1.0 后 UIView 不出现

UIView does not appear after alpha property gets reset to 1.0

我有这个测试代码,你可以点击 UIView(当前是一个小方块),它会从当前坐标淡出,然后重新出现在另一个随机坐标(永远重复这个序列,目前)。

我的问题是正方形没有重新出现在新位置(其坐标是超级视图(背景)中可观察区域的一部分)。

代码如下:

- (IBAction)tap:(UITapGestureRecognizer *)recognizer {
    [self fadeMenuOut];
    [self spawnDot];
    [self setUpSpawnTimer];
}

- (void)fadeMenuOut {
    [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionBeginFromCurrentState
                 animations:(void (^)(void)) ^{
                     self.dot.alpha = 0.0;
                 }
                 completion:^(BOOL finished) {
                     self.dot.hidden = YES;
                     self.dot.userInteractionEnabled = NO;
                 }];
    [self.dot removeFromSuperview];
    NSLog(@"fadeMenuOut");
}

- (void)spawnDot {
    int newX = frandom_range(40.0, 220.0);
    int newY = frandom_range(40.0, 220.0);
    NSLog(@"dot:%@", self.dot);
    CGRect frame = self.dot.frame;
    frame.origin.x = newX;
    frame.origin.y = newY;
    self.dot.frame = frame;
    self.dot.alpha = 1.0;
    self.dot.hidden = NO;
    self.dot.userInteractionEnabled = YES;
   [self.view addSubview:self.dot];
    NSLog(@"dot:%@", self.dot);
    NSLog(@"spawnDot");
}

// took this from another SO member
static inline CGFloat frandom() {
    return (CGFloat)random()/UINT32_C(0x7FFFFFFF);
}

static inline CGFloat frandom_range(CGFloat low, CGFloat high) {
    return (high-low)*frandom()+low;
}

- (void)setUpSpawnTimer {
    [self.spawnTimer invalidate];
    NSTimer *newTimer = [NSTimer timerWithTimeInterval:5.0
                                                target:self
                                              selector:@selector(fadeTheDot)
                                              userInfo:nil
                                               repeats:YES];
    self.spawnTimer = newTimer;
    [[NSRunLoop mainRunLoop] addTimer:newTimer forMode:NSRunLoopCommonModes];
    NSLog(@"setUpSpawnTimer");
}

// if, after 5 seconds, the button isn't tapped, it disappears and you lose
- (void)fadeTheDot {
    [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionBeginFromCurrentState
                 animations:(void (^)(void)) ^{
                     self.dot.alpha = 0.0;
                 }
                 completion:^(BOOL finished) {
                     self.dot.hidden = YES;
                     self.dot.userInteractionEnabled = NO;
                 }];
    [self.dot removeFromSuperview];
    [self.spawnTimer invalidate];
    NSLog(@"fadeTheDot");
}

这是输出:

2017-04-22 12:18:32.341 DotFever[916:570634] fadeMenuOut
2017-04-22 12:18:32.343 DotFever[916:570634] dot:<UIView: 0x17d3b9d0; frame = (130 254; 60 60); alpha = 0; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x17d3b2d0>; animations = { opacity=<CABasicAnimation: 0x17e40850>; }; layer = <CALayer: 0x17d3bb70>>
2017-04-22 12:18:32.345 DotFever[916:570634] dot:<UIView: 0x17d3b9d0; frame = (191 110; 60 60); autoresize = RM+BM; gestureRecognizers = <NSArray: 0x17d3b2d0>; animations = { opacity=<CABasicAnimation: 0x17e40850>; }; layer = <CALayer: 0x17d3bb70>>
2017-04-22 12:18:32.345 DotFever[916:570634] spawnDot
2017-04-22 12:18:32.346 DotFever[916:570634] setUpSpawnTimer
2017-04-22 12:18:34.347 DotFever[916:570634] fadeTheDot

请注意,当我的 UIView 对象 self.dot 的第二个 NSLog 被调用时,self.dot.alpha 属性 不会出现(即使,你在这里看不到,我已经记录了 self.dot.alpha 在它说 1.0 的那个点)。

我在处理我的观点时有什么地方做错了吗?它创建于 Main.storyboard。最初,在开始时,视图通过 Any/Any 场景的情节提要约束完全位于屏幕中心。谢谢。

///更新

- (void)spawnDot {
    UIView* newDot = [[UIView alloc] initWithFrame:CGRectMake((int)frandom_range(40.0, 220.0),
                                                          (int)frandom_range(40.0, 220.0), 60, 60)];
    NSLog(@"self.dot:%@", self.dot);
    [self.view addSubview:newDot];
    self.dot = newDot;
    self.dot.alpha = 1.0;
    self.dot.hidden = NO;
    self.dot.userInteractionEnabled = YES;
    NSLog(@"dot:%@", newDot);
    NSLog(@"self.dot:%@", self.dot);
    NSLog(@"spawnDot");
}

输出:

2017-04-22 14:19:19.727 DotFever[1033:590311] self.dot:<UIView: 0x155ac070; frame = (130 254; 60 60); alpha = 0; hidden = YES; autoresize = RM+BM; userInteractionEnabled = NO; gestureRecognizers = <NSArray: 0x15697990>; layer = <CALayer: 0x155ac210>>
2017-04-22 14:19:19.729 DotFever[1033:590311] dot:<UIView: 0x15684d60; frame = (191 110; 60 60); layer = <CALayer: 0x15684280>>
2017-04-22 14:19:19.730 DotFever[1033:590311] self.dot:<UIView: 0x15684d60; frame = (191 110; 60 60); layer = <CALayer: 0x15684280>>
2017-04-22 14:19:19.730 DotFever[1033:590311] spawnDot
2017-04-22 14:19:19.731 DotFever[1033:590311] setUpSpawnTimer

问题的主要原因是 fadeOutMenu 中的完成块在调用 spawnDot 之后被调用。

另请注意,您在 fadeMenuOut 末尾对 [self.dot removeFromSuperview]; 的调用需要移至完成块的末尾。就像现在一样,您不会看到任何动画。但这是一个附带问题。

为了解决您的整体问题,我会在您的 fadeMenuOut 方法中添加一个完成块参数。在 UIView 动画块的完成处理程序结束时调用此完成块。

您传递给此完成参数的块应该调用您的 spawnDotsetUpSpawnTimer 方法。

附带说明一下,我还会更新您的 fadeTheDot 方法以在 `UIView 动画的完成块结束时调用 [self.dot removeFromSuperview];。同样,这将解决缺少实际动画的问题。