iOS 模仿微信红包动画

2017-08-21  本文已影响0人  WS_0909
//
//  RedPacketVC.m
//  红包
//
//  Created by Sen wang on 2017/8/15.
//  Copyright © 2017年 王森. All rights reserved.
//

#import "RedPacketVC.h"

#import "UIView+Extension.h"

#define kMain_Width [UIScreen mainScreen].bounds.size.width
#define kMain_Height [UIScreen mainScreen].bounds.size.height

#define kiPhone4s ((kMain_Height == 480) ? YES : NO)

#define kiPhone5s ((kMain_Height == 568) ? YES : NO)

#define kiPhone6s_7s ((kMain_Height == 667) ? YES : NO)

#define kiPhone6sP_7sP ((kMain_Height == 736) ? YES : NO)

@interface RedPacketVC ()

@property (nonatomic, strong) UIImageView *imageTop;

@property (nonatomic, strong) UIImageView *imageBottom;

@property (nonatomic, strong) UIButton *sendBtn;

@end

@implementation RedPacketVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self wxRedPacket];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/** * 仿微信红包 */
- (void)wxRedPacket {
    //深色背景

    self.imageTop = [[UIImageView alloc] init];
    self.imageTop.frame = CGRectMake(0, 0, kMain_Width, kMain_Height * 0.4);
    self.imageTop.image = [UIImage imageNamed:@"shanghong"];

    //发红包按钮
    self.sendBtn =
        [[UIButton alloc] initWithFrame:CGRectMake((kMain_Width - 100) / 2, self.imageTop.height - 50, 100, 100)];
    _sendBtn.layer.masksToBounds = YES;
    _sendBtn.layer.cornerRadius = _sendBtn.bounds.size.height / 2;

    [_sendBtn setBackgroundImage:[UIImage imageNamed:@"开"] forState:UIControlStateNormal];

    [_sendBtn addTarget:self action:@selector(sendBtnAction:) forControlEvents:UIControlEventTouchUpInside];

    self.imageBottom = [[UIImageView alloc] init];
    CGFloat height1 = 15;

    if (kiPhone5s) {
        height1 = 1;
    }
    if (kiPhone6s_7s) {
        height1 = 8;
    }
    if (kiPhone6sP_7sP) {
        height1 = 15;
    }
    self.imageBottom.frame = CGRectMake(0, _sendBtn.y - height1, kMain_Width, kMain_Height * 0.7);
    self.imageBottom.image = [UIImage imageNamed:@"xiahong"];

    [self.view addSubview:self.imageTop];
    [self.view addSubview:self.imageBottom];
    [self.view addSubview:_sendBtn];
}

- (void)sendBtnAction:(UIButton *)sendBtn {
    
    CABasicAnimation *transformAnima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];

    transformAnima.toValue = [NSNumber numberWithFloat:M_PI];
    transformAnima.duration = 0.6;
    transformAnima.cumulative = NO;
    //    动画结束时是否执行逆动画
    transformAnima.autoreverses = YES;
    

    
    transformAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    sendBtn.layer.zPosition = 5;
    sendBtn.layer.zPosition = sendBtn.layer.frame.size.width / 2.f;
    transformAnima.repeatCount = MAXFLOAT;
    [sendBtn.layer addAnimation:transformAnima forKey:@"rotationAnimationY"];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self updataFrame];
    });

}

- (void)updataFrame {
    
    CGFloat topDistance = -self.imageTop.height / 2;

    [self.sendBtn removeFromSuperview];
    self.sendBtn =
    [[UIButton alloc] initWithFrame:CGRectMake((kMain_Width - 100) / 2, self.imageTop.height - 50, 100, 100)];
    [_sendBtn setBackgroundImage:[UIImage imageNamed:@"开"] forState:UIControlStateNormal];
    [self.view addSubview:_sendBtn];
    
    
    [UIView animateWithDuration:0.4
                     animations:^{
                         self.imageTop.y = topDistance;
                         self.sendBtn.y = -topDistance - 50;
                         self.imageBottom.y = kMain_Height;

                     }];
  
}



@end

上一篇 下一篇

猜你喜欢

热点阅读