iOS开发-动画

iOS淘宝购物界面动画解析

2016-11-11  本文已影响145人  游某人
taobao2.gif disanfang.gif
动画过程给人感觉很突兀,衔接不够流畅,一个动画需要两个阶段完成,这就导致不够顺滑。

该动画效果可以由缩小,旋转,平移,下落动画并发执行得到的结果,可以使用核心动画组来实现并发的动画;

View弹出的动画:背景下落,popview弹出,使用核心动画组实现
x, y轴方向分别缩放,z轴方向下落,根据锚点绕x轴旋转,再加入y轴方向的平移,使其有飘来飘去的感觉。
效果如下:

popView.gif

代码如下:
popView出现的时候:

//该方法是并发,所以不影响核心动画的执行,为什么需要这个方法,因为我们还需要对popview和coverView进行操作,对不需要操作的self.View就可以使用核心动画
[UIView animateWithDuration:0.5 animations:^{
        popView.yqh_y = (MAINHEIGHT - 280);
        coverView.alpha = 0.3;
    }];
    
//注意设置锚点和位置点,因为旋转和缩放都是按照锚点来的
    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;
//设置近大远小的效果,值越大效果越明显,所以不要太大
    tran.m34 = -1.0/1000;
    self.view.layer.transform = tran;
    
//核心动画也是并发
    CABasicAnimation *scaleAnimateX = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
    scaleAnimateX.toValue = @(0.9);
//延迟0.2秒执行
    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;
    
//动画结束不恢复原状
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    [self.view.layer addAnimation:group forKey:nil];

popView消失的时候:

[UIView animateWithDuration:0.5 animations:^{
//popview消失
        self.popView.yqh_y = MAINHEIGHT;
        self.coverView.alpha = 0;
    }completion:^(BOOL finished) {
        [self.popView removeFromSuperview];
        [self.coverView removeFromSuperview];
//移除动画
        [self.view.layer removeAllAnimations];
//恢复默认的锚点和位置点
        self.view.layer.anchorPoint = CGPointMake(0.5, 0.5);
        self.view.layer.position = CGPointMake(self.view.yqh_width * .5, self.view.yqh_height * .5);
    }];
    
//恢复的动画跟上面的相反
    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写个分类,到时候就可以用上了。。。

上一篇 下一篇

猜你喜欢

热点阅读