上效果图
1.主要思路:首页建立一个悬浮Button给他加上移动手势(UIPanGestureRecognizer) 然后改变center传递给弹出页面;
2.代码
.1初始化button
self.xfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self.xfBtn.frame = CGRectMake(0, 0, 44*1.6, 44*1.6);
self.xfBtn.center = CGPointMake(SCREEN_WIDTH-40, SCREEN_HEIGHT-130-[GetDataNum getTabbarHeight]);
[self.view addSubview:self.xfBtn];
[self.xfBtn addTarget:self action:@selector(jumXfAct:) forControlEvents:UIControlEventTouchUpInside];
[self.xfBtn setBackgroundImage:[UIImage imageNamed:@"sy_new_xf_center"] forState:UIControlStateNormal];
UIPanGestureRecognizer *panG = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(xuanfuPanGesture:)];
[self.xfBtn addGestureRecognizer:panG];
.2处理手势
-(void)xuanfuPanGesture:(UIPanGestureRecognizer *)recognizer{
if (self.xfBtn == recognizer.view) {
CGPoint translation = [recognizer translationInView:self.view];
CGFloat centerX = self.xfBtn.center.x+translation.x;
self.xfBtn.center = CGPointMake(centerX, self.xfBtn.center.y+translation.y);
[recognizer setTranslation:CGPointZero inView:self.view];
CGFloat theCenterX = 0;
CGFloat theCenterY = self.xfBtn.center.y;
if (recognizer.state== UIGestureRecognizerStateEnded ||recognizer.state== UIGestureRecognizerStateCancelled) {
if (centerX>SCREEN_WIDTH/2.0) {
theCenterX = SCREEN_WIDTH-40;
}else{
theCenterX = 40;
}
if (self.xfBtn.center.y<[GetDataNum getStatusBarHight]+44+40) {
theCenterY = [GetDataNum getStatusBarHight]+44+40;
}
if (self.xfBtn.center.y>SCREEN_HEIGHT-[GetDataNum getTabbarHeight]-([GetDataNum getStatusBarHight]+44)-40) {
theCenterY = SCREEN_HEIGHT-[GetDataNum getTabbarHeight]-([GetDataNum getStatusBarHight]+44)-40;
}
[UIView animateWithDuration:0.3 animations:^{
self.xfBtn.center = CGPointMake(theCenterX, theCenterY);
}];
}
}
}
3.自定义alert
注意:弹出扇形菜单使用的是 HZYRoundShowButton 请取github上自行搜索下载
XuanFuBtnAlert.h
//
// XuanFuBtnAlert.h
// Zhouyi
//
// Created by mac_os on 2022/7/8.
//
#import <UIKit/UIKit.h>
#import "HZYRoundShowButton.h"
NS_ASSUME_NONNULL_BEGIN
@interface XuanFuBtnAlert : UIView
@property(nonatomic,copy) void (^actBlock)(BOOL isSure); //
@property (nonatomic, strong) UIView *contentView;
@property(nonatomic,strong) HZYRoundShowButton *roundBtn;
@property(nonatomic,assign) CGPoint centerPoint; //
@property(nonatomic,assign) BOOL isLeft; //
@property (nonatomic, assign) BOOL shouldDismissOnOutsideTapped;
- (void)showInView:(UIView *)view;
// Show in window
- (void)show;
// Dismiss the alert
- (void)dismiss;
@end
NS_ASSUME_NONNULL_END
XuanFuBtnAlert.m
#import "XuanFuBtnAlert.h"
#define IS_IOS_8_OR_HIGHER ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 8.0 )
@interface XuanFuBtnAlert ()<HZYRoundShowButtonDelegate>
{
CGRect ImgFrame;
}
@property (nonatomic, strong) UIView * alertContentView;
@property (nonatomic, strong) UIView * blackOpaqueView;
@property (nonatomic, assign) CGFloat alert_width;
@property (nonatomic, assign) CGFloat alert_height;
@property (nonatomic, assign) CGFloat dimAlpha;
@end
@implementation XuanFuBtnAlert
-(void)setCenterPoint:(CGPoint)centerPoint{
_centerPoint = centerPoint;
}
-(void)setIsLeft:(BOOL)isLeft{
_isLeft = isLeft;
}
- (id)initWithFrame:(CGRect)frame
{
self.alert_width = SCREEN_WIDTH;
self.alert_height = SCREEN_HEIGHT;
self = [super initWithFrame:CGRectMake(0, 0, self.alert_width, self.alert_height)];
if (self) {
// Initialization code
self.layer.cornerRadius = 8;
self.clipsToBounds = YES;
self.dimAlpha = 0.4;
// [self calculateFrame];
[self setupItems];
}
return self;
}
#pragma mark - Show the alert view
- (void)showInView:(UIView *)view
{
[self calculateFrame];
[self setupViews];
UIView *window = [[[UIApplication sharedApplication] delegate] window];
if (view != window) {
self.blackOpaqueView = [[UIView alloc] initWithFrame:window.bounds];
self.blackOpaqueView.backgroundColor = [UIColor colorWithWhite:0 alpha:self.dimAlpha];
UITapGestureRecognizer *outsideTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(outsideTap:)];
[self.blackOpaqueView addGestureRecognizer:outsideTapGesture];
[view addSubview:self.blackOpaqueView];
}
[self addThisViewToView:view];
}
// Show in window
- (void)show
{
UIView *superView;
UIView *window = [[[UIApplication sharedApplication] delegate] window];
if (IS_IOS_8_OR_HIGHER) {
superView = window;
} else {
superView = [[window subviews] lastObject];
}
self.blackOpaqueView = [[UIView alloc] initWithFrame:window.bounds];
self.blackOpaqueView.backgroundColor = [UIColor colorWithWhite:0 alpha:self.dimAlpha];
UITapGestureRecognizer *outsideTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(outsideTap:)];
[self.blackOpaqueView addGestureRecognizer:outsideTapGesture];
[superView addSubview:self.blackOpaqueView];
[self showInView:superView];
}
- (void)outsideTap:(UITapGestureRecognizer *)recognizer
{
[self dismiss];
}
- (void) addThisViewToView: (UIView *) view
{
NSTimeInterval timeAppear = .2;
NSTimeInterval timeDelay = 0;
[view addSubview:self];
self.transform = CGAffineTransformMakeScale(1.1, 1.1);
self.alpha = .6;
[UIView animateWithDuration:timeAppear delay:timeDelay options:UIViewAnimationOptionCurveEaseOut animations:^{
self.transform = CGAffineTransformIdentity;
self.alpha = 1;
} completion:^(BOOL finished){
[self didAppearAlertView];
}];
}
// Hide and dismiss the alert
- (void)dismiss
{
NSTimeInterval timeDisappear = .08;
NSTimeInterval timeDelay = .02;
self.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:timeDisappear delay:timeDelay options:UIViewAnimationOptionCurveEaseOut animations:^{
self.alpha = .0;
} completion:^(BOOL finished){
[self removeFromSuperview];
}];
if (self.blackOpaqueView) {
[UIView animateWithDuration:timeDisappear animations:^{
self.blackOpaqueView.alpha = 0;
} completion:^(BOOL finished) {
[self.blackOpaqueView removeFromSuperview];
}];
}
}
-(void)setupItems{
}
//计算frame
-(void)calculateFrame{
[self setUpItemsFrame];
}
-(void)setUpItemsFrame{
//
NSMutableArray *arr_btn = [NSMutableArray array];
for (int i = 1; i<=6; i ++) {
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 48, 48)];
[btn1 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"sy_new_xf_%d",i]] forState:UIControlStateNormal];
btn1.tag = 200+i;
[btn1 addTarget:self action:@selector(xuanfuAct:) forControlEvents:UIControlEventTouchUpInside];
[arr_btn addObject:btn1];
}
// self.roundBtn = [HZYRoundShowButton roundShowButton:arr_btn andRadius:72 andSize:CGSizeMake(44, 44) andPosition:CGPointMake(SCREEN_WIDTH-40, SCREEN_HEIGHT-130-[GetDataNum getTabbarHeight]+[GetDataNum getStatusBarHight]+44)];
// self.xfBtn.center = CGPointMake(SCREEN_WIDTH-40, SCREEN_HEIGHT-130-[GetDataNum getTabbarHeight]);
self.roundBtn = [HZYRoundShowButton roundShowButton:arr_btn andRadius:72 andSize:CGSizeMake(44, 44) andPosition:CGPointMake(self.centerPoint.x, self.centerPoint.y+[GetDataNum getStatusBarHight]+44)];
if (self.isLeft) {
self.roundBtn.arcLength = M_PI+M_PI/4;
self.roundBtn.startAngle = M_PI+M_PI/2;
}else{
self.roundBtn.arcLength = M_PI+M_PI/4;
self.roundBtn.startAngle = M_PI / 2;
}
self.roundBtn.roundButton = YES;
self.roundBtn.delegate = self;
self.roundBtn.backgroundColor = [UIColor clearColor];
}
//更新试图
- (void)setupViews
{
// Setup Background
if (self.backgroundColor) {
[self setBackgroundColor:self.backgroundColor];
} else {
[self setBackgroundColor:[UIColor clearColor]];
}
// //由于self是全屏大小
UITapGestureRecognizer *outsideTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(outsideTap:)];
[self addGestureRecognizer:outsideTapGesture];
[self addSubview:self.roundBtn];
[self.roundBtn showBtn];
}
//已经出现的逻辑
-(void)didAppearAlertView{
}
-(void)jumpACtion:(UIButton *)btn{
if (self.actBlock) {
self.actBlock(YES);
}
[self dismiss];
}
-(void)xuanfuAct:(UIButton *)btn{
[self dismiss];
}
- (void)baseButtonDidTouched:(UIButton *)baseBtn widthOpened:(BOOL)isOppened{
[self dismiss];
}
@end