我們都知道iOS9 CASpringAnimation 彈簧動畫渔嚷,操作簡單肢专,容易實現(xiàn)妻枕,目前使用也比較廣泛,下面我給大家介紹一種pop的彈簧效果葛账,跟CASpringAnimation 彈簧動畫 很相似
CocaPods加載 pod 'pop', '~> 1.0.9'
首先我們先看下動畫的效果
具體實現(xiàn)如下
#import "CSPlusVC.h"
#import "CSVerticalButton.h" //自定義封裝的button
#import <POP.h>
@implementation CSPlusVC
- (void)viewDidLoad {
[super viewDidLoad];
[self addVerticalButtons];
}
- (void)addVerticalButtons{
//點擊之前 關閉交互
self.view.userInteractionEnabled = NO;
//創(chuàng)建兩個數組 分別存button上image的名字和title的名字
NSArray *imagesArray = @[@"renwu",@"shuju",@"shudu",@"xianhua",@"yanlun",@"quantou"];
NSArray *titleArray = @[@"人物",@"數據",@"數讀",@"閑話",@"言論",@"拳頭"];
// kWidth為整屏寬柠衅,kHeight為整屏高
CGFloat buttonW = kWidth*0.2;
CGFloat buttonH = buttonW + 0.04*kHeight;
CGFloat buttonStartY = (kHeight - 2*buttonW)*0.5;//buttonY的開始位置
CGFloat buttonStartX = 20;
int maxCols = 3;//每行最多三個
CGFloat xMargin = (kWidth - 2*buttonStartX - maxCols *buttonW)/(maxCols-1);
for (int i=0;i<6;i++) {
//添加6個button
CSVerticalButton *button = [[CSVerticalButton alloc] init];
//設置button的相關屬性
button.titleLabel.font = [UIFont systemFontOfSize:15];
[button setTitle:titleArray[i] forState:(UIControlStateNormal)];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:imagesArray[i]] forState:(UIControlStateNormal)];
button.tag = i; //原始方法 給button一個tag值 用與后面點擊方法
[button addTarget:self action:@selector(clickButtonAciton:) forControlEvents:(UIControlEventTouchUpInside)];
int row = i/maxCols; //行
int col = i%maxCols; //列
CGFloat buttonX = buttonStartX+col*(buttonW+xMargin);
CGFloat buttonEndY = buttonStartY +row*(buttonH+10);
CGFloat buttonBeginY = buttonEndY -kHeight;
[self.view addSubview:button];
//創(chuàng)建POPSpringAnimation動畫 對象 動畫結束位置就是button最終frame的位置 所以不需要一開始設置button在window中的位置 這也是跟CASpringAnimation的區(qū)別
POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame]; //改變frame
anim.fromValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonBeginY, buttonW, buttonH)];//動畫開始位置
anim.toValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonEndY, buttonW, buttonH)];//動畫結束位置
anim.springBounciness = 15;//彈簧反彈彈度[4,20]
anim.springSpeed = 15;//彈簧速度[4,20]
anim.beginTime = CACurrentMediaTime()+0.1*i;//動畫開始時間
//動畫結束后 打開用戶交互界面
[anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
self.view.userInteractionEnabled = YES;
}];
[button pop_addAnimation:anim forKey:nil];
}
// 添加標語
UIImageView *sloganView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"app_slogan"]];
[self.view addSubview:sloganView];
// 標語動畫
POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter]; //改變center位置
CGFloat centerX = kWidth * 0.5;
CGFloat centerEndY = kHeight * 0.2;
CGFloat centerBeginY = centerEndY - kHeight;
anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(centerX, centerBeginY)];
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(centerX, centerEndY)];
anim.springBounciness = 15;
anim.springSpeed = 15;
anim.beginTime = CACurrentMediaTime() + imagesArray.count * 0.1;
[anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
// 標語動畫執(zhí)行完畢, 恢復點擊事件
self.view.userInteractionEnabled = YES;
}];
[sloganView pop_addAnimation:anim forKey:nil];
}
//取消按鈕的方法
- (IBAction)cancelAction:(id)sender {
[self cancelWithCompletionBlock:nil];
}
//取消的block方法
- (void)cancelWithCompletionBlock:(void(^)())completionBlock
{
//關閉交互
self.view.userInteractionEnabled = NO;
int index = 0;
//遍歷父視圖 用于設置頁面消失時的動畫
for (UIButton *button in self.view.subviews) {
if ([button isKindOfClass:[CSVerticalButton class]]) {
//給button設置下墜動畫 用最普通的POPBasicAnimation就可以
POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
CGFloat centerY = button.center.y + kHeight;
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(button.center.x, centerY)];
anim.beginTime = CACurrentMediaTime()+ index*0.1;
[button pop_addAnimation:anim forKey:nil];
index++;
if (index==5) { //在最后一個動畫執(zhí)行完畢后 ,頁面退出 籍琳,然后調用block 菲宴,block里執(zhí)行要點擊的事件
[anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
[self dismissViewControllerAnimated:NO completion:nil];
if (completionBlock) {
completionBlock();
}
}];
}
}
}
}
相信我寫的已經很詳細了,如有問提趋急,請留言