我有这个测试代码,你点击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
请注意,当我的NSLog
对象UIView
的第二个self.dot
被调用时,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
答案 0 :(得分:1)
问题主要是由于在调用fadeOutMenu
后调用spawnDot
中的完成功能块。
另请注意,您在[self.dot removeFromSuperview];
末尾对fadeMenuOut
的调用需要移至完成块的末尾。就像现在一样,你不会看到任何动画。但这是一个侧面问题。
要解决您的整体问题,我会在您的fadeMenuOut
方法中添加完成块参数。在UIView动画块的完成处理程序结束时调用此完成块。
您传递给此完成参数的数据块应调用spawnDot
和setUpSpawnTimer
方法。
在旁注中,我还会更新您的fadeTheDot
方法,以便在`UIView动画的完成块结束时调用[self.dot removeFromSuperview];
。同样,这将解决缺少实际动画的问题。