iOS 微信拼多多開紅包動(dòng)畫

redbag.gif

模仿一波拼多多的開紅包.

主要用的就是
CAShapeLayer繪制
UIBezierPath繪制(控制點(diǎn)起點(diǎn)和終點(diǎn))
CABasicAnimation基本動(dòng)畫

未開啟動(dòng)畫前,繪制兩個(gè)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);
    //曲線起點(diǎn)
    [path moveToPoint:startPoint];
    //曲線終點(diǎn)和控制基點(diǎn)
    [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動(dòng)畫

/***  添加縮放動(dòng)畫 */

- (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"];
    
    
}

/*** 給按鈕添加了一個(gè)光暈 添加外環(huán) */

- (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"];

}

其余的幾個(gè)文案顯示加的是label

/***  添加恭喜發(fā)財(cái)文案 */

- (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 = @"點(diǎn)開即可獲得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 = @"恭喜發(fā)財(cái),大吉大利";
    _botText.tag = 102;
    _botText.textColor = [UIColor colorWithHexString:@"#e0ba88"];
    _botText.font = [UIFont boldSystemFontOfSize:20];
    _botText.textAlignment = NSTextAlignmentCenter;
    _botText.layer.zPosition = 3;
    [self addSubview:_botText];
}

點(diǎn)開動(dòng)畫

/***  開紅包后移動(dòng) */

- (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);
            //曲線起點(diǎn)
            [newPath moveToPoint:startPoint];
            //曲線終點(diǎn)和控制基點(diǎn)
            [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:@"您要是不想使用可以轉(zhuǎn)給我喲"];
        
            [self.lineLayer addAnimation:anims forKey:nil];
        });
    
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容