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