iOS 微信拼多多开红包动画

redbag.gif

模仿一波拼多多的开红包.

主要用的就是
CAShapeLayer绘制
UIBezierPath绘制(控制点起点和终点)
CABasicAnimation基本动画

未开启动画前,绘制两个CAShapeLayer显示不同的层次感.

- (void)config{
    _redLayer = [[CAShapeLayer alloc] init];
    UIBezierPath *pathFang = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 10, self.frame.size.width -40, self.frame.size.height-20) cornerRadius:4];
    _redLayer.path = pathFang.CGPath;
    _redLayer.zPosition = 1;
    [self.layer addSublayer:_redLayer];
    [_redLayer setFillColor:[UIColor colorWithRed:0.7968 green:0.2186 blue:0.204 alpha:1.0].CGColor];//深色背景
    
    _lineLayer = [[CAShapeLayer alloc] init];
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 10, self.frame.size.width-40, self.frame.size.height*close_rate) byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(4, 4)];
    CGPoint startPoint = CGPointMake(20, self.frame.size.height*close_bot_rate+10);
    CGPoint endPoint = CGPointMake(self.frame.size.width-20, self.frame.size.height*close_bot_rate+10);
    CGPoint controlPoint = CGPointMake(self.frame.size.width*0.5, self.frame.size.height*close__bot_rate);
    //曲线起点
    [path moveToPoint:startPoint];
    //曲线终点和控制基点
    [path addQuadCurveToPoint:endPoint controlPoint:controlPoint];

    //曲线部分颜色和阴影
    [_lineLayer setFillColor:[UIColor colorWithRed:0.851 green:0.3216 blue:0.2784 alpha:1.0].CGColor];
    [_lineLayer setStrokeColor:[UIColor colorWithRed:0.9401 green:0.0 blue:0.0247 alpha:0.02].CGColor];
    [_lineLayer setShadowColor:[UIColor blackColor].CGColor];
    [_lineLayer setLineWidth:0.1];
    [_lineLayer setShadowOffset:CGSizeMake(6, 6)];
    [_lineLayer setShadowOpacity:0.2];
    [_lineLayer setShadowOffset:CGSizeMake(1, 1)];
    _lineLayer.path = path.CGPath;
    _lineLayer.zPosition = 1;
    [self.layer addSublayer:_lineLayer];
    
    
    [self addTopText];
    [self addOpenButton];
    
}

按钮添加的是CABasicAnimation动画

/***  添加缩放动画 */

- (void)addGrowAnimation:(UIButton*)sender{
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.duration = 0.5;
    scaleAnimation.fromValue=@(0.8);
    scaleAnimation.toValue=@(1.1);
    scaleAnimation.repeatCount = MAXFLOAT;
    scaleAnimation.autoreverses = YES;
    scaleAnimation.removedOnCompletion = NO;
    scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [sender.layer addAnimation:scaleAnimation forKey:@"scaleAnimationDemo"];
    
    
}

/*** 给按钮添加了一个光晕 添加外环 */

- (void)addFlowAnimation:(UIButton *)sender{
    
    sender.layer.shadowPath  = [UIBezierPath bezierPathWithRoundedRect:sender.bounds byRoundingCorners:(UIRectCornerAllCorners) cornerRadii:CGSizeMake(sender.layer.cornerRadius, sender.layer.cornerRadius)].CGPath;
    sender.layer.shadowColor = [UIColor colorWithHexString:@"#CD7F32"].CGColor;
    sender.layer.shadowOffset = CGSizeZero;
    sender.layer.shadowRadius = 10;
    sender.layer.shadowOpacity = 1;
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"shadowRadius"];
    animation.fromValue = @(10);
    animation.toValue = @(0);
    animation.autoreverses = YES;
    animation.additive = NO;
    animation.duration = 1.5;
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    animation.repeatCount = INFINITY;
    [sender.layer addAnimation: animation forKey:@"glowingAnimation"];

}

其余的几个文案显示加的是label

/***  添加恭喜发财文案 */

