我正在做一个示例项目,其中我有一个垂直的scrollview和一个水平的scrollview。垂直scrollview有许多子视图。在scrollviewDidScroll中,我正在执行一些操作。同时,当屏幕上可见特定的子视图时,我想在垂直滚动视图中动画一个子视图。为此,我做了一个正确的计算。动画如下:
子视图包含多个自定义视图。我试图在特定的时间序列中(减少视图的alpha值,然后再增加视图的alpha值)来动画这些视图(因此动画看起来是顺序的)。为此,我发布通知的视图和动画序列和逻辑是完美的,根据我想要的。
但是当我放置断点时,我面临着代码正在执行的问题,但是动画没有显示出来。如果我在停止滚动后发布通知,那么动画就会完全正常。但我想要的是动画发生,即使我是滚动和视图在屏幕上。
我正在添加代码片段如下所示:
SubView:(在我的scrollview里面)
- (void)animateSequenceTemplate {
if (!hasSequenceTemplateAnimated) {
[self performSelector:@selector(animateSequenceSlides) withObject:nil afterDelay:0];
hasSequenceTemplateAnimated = YES;
}
else {
return;
}
}
- (void)animateSequenceSlides {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:imageAsset forKey:@"assetMO"];
[[NSNotificationCenter defaultCenter]postNotificationName:AnimateSequenceSlide object:nil userInfo:userInfo];
}上述子视图中的子视图:
- (void)animateSlideView:(NSNotification *)notification {
NSDictionary *userInfo = notification.userInfo;
if ([userInfo objectForKey:@"assetMO"] == assetMO) {
[[NSNotificationCenter defaultCenter]removeObserver:self name:AnimateSequenceSlide object:nil];
CGFloat duration = 0.035;
float delay = (self.slideCount * duration);
CGFloat timeDelay = 0;
[self performSelector:@selector(animationPhase1) withObject:nil afterDelay:delay];
timeDelay = delay + duration;
[self performSelector:@selector(animationPhase2) withObject:nil afterDelay:timeDelay];
if (self.slideCount == (self.maxSlideCount - 1)) {
timeDelay += duration * 18; //No animation till 18 frames
}
else {
timeDelay += duration;
}
[self performSelector:@selector(animationPhase3) withObject:nil afterDelay:timeDelay];
timeDelay += duration;
[self performSelector:@selector(animationPhase4) withObject:nil afterDelay:timeDelay];
}
}
- (void)animationPhase1 {
[UIView animateWithDuration:0.035 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^ {
[self setAlpha:0.85];
}completion:^(BOOL finished) {
}];
}在
- (void)animateSequenceTemplate中发生两种情况:
[self animateSequenceSlides];。在这种情况下,动画不会出现。[self performSelector:@selector(animateSequenceSlides) withObject:nil afterDelay:0.0f];。在这种情况下,动画会显示出来,但是在scrollview休息之后。我不得不使用执行选择器来使用UIView动画,因为如果我删除它并使用嵌套的UIView动画块/或者直接调用这些方法,那么动画就不会出现。现在,它至少在I rest滚动之后出现。
我想要一个解决方案的建议,或者任何关于我可能犯的错误的猜测。
发布于 2014-06-10 11:23:57
有可能不是在主队列上执行它。
dispatch_async(dispatch_get_main_queue, block())可能会有帮助。当使用动画时,主队列是执行代码的唯一位置,这会使动画发生。
编辑:
[self performSelector:@selector(animationPhase1) withObject:nil afterDelay:delay];我不知道它是在哪里发生的,但我不认为主要原因是什么。试试这个:
[self performSelectorOnMainThread:@selector(animationPhase1)
withObject:nil
waitUntilDone:YES];我不确定延迟,这就是为什么我更喜欢GCD函数和块。
https://stackoverflow.com/questions/24139402
复制相似问题