ios15 pop框架的使用
主要源码
//
// TestPopVC.m
// podDemos
//
// Created by lujun on 2022/1/31.
//
#import "TestPopVC.h"
#import "BSVerticalButton.h"
#define BSScreenW [UIScreen mainScreen].bounds.size.width
#define BSScreenH [UIScreen mainScreen].bounds.size.height
#import <POP.h>
#import "UIView+Frame.h"
@interface TestPopVC ()
@property(nonatomic,weak)UIImageView *sloganView;
@end
@implementation TestPopVC
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *ivView = [[UIImageView alloc]init];
ivView.image = [UIImage imageNamed:@"shareBottomBackground"];
ivView.frame = self.view.bounds;
[self.view addSubview:ivView];
UIButton *calBtn = [[UIButton alloc]init];
[calBtn setImage:[UIImage imageNamed:@"shareButtonCancel"] forState:UIControlStateNormal];
[calBtn setImage:[UIImage imageNamed:@"shareButtonCancelClick"] forState:UIControlStateHighlighted];
calBtn.height = 100;
[calBtn addTarget:self action:@selector(cancelTest) forControlEvents:UIControlEventTouchUpInside];
calBtn.width = BSScreenW;
calBtn.x = 0;
calBtn.y = BSScreenH - calBtn.height - 20;
[self.view addSubview:calBtn];
//添加6个按钮
NSArray *name = @[@"发视频",@"发图片",@"发段子",@"发声音",@"审帖",@"离线下载"];
NSArray *imageName = @[@"publish-video",@"publish-picture",@"publish-text",@"publish-audio",@"publish-review",@"publish-offline"];
NSUInteger count = name.count;
NSUInteger maxCols = 3;
CGFloat buttonW = 72;
CGFloat buttonH = buttonW + 50;
CGFloat buttonStartX = 20;
CGFloat buttonStartY = (BSScreenH-2*buttonH)*0.5;
CGFloat buttonMargin = (BSScreenW-2*buttonStartX-buttonW*maxCols)/(maxCols-1);
for (NSUInteger i = 0; i < count; i++) {
BSVerticalButton *button = [[BSVerticalButton alloc]init];
[button setImage:[UIImage imageNamed:imageName[i]] forState:UIControlStateNormal];
[button setTitle:name[i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14];
button.tag = i;
[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//计算frame
NSUInteger row = i/maxCols;
NSUInteger col = i%maxCols;
CGFloat buttonX = buttonStartX + col*(buttonW+buttonMargin);
CGFloat buttonY = buttonStartY + row*buttonH;
//使用pop框架,改变frame往下掉,而且不需要再设置frame了,因为pop改变了frame
POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
anim.fromValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonY-BSScreenH, buttonW, buttonH)];
anim.toValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonY, buttonW, buttonH)];
anim.springBounciness = 8;
anim.springSpeed = 8;
anim.beginTime = CACurrentMediaTime()+0.1*i;
[button pop_addAnimation:anim forKey:nil];
}
//添加顶部图片
UIImageView *slogan = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"app_slogan"]];
self.sloganView = slogan;
[self.view addSubview:slogan];
//中心约束
POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(BSScreenW*0.5, BSScreenH*0.2-BSScreenH)];
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(BSScreenW*0.5, BSScreenH*0.2)];
anim.springBounciness = 8;
anim.springSpeed = 8;
anim.beginTime = CACurrentMediaTime()+0.1*count;
[slogan pop_addAnimation:anim forKey:nil];
}
-(void)btnClick:(UIButton *)sender{
NSLog(@"%ld",sender.tag);
}
-(void)cancelTest{
[self cancel:nil];
}
- (void)cancel:(void(^)(int))completeion{
//让按钮往下掉
NSUInteger index = 2;
NSUInteger count = self.view.subviews.count;
for (NSUInteger i = index; i<count ; i++) {
UIView *subview = self.view.subviews[i];
//这里不需要计算frame,因为已经添加到view中,只需要设置最后的位置
POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(subview.centerX, subview.centerY+BSScreenH)];
anim.beginTime = CACurrentMediaTime()+0.1*i;
[subview pop_addAnimation:anim forKey:nil];
if (i == count-1) {
//最后一个控件pop完退出控制器,写在外面会直接退出
[anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
if (completeion) {
}
[self dismissViewControllerAnimated:NO completion:nil];
}];
}
}
}
@end
按钮的源码
//
// BSVerticalButton.m
// podDemos
//
// Created by lujun on 2022/1/31.
//
#import "BSVerticalButton.h"
#import "UIView+Frame.h"
@implementation BSVerticalButton
- (instancetype)initWithFrame:(CGRect)frame {
if (self == [super initWithFrame:frame]) {
self.titleLabel.textAlignment = NSTextAlignmentCenter;
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
}
//这个是控件重新布局,所以要调用layoutSubviews,而修改控件大小,是调用setFrame
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.x = 0;
self.imageView.y = 0;
self.imageView.width = self.width;
self.imageView.height = self.imageView.width;
self.titleLabel.x = 0;
self.titleLabel.y = self.imageView.height;
self.titleLabel.width = self.width;
self.titleLabel.height = self.height - self.width;
}
- (void)setFrame:(CGRect)frame {
[super setFrame:frame];
}
@end