iOS动画 狗和狐狸的对战

作者: e40c669177be | 来源:发表于2017-01-24 16:13 被阅读557次

年前的最后一篇了,希望年前收获的喜欢可以突破100,给去年个圆满的结尾当然实现不了,我也很高兴的,比较已经收获了九十多个了,谢谢大家的喜欢🤗🤗

老规矩还是先放swift和OC版本的狐狸和狗的对战,比较有的人是看不下去文章的~(我就是那类人啊)

复杂动画都是由简单动画构成的

动画的设计思路, 使用OC的代码进行讲解了

  • 1.怎么由方形的图片得到圆形的带边框的图片那?


    A2945CB9-78AD-4731-A198-1A5EE4BDD6DF.png

你或许告诉我可以使用图片的裁剪啊,或许有人问啥叫图片的裁剪啊啥,你说啥我听不到啊嘿嘿,不逗了,过年了,还是说下怎么那样裁剪吧

//普通裁剪
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width , size.height)];
    [[UIColor orangeColor] set];
    [path fill];
    //5.在添加有一个小圆,把小圆设置为裁剪区域(因为图片不是方形的所有裁剪会有问题)
    path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderw , borderw , image.size.width , image.size.height - 5 )];
    [path addClip];

今天咱们要使用个CALayer的强大属性mask属性,好像不对,上篇文章滑动解锁好像就使用了,今天咱们接着用~

- (void)drawRect:(CGRect)rect {
     //设置phoneLayer的图片
        self.phoneLayer.contents = (__bridge id _Nullable)(_image.CGImage);
     
        self.phoneLayer.mask = self.maskLayer;
        
        [self addSubview:self.label];
  }

-(void)layoutSubviews{
        
        NSLog(@"%@",NSStringFromCGRect(self.bounds));
        
        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
        self.circleLayer.strokeColor = [UIColor whiteColor].CGColor;
        
        self.circleLayer.path = path.CGPath;
        self.circleLayer.lineWidth = _lineWidth;
        
        self.maskLayer.path = self.circleLayer.path;
        self.maskLayer.position = CGPointMake(0, 0);
        
    }

-2.前进动画

 CGPoint originalCenter = self.center;

//前进
[UIView animateWithDuration:_animationDuration delay:0.0 usingSpringWithDamping:0.8 initialSpringVelocity:0.0 options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
    CGRect rect = self.frame;
    rect.origin.x += boundsOffset;
    self.frame = rect;
    
} completion:^(BOOL finished) {
    //将圆角图片变成方角图片
    [self animateToSquare];
    
}];

//图片变成方形
 -(void)animateToSquare{
        
        _isSquare = YES;
        
        UIBezierPath *squarePath = [UIBezierPath bezierPathWithRect:self.bounds];
        CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"];
        morph.duration = 0.25;
        morph.fromValue = (__bridge id _Nullable)(self.circleLayer.path);
        morph.toValue = (__bridge id _Nullable)(squarePath.CGPath);
       
        [self.circleLayer addAnimation:morph forKey:nil];
        [self.maskLayer addAnimation:morph forKey:nil];
        self.circleLayer.backgroundColor = [UIColor magentaColor].CGColor;
        self.circleLayer.path = squarePath.CGPath;
        self.maskLayer.path = squarePath.CGPath;
        
  }

2.后退动画

  [UIView animateWithDuration:_animationDuration delay:_animationDuration usingSpringWithDamping:0.7 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.center = originalCenter;
        
    } completion:^(BOOL finished) {
        //个人感觉这块没用,因为前进后就直接变成方形了,不需要在判断是否是方形了
        
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            if(!self.isSquare){
//                [self boundsOffSet:boundsOffset morphSize:morphSize];
//            }
//        });
    }];

3.碰撞动画

  //碰撞效果

    CGRect morphedFrame = originalCenter.x > boundsOffset ? CGRectMake(0.0, self.bounds.size.height - morphSize.height,morphSize.width, morphSize.height) : CGRectMake(self.bounds.size.width - morphSize.width, self.bounds.size.height - morphSize.height, morphSize.width, morphSize.height);
    
    CABasicAnimation *morphAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    morphAnimation.duration = _animationDuration;
    morphAnimation.toValue = (__bridge id _Nullable)([UIBezierPath bezierPathWithOvalInRect:morphedFrame].CGPath);
    
    
    morphAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.circleLayer addAnimation:morphAnimation forKey:nil];
    [self.maskLayer addAnimation:morphAnimation forKey:nil];

相关文章

  • iOS动画 狗和狐狸的对战

    年前的最后一篇了,希望年前收获的喜欢可以突破100,给去年个圆满的结尾当然实现不了,我也很高兴的,比较已经收获了九...

  • iOS动画

    iOS动画-从UIView动画说起iOS动画-Transform和KeyFrame动画iOS动画-layout动画...

  • 狗和狐狸

    ——学习非暴力沟通心得 今天看到两个视频新闻:一个是,一条小狗遵守交规,走斑马线过马路,被一辆违规闯红灯的轿车给撞...

  • 狗和狐狸

    大山深处只有狗和狐狸是形影不离的好朋友,慢慢的狗深深地爱上了狐狸。 正在他们热恋时却遇到了死神。死神说:“你们两个...

  • 厚道的人总是吃亏吗?

    原创 文/千益读 狗和狐狸是好朋友。狐狸精明,狗很傻。平常,狗习惯于吃亏,狐狸一直沾光。 这天他们遇到了死神。死神...

  • ios动画

    ios动画 ios动画2 ios动画3

  • iOS动画

    iOS动画分为基础动画和核心动画 本文发布在http://he8090.cn/2016/07/18/iOS动画/...

  • 狗、公鸡和狐狸

    狗与公鸡结交为朋友,他们一同赶路。到了晚上,公鸡一跃跳到树上,在树枝上栖息,狗就在下面树洞里过夜。黎明到来...

  • 狗、公鸡和狐狸

    在很远很远的地方,有一片美丽的大森林,大森林里住着许多可爱的动物,他们有猴子领导,在这个王国大家相亲相爱就是一家人...

  • 狗、鸡和狐狸

    很久很久以前,有一个美丽、宁静的村庄,那里的人们过着与世无争的生活。通过辛勤的劳动,这个村庄充裕且祥和。这...

网友评论

  • 471c5d91db13:楼主,你好,问一下关于三个layer的思路?就是布局的思路
    e40c669177be:@简书新 没事的,新年快乐,也是我写的不清楚
    471c5d91db13:@ayilimi 谢谢你的回复,明白了。提前祝你新年快乐
    e40c669177be:phoneLayer 是用来盛放底部的图片的
    circleLayer 是用来画出图片的白色边框
    maskLayer这个就是裁剪超过他区域范围内的phoneLayer
    不知道这样解释能不能看懂,语言表达能力有点差

本文标题:iOS动画 狗和狐狸的对战

本文链接:https://www.haomeiwen.com/subject/ysizbttx.html