【发布时间】:2015-11-01 17:07:10
【问题描述】:
首先我创建了CAShapeLayer,用于在底部以10px 的高度遮盖UIView,如下所示:
CAShapeLayer *mask = [[CAShapeLayer alloc]init];
CGRect maskRect = CGRectMake(0, self.view.frame.size.height-10, self.view.frame.size.width, 10.0);
CGPathRef path = CGPathCreateWithRect(maskRect, NULL);
mask.path = path;
CGPathRelease(path);
mask.anchorPoint = CGPointMake(0.5, 1.0);
self.view.layer.mask = mask;
在注释完上面的代码后,我创建了一个CALayer,用于将底部的UIView 隐藏起来,高度为10px,如下所示:
CALayer *mask = [CALayer layer];
mask.contents = (id)[[self getImg]CGImage];
mask.frame = CGRectMake(0, self.view.frame.size.height-5, self.view.frame.size.width, 10.0);
mask.anchorPoint = CGPointMake(0.5, 1.0);
self.view.layer.mask = mask;
问题 1:
要完美地触摸底部而不是上方或下方的像素,对于CAShapeLayer 我定义的CGRect 是
CGRectMake(0, self.view.frame.size.height-10, self.view.frame.size.width, 10.0);
对于CALayer,我定义的CGRect是
CGRectMake(0, self.view.frame.size.height-5, self.view.frame.size.width, 10.0);
为什么CAShapeLayer 是-10,CALayer 为什么是-5?
CAShapeLayer 和 CALayer 上的动画代码及其效果:
CGRect oldBounds = mask.bounds;
CGRect newBounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/2);
[UIView animateWithDuration:1.0 animations:^{
CABasicAnimation* revealAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
revealAnimation.fromValue = [NSValue valueWithCGRect:oldBounds];
revealAnimation.toValue = [NSValue valueWithCGRect:newBounds];
revealAnimation.duration = 0.5;
[mask addAnimation:revealAnimation forKey:@"revealAnimation"];
}];
// Update the bounds so the layer doesn't snap back when the animation completes.
mask.bounds = newBounds;
对CAShapeLayer 的影响位置发生了变化,但对高度没有影响,如下所示:
对CALayer 的影响,效果完美,我想要的结果:
问题 2:
为什么CAShapeLayer改变了位置而不是高度?
我不知道为什么,但我感觉CALayer 动画不流畅?
问题 3:
[UIView animateWithDuration:1.0 animations:^{ ... 块对动画没有任何影响,为什么?
【问题讨论】:
标签: ios objective-c uiview calayer cashapelayer