所以就想來(lái)自己實(shí)現(xiàn)以下
不試不知道,這個(gè)動(dòng)畫(huà)還真不是看上去那么簡(jiǎn)單,我自己想了半天愣是沒(méi)做出來(lái),最后還是看了作者的代碼,才知道怎么實(shí)現(xiàn)。
不過(guò)也從作者哪兒學(xué)了一招,就是layer.mask的用法。
自己實(shí)現(xiàn)的效果如圖:
(前面的畫(huà)圓的動(dòng)畫(huà),這是一個(gè)CAShaperLayer修改其strokeEnd的動(dòng)畫(huà),比較簡(jiǎn)單,就沒(méi)有再寫(xiě)了)
這個(gè)動(dòng)畫(huà)說(shuō)難也不難,其關(guān)鍵就在于對(duì)layer.mask的特性的運(yùn)用,如果你不知道layer.mask的特性,那實(shí)現(xiàn)起來(lái)就相當(dāng)困難了;相反,如果知道,那思路就豁然開(kāi)朗了。
關(guān)鍵就是這個(gè):
/* A layer whose alpha channel is used as a mask to select between the
* layer's background and the result of compositing the layer's * contents with its filtered background. Defaults to nil. When used as * a mask the layer's `compositingFilter' and `backgroundFilters' * PRoperties are ignored. When setting the mask to a new layer, the * new layer must have a nil superlayer, otherwise the behavior is * undefined. Nested masks (mask layers with their own masks) are * unsupported. */
@property(strong) CALayer *mask;
mask屬性,可以實(shí)現(xiàn)很多形狀的遮罩,其基本效果是:
比如layerA是layerB的mask,即layerB.mask = layerA;
那么layerA上透明的部分,會(huì)被繪制成白色擋住layerB(貌似都是白色,不知道能不能弄成其他顏色);
layerA上不透明的部分,會(huì)被繪制成透明,顯示出layerB的內(nèi)容。
2015-09-07更新
之前理解錯(cuò)誤,mask不是遮罩,不是add到layer上的另一個(gè)layer,而是控制layer本身渲染的一個(gè)layer。
效果是:比如imageLayer有一個(gè)maskLayer作為mask(注意maskLayer可以不跟imageLayer大小一樣),
那maskLayer透明的地方,imageLayer就不會(huì)渲染,而是變透明,顯示出imageLayer之后的內(nèi)容,
maskLayer不透明的地方,imageLayer就會(huì)正常渲染,顯示出imageLayer本來(lái)的內(nèi)容
如果maskLayer比imageLayer要小,那默認(rèn)的maskLayer之外的地方都是透明的,都不會(huì)渲染。
注意:作為mask的layer不能有superLayer或者subLayer!
知道了這個(gè),動(dòng)畫(huà)的思路就有了:
imageView上有個(gè)遮罩,遮罩透明的部分逐漸變大,向外向內(nèi)同時(shí)擴(kuò)展,使遮罩后面的圖片爆料出來(lái)。
這里首先需要一個(gè)圓形的CAShapeLayer,還需要兩個(gè)動(dòng)畫(huà),使這個(gè)layer同時(shí)向內(nèi)向外擴(kuò)展。
那么問(wèn)題來(lái)了,只有一個(gè)layer,還不能有subLayer,怎么讓它同時(shí)又變大又變小呢?
答案是:換個(gè)方式。
注意CAShapeLayer是線畫(huà)出來(lái),線也有顏色,還有寬度是 lineWidth,而且這些屬性也是可以動(dòng)畫(huà)的。
所以最終的方案是:設(shè)置圓的線的顏色為透明,圓的填充色為不透明,園外的顏色不透明(這里的設(shè)置指的是看到的效果),讓圓逐漸變大到可以顯示出整個(gè)view,同時(shí)讓圓的lineWidth逐漸變寬到圓的半徑那么大。
看到的效果就是圖片像上面的效果一樣逐漸顯露出來(lái)了。
核心動(dòng)畫(huà)代碼如下:
- (void)reveal {
self.backgroundColor = [UIColor clearColor];
[self.circleLayer removeFromSuperlayer];//理論上作為mask的layer不能有父layer,所以要remove掉
self.superview.layer.mask = self.circleLayer;
//讓圓的變大的動(dòng)畫(huà)
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
UIBezierPath *toPath = [self pathWithDiameter:self.bigDiameter];
// UIBezierPath *toPath = [self pathWithDiameter:0];//縮小當(dāng)前path的動(dòng)畫(huà)
pathAnimation.toValue = (id)toPath.CGPath;
pathAnimation.duration = 1.0;
//讓圓的線的寬度變大的動(dòng)畫(huà),效果是內(nèi)圓變小
CABasicAnimation *lineWidthAnimation = [CABasicAnimation animationWithKeyPath:NSStringFromSelector(@selector(lineWidth))];
lineWidthAnimation.toValue = @(self.bigDiameter);
lineWidthAnimation.duration = 1.0;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[pathAnimation, lineWidthAnimation];
group.duration = 1.0;
group.removedOnCompletion = NO;//這兩句的效果是讓動(dòng)畫(huà)結(jié)束后不會(huì)回到原處,必須加
group.fillMode = kCAFillModeForwards;//這兩句的效果是讓動(dòng)畫(huà)結(jié)束后不會(huì)回到原處,必須加
group.delegate = self;
[self.circleLayer addAnimation:group forKey:@"revealAnimation"];
}
/**
* 根據(jù)直徑生成圓的path,注意圓點(diǎn)是self的中心點(diǎn),所以(x,y)不是(0,0)
*/
- (UIBezierPath *)pathWithDiameter:(CGFloat)diameter {
return [UIBezierPath bezierPathWithOvalInRect:CGRectMake((CGRectGetWidth(self.bounds) - diameter) / 2, (CGRectGetHeight(self.bounds) - diameter) / 2, diameter, diameter)];
}
#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
self.superview.layer.mask = nil;
[self removeFromSuperview];
}
#pragma mark - property
- (CAShapeLayer *)circleLayer {
if (!_circleLayer) {
_circleLayer = [CAShapeLayer layer];
_circleLayer.fillColor = [UIColor clearColor].CGColor;//這個(gè)必須透明,因?yàn)檫@樣內(nèi)圓才是不透明的
_circleLayer.strokeColor = [UIColor yellowColor].CGColor;//注意這個(gè)必須不能透明,因?yàn)閷?shí)際上是這個(gè)顯示出后面的圖片了
_circleLayer.path = [self pathWithDiameter:self.smallDiameter].CGPath;
}
return _circleLayer;
}
有什么錯(cuò)誤歡迎批評(píng)指正