【问题标题】:UIView does not appear after alpha property gets reset to 1.0alpha 属性重置为 1.0 后 UIView 不出现
【发布时间】:2017-04-22 16:37:24
【问题描述】:

我有这个测试代码,您可以在其中点击 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 属性不会出现(尽管您在这里看不到,但我已经记录了@ 987654328@ 表示 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

【问题讨论】:

  • fadeMenuOut 方法的animationscompletion 块内添加NSLog 语句,您将看到问题。
  • 很奇怪,所以 animation 块在某个后台线程上运行(我在想什么)?因为我在completion 块中的NSLog 语句在setupSpawnTimer NSLog 语句执行之后执行。我该如何解决这个问题,或者动画运行时间是否没有灵活性?
  • 我是否应该为后续方法创建执行延迟([self spawnDot][self setupSpawnTimer])

标签: ios uiview alpha


【解决方案1】:

这个问题主要是由于fadeOutMenu 中的完成块在调用spawnDot 之后被调用。

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

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

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

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

【讨论】:

  • 哦,那好多了,使后续方法的“延迟”变得任意(或者,更确切地说,它们依赖于 1 秒淡出菜单......或 3 秒,4 , ETC。)。谢谢rmaddy。 completion 块内的 [self.dot removeFromSuperview] 使某些事情明显发生,谢谢。
  • 作为后续,我从来没有过多地使用故事板或 xibs,所以如果我在通过故事板创建的 UIView 对象上调用 [self.dot removeFromSuperview],我该怎么做考虑到它是一个弱属性,重新为其分配一个新实例?由于某种原因,我无法显示此新视图。
  • 我会在创建新视图时使用局部变量来引用它。将新视图添加为子视图后,将局部变量分配给弱属性。
  • 不幸的是仍然没有运气。我用新的[self spawnDot] 代码在上面发布了更新。
  • 你知道有时我会遇到什么......我无法寻找最简单的混乱来源。事实证明,(鉴于我为 self.view 使用白色背景)newDot 实例是白色......所以它一直与背景融为一体。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-28
  • 1970-01-01
  • 2013-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多