- 首先我要說的是點(diǎn)擊加入購(gòu)物車按鈕彈出View的動(dòng)畫解析聂示,而不是什么點(diǎn)擊商品圖片飛來飛去的動(dòng)畫,效果是這樣的:
taobao2.gif
- 乍一看很簡(jiǎn)單鹅颊,不就把它放大縮小一下彩匕,你要這么做效果那真是慘不能睹,仔細(xì)觀察手機(jī)淘寶這個(gè)動(dòng)畫效果杏愤,一氣呵成靡砌,有3D效果,像一張紙一樣往下落的感覺珊楼。由于產(chǎn)品的需求通殃,一開始毫無頭緒,網(wǎng)上的第三方做出效果我很不滿意:
disanfang.gif
動(dòng)畫過程給人感覺很突兀厕宗,銜接不夠流暢画舌,一個(gè)動(dòng)畫需要兩個(gè)階段完成,這就導(dǎo)致不夠順滑已慢。
該動(dòng)畫效果可以由縮小曲聂,旋轉(zhuǎn),平移佑惠,下落動(dòng)畫并發(fā)執(zhí)行得到的結(jié)果朋腋,可以使用核心動(dòng)畫組來實(shí)現(xiàn)并發(fā)的動(dòng)畫;
View彈出的動(dòng)畫:背景下落膜楷,popview彈出旭咽,使用核心動(dòng)畫組實(shí)現(xiàn)
x, y軸方向分別縮放赌厅,z軸方向下落穷绵,根據(jù)錨點(diǎn)繞x軸旋轉(zhuǎn),再加入y軸方向的平移察蹲,使其有飄來飄去的感覺。
效果如下:
popView.gif
代碼如下:
popView出現(xiàn)的時(shí)候:
//該方法是并發(fā)催训,所以不影響核心動(dòng)畫的執(zhí)行洽议,為什么需要這個(gè)方法,因?yàn)槲覀冞€需要對(duì)popview和coverView進(jìn)行操作漫拭,對(duì)不需要操作的self.View就可以使用核心動(dòng)畫
[UIView animateWithDuration:0.5 animations:^{
popView.yqh_y = (MAINHEIGHT - 280);
coverView.alpha = 0.3;
}];
//注意設(shè)置錨點(diǎn)和位置點(diǎn)亚兄,因?yàn)樾D(zhuǎn)和縮放都是按照錨點(diǎn)來的
self.view.layer.anchorPoint = CGPointMake(0.5, 0.8);
self.view.layer.position = CGPointMake(self.view.yqh_width * .5, self.view.yqh_height * .8);
CATransform3D tran = CATransform3DIdentity;
//設(shè)置近大遠(yuǎn)小的效果,值越大效果越明顯采驻,所以不要太大
tran.m34 = -1.0/1000;
self.view.layer.transform = tran;
//核心動(dòng)畫也是并發(fā)
CABasicAnimation *scaleAnimateX = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
scaleAnimateX.toValue = @(0.9);
//延遲0.2秒執(zhí)行
scaleAnimateX.beginTime = 0.2;
CABasicAnimation *scaleAnimateY = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
scaleAnimateY.toValue = @(0.9);
scaleAnimateY.beginTime = 0.2;
CABasicAnimation *positonY = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
positonY.toValue = @(-60);
CABasicAnimation *positonZ = [CABasicAnimation animationWithKeyPath:@"transform.translation.z"];
positonZ.toValue = @(-100);
CAKeyframeAnimation *rotation = [CAKeyframeAnimation animation];
rotation.keyPath = @"transform.rotation.x";
rotation.values = @[@(0), @(15 / 180.0 * M_PI), @(0)];
rotation.repeatCount = 1;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[scaleAnimateX, scaleAnimateY, rotation, positonY, positonZ];
group.duration = 0.5;
//動(dòng)畫結(jié)束不恢復(fù)原狀
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
[self.view.layer addAnimation:group forKey:nil];
popView消失的時(shí)候:
[UIView animateWithDuration:0.5 animations:^{
//popview消失
self.popView.yqh_y = MAINHEIGHT;
self.coverView.alpha = 0;
}completion:^(BOOL finished) {
[self.popView removeFromSuperview];
[self.coverView removeFromSuperview];
//移除動(dòng)畫
[self.view.layer removeAllAnimations];
//恢復(fù)默認(rèn)的錨點(diǎn)和位置點(diǎn)
self.view.layer.anchorPoint = CGPointMake(0.5, 0.5);
self.view.layer.position = CGPointMake(self.view.yqh_width * .5, self.view.yqh_height * .5);
}];
//恢復(fù)的動(dòng)畫跟上面的相反
CATransform3D tran = CATransform3DIdentity;
tran.m34 = -1.0/1000;
self.view.layer.transform = tran;
CABasicAnimation *scaleAnimateX = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
scaleAnimateX.toValue = @(1.0);
CABasicAnimation *scaleAnimateY = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
scaleAnimateY.toValue = @(1.0);
CABasicAnimation *positonY = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
positonY.toValue = @(0);
CABasicAnimation *positonZ = [CABasicAnimation animationWithKeyPath:@"transform.translation.z"];
positonZ.toValue = @(0);
CAKeyframeAnimation *rotation = [CAKeyframeAnimation animation];
rotation.keyPath = @"transform.rotation.x";
rotation.values = @[@(0), @(15 / 180.0 * M_PI), @(0)];
rotation.repeatCount = 1;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[scaleAnimateX, scaleAnimateY, rotation, positonY, positonZ];
group.duration = 0.5;
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
[self.view.layer addAnimation:group forKey:nil];
唉审胚,也懶得封裝了匈勋,大家可以給uiviewcontroller寫個(gè)分類,到時(shí)候就可以用上了膳叨。洽洁。。