- (void)addTopText{
    _topText = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height*0.2, self.frame.size.width, 30)];
    _topText.text = @"五亿好礼开出来";
    _topText.tag = 100;
    _topText.textColor = [UIColor colorWithHexString:@"#e0ba88"];
    _topText.font = [UIFont boldSystemFontOfSize:25];
    _topText.textAlignment = NSTextAlignmentCenter;
    _topText.layer.zPosition = 3;
    [self addSubview:_topText];
    
    _tips = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_topText.frame)+10, self.frame.size.width, 30)];
    _tips.text = @"点开即可获得100元大礼包";
    _tips.tag = 101;
    _tips.textColor = [UIColor colorWithHexString:@"#e0ba88"];
    _tips.font = [UIFont boldSystemFontOfSize:12];
    _tips.textAlignment = NSTextAlignmentCenter;
    _tips.layer.zPosition = 3;
    [self addSubview:_tips];
    
    _botText = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_tips.frame)+20, self.frame.size.width, 30)];
    _botText.text = @"恭喜发财,大吉大利";
    _botText.tag = 102;
    _botText.textColor = [UIColor colorWithHexString:@"#e0ba88"];
    _botText.font = [UIFont boldSystemFontOfSize:20];
    _botText.textAlignment = NSTextAlignmentCenter;
    _botText.layer.zPosition = 3;
    [self addSubview:_botText];
}

点开动画

/***  开红包后移动 */

- (void)moveAnimation:(UIButton*)sender{
    CABasicAnimation *transformAnima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    // transformAnima.fromValue = @(M_PI_2);
    transformAnima.toValue = [NSNumber numberWithFloat: M_PI];
    transformAnima.duration = 0.5;
    transformAnima.cumulative = YES;
    transformAnima.autoreverses = NO;
    transformAnima.repeatCount = HUGE_VALF;
    transformAnima.fillMode = kCAFillModeForwards;
    transformAnima.removedOnCompletion = NO;
    transformAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    sender.layer.zPosition = 5;
    sender.layer.zPosition = sender.layer.frame.size.width/2.f;
    [sender.layer addAnimation:transformAnima forKey:@"rotationAnimationY"];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [sender removeFromSuperview];
            
            UIBezierPath *newPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 10, self.frame.size.width-40, self.frame.size.height*open_rate) byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(4, 4)];
            CGPoint startPoint = CGPointMake(20,  self.frame.size.height*open_rate+10);
            CGPoint endPoint = CGPointMake(self.frame.size.width-20, self.frame.size.height*open_rate+10);
            CGPoint controlPoint = CGPointMake(self.frame.size.width*0.5, self.frame.size.height*open__rate);
            //曲线起点
            [newPath moveToPoint:startPoint];
            //曲线终点和控制基点
            [newPath addQuadCurveToPoint:endPoint controlPoint:controlPoint];
            
            CGRect newFrame = CGRectMake(20, 10, self.frame.size.width-40, self.frame.size.height*open__rate);
            
            CABasicAnimation* pathAnim = [CABasicAnimation animationWithKeyPath: @"path"];
            pathAnim.toValue = (id)newPath.CGPath;
            
            CABasicAnimation* boundsAnim = [CABasicAnimation animationWithKeyPath: @"frame"];
            boundsAnim.toValue = [NSValue valueWithCGRect:newFrame];
            
            CAAnimationGroup *anims = [CAAnimationGroup animation];
            anims.animations = [NSArray arrayWithObjects:pathAnim, boundsAnim, nil];
            anims.removedOnCompletion = NO;
            anims.duration = 0.1f;
            anims.fillMode  = kCAFillModeForwards;
            
            [self.topText setText:@"恭喜您获得"];
        
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"¥20.88"];
            [str addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15] range:NSMakeRange(0, 1)];

            self.botText.frame  = CGRectMake(0, self.frame.size.height*open__rate+20, self.frame.size.width, 35);
            [self.botText setFont:[UIFont boldSystemFontOfSize:30]];
            [self.botText setAttributedText:str];
            [self addGrowAnimation:self.botText];
        
            self.tips.frame  = CGRectMake(0, self.frame.size.height*close__bot_rate+20, self.frame.size.width, 20);
            [self.tips setText:@"您要是不想使用可以转给我哟"];
        
            [self.lineLayer addAnimation:anims forKey:nil];
        });
    
}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,383评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,522评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,852评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,621评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,741评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,929评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,076评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,803评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,265评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,582评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,716评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,395评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,039评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,798评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,027评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,488评论 2 361
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,612评论 2 350

推荐阅读更多精彩内容