1.中间的彩色按钮是用POP做的弹性动画依次下落到指定位置,并有弹簧效果
2.彩色Button为自定义button
3.透明背景为自定义window,在modal情况下,覆盖的控制器会被移除,在dismiss时重新添加,因此改为添加一个新的窗口;
4.在移除动画时,用block回调,执行按钮点击后的操作
#import "ZHPublicView.h"
#import <POP.h>
#import "ZHbutton.h"
@implementation ZHPublicView
//点击底部加号按钮,对外接口方法
+(void)show{
UIView *publicView = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil]firstObject];
publicView.frame = window_.bounds;
[window_ addSubview:publicView];
}
static UIWindow *window_;
//一次性操作
-(void)awakeFromNib{
window_ = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
window_.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.8];
window_.hidden = NO;
self.userInteractionEnabled = NO;
[self setUptext];
[self addAllChildButton];
}
//添加子控件,图片为本地bundle图片
-(void)addAllChildButton{
NSArray *title = @[@"发视频",@"发图片",@"发段子",@"发声音",@"审帖",@"离线下载"];
NSArray *image = @[@"publish-video",@"publish-picture",@"publish-text",@"publish-audio",@"publish-review",@"publish-offline"];
//按钮的frame
NSInteger col = 3;
CGFloat margin = 10;
CGFloat buttonW = 80;
CGFloat buttonH = 100;
CGFloat marginX = ([UIScreen mainScreen].bounds.size.width - 2 * margin - col * buttonW) / 2;
CGFloat beginY = [UIScreen mainScreen].bounds.size.height * 0.5 - buttonH;
for (NSInteger i = 0; i < 6; i++) {
CGFloat x = margin + (buttonW + marginX)*(i % col);
CGFloat y = beginY + (buttonH + margin)*(i / col);
ZHbutton *button = [ZHbutton buttonWithType:UIButtonTypeCustom];
button.tag = i;
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:title[i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
CGRect beginFrame = CGRectMake(x, y - 375, buttonW, buttonH);;
CGRect endFrame = CGRectMake(x, y, buttonW, buttonH);
button.frame = endFrame;
[button setImage:[UIImage imageNamed:image[i]] forState:UIControlStateNormal];
[self addSubview:button];
//POP动画
POPSpringAnimation *spring = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
spring.fromValue = [NSValue valueWithCGRect:beginFrame];
spring.toValue = [NSValue valueWithCGRect:endFrame];
spring.springBounciness = 12;
spring.springSpeed = 8;
spring.beginTime = CACurrentMediaTime() + 0.05 * i;
[button pop_addAnimation:spring forKey:nil];
}
}
-(void)cancelButtonWithCompleBlock:(void (^)())completion{
for (int i = 1; i < self.subviews.count; i++) {
UIView *subView = self.subviews[i];
POPBasicAnimation *spring = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
CGFloat centerY = [UIScreen mainScreen].bounds.size.height + subView.ZH_Y;
spring.toValue = [NSValue valueWithCGPoint:CGPointMake(subView.ZH_centerX, centerY)];
spring.beginTime = CACurrentMediaTime() + i * 0.1;
[subView pop_addAnimation:spring forKey:nil];
if (i == self.subviews.count - 1) {
[spring setCompletionBlock:^(POPAnimation *anima, BOOL comple) {
window_.hidden = YES;
window_ = nil;
!completion ? :completion();
}];
}
}
}
-(void)buttonClick:(UIButton *)button{
[self cancelButtonWithCompleBlock:^{
NSLog(@"%ld",button.tag); //点击按钮操作事件
}];
}
-(void)setUptext{
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"app_slogan"]];
[self addSubview:imageView];
POPSpringAnimation *spring = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
CGFloat centerX = [UIScreen mainScreen].bounds.size.width * 0.5;
CGFloat centerY = [UIScreen mainScreen].bounds.size.height * 0.2;
spring.fromValue = [NSValue valueWithCGPoint:CGPointMake(centerX , centerY - 200)];
spring.toValue = [NSValue valueWithCGPoint:CGPointMake(centerX, centerY)];
spring.springBounciness = 20;
spring.springSpeed = 18;
spring.beginTime = CACurrentMediaTime() + 6 * 0.05;
[spring setCompletionBlock:^(POPAnimation * anima, BOOL complish) {
self.userInteractionEnabled = YES;
}];
[imageView pop_addAnimation:spring forKey:nil];
}
- (IBAction)cancelButton{
[self cancelButtonWithCompleBlock:nil];
}
@end
#import <UIKit/UIKit.h>
@interface ZHPublicView : UIView
+(void)show;
@end
/自定义button
#import "ZHbutton.h"
@implementation ZHbutton
-(void)layoutSubviews{
[super layoutSubviews];
self.imageView.ZH_Y = 0;
self.imageView.ZH_centerX = self.ZH_Width * 0.5;
self.titleLabel.ZH_Y = self.imageView.ZH_Height + 3;
[self.titleLabel sizeToFit];
self.titleLabel.ZH_centerX = self.ZH_Width * 0.5;
}
@end