记录一下代码:
HYNButtonAnimationView
//HYNButtonAnimationView.h
#import <UIKit/UIKit.h>
@interface HYNButtonAnimationView : UIButton
/// 初始化 必须调用此方法
- (id)initWithFrame:(CGRect)frame superView:(UIView*)superView ;
/// 主按钮图片
@property (nonatomic, strong) UIImage *homeBtnImage;
/// 主按钮旋转完成后图片
@property (nonatomic, strong) UIImage *finishImage;
/// 显示时的背景颜色
@property (nonatomic, strong) UIColor* showColor;
///// 弹出时间。默认0.2
@property (nonatomic, assign) CGFloat animateTime;
///// 是否弹出
@property (nonatomic,readonly,assign) BOOL isShowing;
@property (nonatomic,assign) BOOL show;
/// 显示子按钮
- (void)showItem;
/// 隐藏子按钮
- (void)finishedItem;
/// 显示
-(void)showInSuper;
/// 隐藏
-(void)hideFromSuper;
@end
//HYNButtonAnimationView.m
#define Main_Screen_Height [[UIScreen mainScreen] bounds].size.height
#define Main_Screen_Width [[UIScreen mainScreen] bounds].size.width
// 以4.7寸屏的宽度为基准,计算宽比和高比
#define SCREEN_WIDTH_RATIO47 Main_Screen_Width/375.0f
#import "HYNLayerBurron.h"
#import "HYNButtonAnimationView.h"
@interface HYNButtonAnimationView()
@property (nonatomic, strong) UIView* mySuper;//父视图
@property (nonatomic, strong) UIButton* backView;//背景视图
@property (nonatomic,strong) UIButton *subBtn;
@property (nonatomic, strong) NSTimer *time;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) int i;
@end
@implementation HYNButtonAnimationView
- (NSMutableArray *)dataArr {
if (!_dataArr) {
_dataArr = [NSMutableArray array];
}
return _dataArr;
}
-(UIView *)backView{
if (!_backView) {
_backView = [[UIButton alloc]init];
[_backView addTarget:self action:@selector(dismiss:) forControlEvents:UIControlEventTouchUpInside];
}
return _backView;
}
-(UIButton *)subBtn{
if (!_subBtn) {
_subBtn = [[UIButton alloc]init];
}
return _subBtn;
}
-(void)setShowColor:(UIColor *)showColor{
_showColor = showColor;
self.backView.backgroundColor = showColor;
}
- (id)initWithFrame:(CGRect)frame superView:(UIView*)superView
{
if (self = [super init])
{
_showColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
self.mySuper = superView;
self.backView.frame = superView.bounds;
self.backView.backgroundColor = _showColor;
self.backView.hidden = YES;
[superView addSubview:self.backView];
[superView addSubview:self];
self.frame = frame;
[self addTarget:self action:@selector(showItems:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.subBtn];
[self.subBtn addTarget:self action:@selector(showItems:) forControlEvents:UIControlEventTouchUpInside];
self.subBtn.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
}
return self;
}
-(void)setHomeBtnImage:(UIImage *)homeBtnImage{
_homeBtnImage = homeBtnImage;
[self.subBtn setImage:[homeBtnImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
}
-(void)setFinishImage:(UIImage *)finishImage{
_finishImage = finishImage;
}
-(void)dismiss:(UIButton*)tap{
[self hiddenButton];
}
-(void)showItems:(UIButton*)sender{
if (!_isShowing) {
[self showItem];
}else{
[self hiddenButton];
}
}
- (void)showItem
{
self.backView.hidden = NO;
// 把当前界面置于屏幕最前方
[self.mySuper bringSubviewToFront:self.backView];
[self.mySuper bringSubviewToFront:self];
self.backView.backgroundColor = _showColor;
if (_finishImage) {
[self setImage:_finishImage forState:0];
}
[UIView animateWithDuration:_animateTime animations:^{
CGAffineTransform rotate = CGAffineTransformMakeRotation( M_PI / 4 );
[self.subBtn setTransform:rotate];
} completion:^(BOOL finished) {
self.subBtn.alpha = 0;
}];
//创建UI
[self createUI];
_isShowing = YES;
}
- (void)finishedItem
{
self.backView.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0];
[UIView animateWithDuration:_animateTime animations:^{
CGAffineTransform rotate = CGAffineTransformMakeRotation( 0 );
[self.subBtn setTransform:rotate];
self.subBtn.alpha = 1;
} completion:^(BOOL finished) {
if (_finishImage) {
[self setImage:nil forState:0];
}
}];
self.backView.hidden = YES;
_isShowing = NO;
}
/// 在window里显示
-(void)showInSuper{
if (_mySuper) {
[_mySuper addSubview:self.backView];
[self.mySuper addSubview:self];
}else{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[window addSubview:self.backView];
[window addSubview:self];
}
}
/// 在window里隐藏
-(void)hideFromSuper{
if (self.superview) {
[self removeFromSuperview];
}
if (self.backView.superview) {
[self.backView removeFromSuperview];
}
[self finishedItem];
}
-(void)createUI
{
for (int i=0; i<6; i++) {
if(i>3){
break;
}
HYNLayerBurron *btn = [[HYNLayerBurron alloc] initWithFrame:CGRectMake(Main_Screen_Width, Main_Screen_Height-214*SCREEN_WIDTH_RATIO47-75*i, 200, 45)];
btn.tag = 3030+i;
[btn addTarget:self action:@selector(enterDifferentChose:) forControlEvents:UIControlEventTouchUpInside];
btn.photoImg.image = [UIImage imageNamed:@"PopView_more_orange"];
btn.title.text = @"更多社群";
[self.backView addSubview:btn];
[self.dataArr addObject:btn];
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2*i/*延迟执行时间*/ * NSEC_PER_SEC));
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
FH_LayerButton *btn = _dataArr[i];
[UIView animateWithDuration:0.25 animations:^{
btn.frame = CGRectMake(Main_Screen_Width/2-100,Main_Screen_Height-214*SCREEN_WIDTH_RATIO47-75*i, 200, 45);
}];
});
}
}
-(void)hiddenButton
{
for (int i=0; i<6; i++) {
if(i>3){
break;
}
FH_LayerButton *btn = _dataArr[i];
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2*i/*延迟执行时间*/ * NSEC_PER_SEC));
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.25 animations:^{
btn.frame = CGRectMake(Main_Screen_Width,Main_Screen_Height-214*SCREEN_WIDTH_RATIO47-75*i, 200, 45);
}];
if(self.dataArr.count>3){
if(i == 3){
[self finishedItem];
[self.dataArr removeAllObjects];
}
}else{
if(i == self.dataArr.count-1){
[self finishedItem];
[self.dataArr removeAllObjects];
}
}
});
}
}
-(void)enterDifferentChose:(FH_LayerButton *)sender
{
if(sender.tag == 3030){//底部第一个
}else if (sender.tag == 3031){//倒数第二个
}else if (sender.tag == 3032){//倒数第三个
}else{//查看更多
}
}
@end
HYNLayerBurron
//HYNLayerBurron.h
#import <UIKit/UIKit.h>
@interface HYNLayerBurron : UIButton
@property (nonatomic, strong) UIImageView *photoImg;
@property (nonatomic, strong) UILabel *title;
@end
//HYNLayerBurron.m
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define Main_Color [UIColor colorWithHexString:@"ff9e1b"]
#import "HYNLayerBurron.h"
@implementation HYNLayerBurron
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = UIColorFromRGB(0xff9e1b);
self.layer.cornerRadius = 23;
[self createuI];
}
return self;
}
- (void)createuI{
self.photoImg = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 34, 34)];
self.photoImg.layer.cornerRadius = self.photoImg.frame.size.height/2;
self.photoImg.layer.borderWidth = 3;
self.photoImg.clipsToBounds = YES;
self.photoImg.layer.borderColor = [UIColor colorWithRed:255/255.0 green:207/255.0 blue:141/255.0 alpha:1].CGColor;
self.photoImg.backgroundColor = [UIColor cyanColor];
[self addSubview:self.photoImg];
self.title = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxY(self.photoImg.frame)+10, 15, self.frame.size.width-CGRectGetMaxY(self.photoImg.frame)-34, 15)];
self.title.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15];
self.title.textColor = [UIColor whiteColor];
// self.title.backgroundColor = [UIColor cyanColor];
[self addSubview:self.title];
}
@end
Controller.m 中调用
#define Main_Screen_Height [[UIScreen mainScreen] bounds].size.height
#define Main_Screen_Width [[UIScreen mainScreen] bounds].size.width
#import "ViewController.h"
#import "HYNButtonAnimationView.h"
@interface ViewController ()
@property (nonatomic, strong) HYNButtonAnimationView *button;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"图片按钮";
_button = [[HYNButtonAnimationView alloc] initWithFrame:CGRectMake(Main_Screen_Width-83, Main_Screen_Height-150, 66, 66) superView:[UIApplication sharedApplication].keyWindow.rootViewController.view ];
[[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:_button];
_button.homeBtnImage = [UIImage imageNamed:@"Home_addbtn"];
_button.finishImage = [UIImage imageNamed:@"Home_addbtnClose"];
_button.showColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
[[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:_button];
}