【发布时间】:2014-07-31 02:55:58
【问题描述】:
我正在处理一个示例项目,其中我有一个垂直的scrollview 和一个水平的scrollview。垂直的scrollview 有许多子视图。在scrollviewDidScroll 我正在执行一些操作。除此之外,我现在想在垂直滚动视图内的子视图在屏幕上可见时为该子视图设置动画。为此,我正在做一个正确的计算。动画如下:
子视图包含多个自定义视图。我正在尝试以某些特定的时间顺序对这些视图中的每一个进行动画处理(减少然后再次增加视图的 alpha 值)(以便动画看起来是按顺序排列的)。为此,我正在向视图发布通知,并且动画序列和逻辑根据我的需要是完美的。
但是我遇到的问题是我下断点时代码正在执行,但动画没有出现。如果我在停止滚动后发布通知,那么动画会非常好。但我希望即使在我滚动并且视图在屏幕上时也能发生动画。
我正在添加我的代码 sn-p 如下:
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 动画块/或直接调用这些方法,那么动画不会再次出现。现在至少它会在我休息滚动后显示。
我想就解决方案提出建议,或者对我可能犯的错误进行任何猜测。
【问题讨论】:
-
你能发布你的代码吗?很难猜测到底出了什么问题。
-
已编辑我的问题。请看一看。希望它对我们俩都有帮助。
-
谢谢安东!但这对我不起作用。
标签: ios objective-c ipad ios7 xcode5