因为公司项目的需求,需要一个弧形滚动的菜单,就写了一个demo。
// Created by Flynn Lee on 2021/12/5.
//
#import "ViewController.h"
#import "UIView+viewCategory.h"
#define KScreenHeight [UIScreen mainScreen].bounds.size.height
#define KScreenWidth [UIScreen mainScreen].bounds.size.width
@interface ViewController ()<UIScrollViewDelegate>
///滚动scrollView
@property(nonatomic,strong)UIScrollView *scrollView;
///发布按钮
@property(nonatomic,strong)UIButton *publishButton;
///存放cell的数组
@property(nonatomic,strong)NSMutableArray<UIView *> *cells;
@end
@implementation ViewController
-(UIScrollView *)scrollView
{
if (!_scrollView) {
_scrollView = [[UIScrollView alloc]init];
_scrollView.X = KScreenWidth;
_scrollView.Y = KScreenHeight;
_scrollView.Size = CGSizeZero;
_scrollView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
_scrollView.delegate = self;
_scrollView.contentSize = CGSizeMake(0, 95 * 50);
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(scrollViewRemove)];
[_scrollView addGestureRecognizer:tap];
}
return _scrollView;
}
-(UIButton *)publishButton
{
if (!_publishButton) {
_publishButton = [[UIButton alloc]init];
_publishButton.Size = CGSizeMake(80, 80);
_publishButton.X = KScreenWidth - 100;
_publishButton.Y = KScreenHeight - 120;
_publishButton.backgroundColor = [UIColor purpleColor];
[_publishButton setTitle:@"发布" forState:UIControlStateNormal];
_publishButton.layer.masksToBounds = YES;
_publishButton.layer.cornerRadius = 40;
[_publishButton addTarget:self action:@selector(publishButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}
return _publishButton;
}
-(NSMutableArray<UIView *> *)cells
{
if (!_cells) {
_cells = [NSMutableArray array];
}
return _cells;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.publishButton];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self.cells enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
CGRect objFrame = [self.scrollView convertRect:obj.frame toView:self.view];
if (obj.X < 0) {
obj.X = 0;
}else{
obj.X = (objFrame.origin.y < KScreenHeight/2) ? objFrame.origin.y/KScreenHeight * KScreenWidth : (1-objFrame.origin.y/KScreenHeight) * KScreenWidth;
}
obj.Width = KScreenWidth - obj.X;
}];
}
-(void)publishButtonClick:(UIButton *)button
{
[self.view addSubview:self.scrollView];
[self createCell];
[UIView animateWithDuration:0.5 animations:^{
self.scrollView.frame = [UIScreen mainScreen].bounds;
}];
}
-(void)scrollViewRemove
{
[UIView animateWithDuration:0.5 animations:^{
self.scrollView.X = self.view.Width;
self.scrollView.Y = self.view.Height;
self.scrollView.Size = CGSizeMake(0, 0);
} completion:^(BOOL finished) {
[self.scrollView removeFromSuperview];
self.scrollView = nil;
if (self.cells.count > 0) {
[self.cells makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
}];
}
-(void)createCell
{
[self.cells removeAllObjects];
CGFloat cellX = 0;
CGFloat cellY = 0;
CGFloat cellW = 0;
CGFloat cellH = 80;
CGFloat margin = 15;
for (int i = 0; i < 50; i++) {
cellY = CGRectGetMaxY([self.cells lastObject].frame) + margin;
cellX = (cellY < KScreenHeight/2) ? cellY/KScreenHeight * KScreenWidth : (1-cellY/KScreenHeight) * KScreenWidth;
cellW = KScreenWidth - cellX;
UIView *cell = [[UIView alloc]init];
cell.frame = CGRectMake(cellX, cellY, cellW, cellH);
cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
[self.scrollView addSubview:cell];
[self.cells addObject:cell];
}
}
@end