话不多说直接上代码,一共需要四个文件,产品要求全局只能有一个悬浮球,为了不受tabbar的影象,所以做成了单例,统一由单例去设置悬浮球的属性,可根据项目具体要求更改下列代码,代码如下:
ANTSuspensionBtn.h
#import <UIKit/UIKit.h>
@class ANTSuspensionBtn;
@protocol ANTSuspensionViewDelegate <NSObject>
/** 点击悬浮球的回调 */
- (void)suspensionViewClick:(ANTSuspensionBtn *)suspensionView;
@end
@interface ANTSuspensionBtn : UIButton
/** 代理 */
@property (nonatomic, weak) id<ANTSuspensionViewDelegate> delegate;
- (instancetype)initWithFrame:(CGRect)frame color:(UIColor*)color;
/**
* 显示
*/
- (void)show;
@end
ANTSuspensionBtn.m
#import "ANTSuspensionBtn.h"
#define kTouchWidth self.frame.size.width
#define kTouchHeight self.frame.size.height
@implementation ANTSuspensionBtn
- (instancetype)initWithFrame:(CGRect)frame color:(UIColor *)color
{
if(self = [super initWithFrame:frame])
{
self.userInteractionEnabled = YES;
self.backgroundColor = color;
self.alpha = 1.0;
self.titleLabel.font = [UIFont systemFontOfSize:14];
//拖动
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(changeLocation:)];
pan.delaysTouchesBegan = YES;
[self addGestureRecognizer:pan];
//点击
[self addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
#pragma mark - event response
- (void)changeLocation:(UIPanGestureRecognizer*)p
{
UIWindow *appWindow = [UIApplication sharedApplication].delegate.window;
CGPoint panPoint = [p locationInView:appWindow];
//透明度
if(p.state == UIGestureRecognizerStateBegan)
{
self.alpha = 1;
}
else if (p.state == UIGestureRecognizerStateEnded)
{
self.alpha = 0.5;
}
//位置
if(p.state == UIGestureRecognizerStateChanged)
{
self.center = CGPointMake(panPoint.x, panPoint.y);
}else if(p.state == UIGestureRecognizerStateEnded)
{
CGFloat left = fabs(panPoint.x);
CGFloat right = fabs(kScreenWidth - left);
//ripper:只停留在左右,需要在上下,将注释打开
// CGFloat top = fabs(panPoint.y);
// CGFloat bottom = fabs(kScreenHeight - top);
CGFloat minSpace = MIN(left, right);
// CGFloat minSpace = MIN(MIN(MIN(top, left), bottom), right);
CGPoint newCenter;
CGFloat targetY = 0;
//校正Y
if (panPoint.y < 15 + kTouchHeight/2.0) {
targetY = 15 + kTouchHeight/2.0;
}else if (panPoint.y > (kScreenHeight - kTouchHeight/2.0 - 15)) {
targetY = kScreenHeight - kTouchHeight/2.0 - 15;
}else{
targetY = panPoint.y;
}
if (minSpace == left) {
newCenter = CGPointMake(kTouchHeight/3 + 20, targetY);
}else if (minSpace == right) {
newCenter = CGPointMake(kScreenWidth-kTouchHeight/3-20, targetY);
}
[UIView animateWithDuration:.25 animations:^{
self.center = newCenter;
}];
}
}
- (void)click
{
if([self.delegate respondsToSelector:@selector(suspensionViewClick:)])
{
[self.delegate suspensionViewClick:self];
}
}
- (void)show
{
[[UIApplication sharedApplication].keyWindow addSubview:self];
}
@end
ANTSuspensionManager.h
/*点击悬浮球的回调 */
- (void)suspensionManagerClick:(ANTSuspensionManager *)suspensionView;
@end
@interface ANTSuspensionManager : NSObject
@property (nonatomic , weak) id<ANTSuspensionManagerDelegate>delegate;
//初始化
+ (ANTSuspensionManager *)sharedSuspensionManager;
//添加悬浮球
- (void)createSuspension;
//显示悬浮球
- (void)displaySuspension;
//隐藏悬浮球
- (void)hiddenSuspension;
//改变悬浮球的透明度
- (void)changeSuspensionAlpha:(CGFloat)alpha;
@end
ANTSuspensionManager.m
#import "ANTSuspensionManager.h"
#import "ANTSuspensionBtn.h"
static ANTSuspensionManager *sharedSuspensionManager = nil;
@interface ANTSuspensionManager ()<ANTSuspensionViewDelegate>
/** 悬浮球*/
@property (nonatomic , strong) ANTSuspensionBtn *susGlobe;
@end
@implementation ANTSuspensionManager
+ (ANTSuspensionManager *)sharedSuspensionManager
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedSuspensionManager = [[self alloc] init];
});
return sharedSuspensionManager;
}
- (ANTSuspensionBtn *)susGlobe{
if (!_susGlobe) {
_susGlobe = [[ANTSuspensionBtn alloc] initWithFrame:CGRectMake(SCREENWidth - 60, SCREENHeight - 130, 50, 50) color:[UIColor clearColor]];
[_susGlobe setImage:[UIImage imageNamed:@"实境"] forState:UIControlStateNormal];
_susGlobe.delegate = self;
}
return _susGlobe;
}
- (void)createSuspension{
[self.susGlobe show];
}
//显示悬浮球
- (void)displaySuspension;
{
self.susGlobe.hidden = NO;
}
//隐藏悬浮球
- (void)hiddenSuspension;
{
self.susGlobe.hidden = YES;
}
//改变悬浮球的透明度
- (void)changeSuspensionAlpha:(CGFloat)alpha;
{
self.susGlobe.alpha = alpha;
}
#pragma mark - ANTSuspensionViewDelegate
- (void)suspensionViewClick:(ANTSuspensionBtn *)suspensionView
{
if([self.delegate respondsToSelector:@selector(suspensionManagerClick:)])
{
[self.delegate suspensionManagerClick:self];
}
}
@end
网友评论