我正在尝试为 a 的位置设置动画以在 a到达某个位置CAGradientLayer
时缩小。UIScrollView
我将渐变设置为 的遮罩UIScrollView
,并调整渐变的locations
属性以在滚动视图滚动时稍微扩展。这对我来说很好,除非我尝试对位置的更改进行动画处理。当我执行动画时 - 无论是显式CABasicAnimation
还是隐式 - 动画都会正确执行,但无论出于何种原因,一旦动画完成,它就会将我的渐变(没有动画)的位置更改为其他一些非零开始和结束点。它每次都使用相同的值来执行此操作,并且这些值未在我的代码中的任何地方指定。
我对 Core Animation 很陌生,所以也许我遗漏了一些东西,但据我所知,我的代码是正确的。我也尝试过等待执行动画,以解决滚动的任何时间问题,但这也无济于事。
这是我的代码(无论如何,相关部分):
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[CATransaction begin];
[CATransaction setDisableActions:YES];
gradientMask.position = CGPointMake(currentPageOffset, 0.0f);
gradientMask.bounds = self.scrollView.bounds;
[CATransaction commit];
_animateGradientShrinkOnScroll = YES;
// Reset scrollView contentOffset (I'm doing infinite paging)
...
}
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat offset = self.scrollView.contentOffset.x;
CGFloat relativeOffset = offset - currentPageOffset;
[CATransaction begin];
[CATransaction setDisableActions:YES];
gradientMask.position = self.scrollView.contentOffset;
if (_animateGradientShrinkOnScroll)
[CATransaction commit]; // if animating, set position w/o animation first
CGFloat leftLength = ABS(relativeOffset);
CGFloat rightLength = ABS(relativeOffset);
CGFloat maxLength = [self maxGradientLength];
leftLength = MIN((leftLength / 2), maxLength);
rightLength = MIN((rightLength / 2), maxLength);
// When animating, this effectively always goes from non-zero lengths to zero lengths,
// and I've verified this by logging the values used.
[self setGradientLeftLength:leftLength rightLength:rightLength animated:_animateGradientShrinkOnScroll];
if (!_animateGradientShrinkOnScroll)
[CATransaction commit];
_animateGradientShrinkOnScroll = NO;
}
- (void) setGradientLeftLength:(CGFloat)leftLength rightLength:(CGFloat)rightLength animated:(BOOL)animated {
CGFloat startRatio = MAX((leftLength / gradientMask.bounds.size.width), 0);
CGFloat endRatio = 1.0f - MAX((rightLength / gradientMask.bounds.size.width), 0);
NSArray *locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:startRatio],
[NSNumber numberWithFloat:endRatio],
[NSNumber numberWithFloat:1.0f], nil];
if (animated) {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"locations"];
animation.toValue = locations;
[gradientMask addAnimation:animation forKey:kGradientMaskAnimationShrink];
}
else
gradientMask.locations = locations;
}