哈哈好久沒有寫簡書了,抽點空閑時間寫一個簡單的Demo
上個星期的版本迭代,產(chǎn)品經(jīng)理提出一個需求,就是頁面上要有一個懸浮按鈕。就和蘋果系統(tǒng)自帶的小白色按鈕一樣。還帶吸附的效果,我覺得直接放在window 的上不就ok ,然后然后等我做完以后說只需要三個主頁面有這個效果。尼瑪這下我就要改呀改,想想appdelegate不是單利嗎,直接給他一個button 的屬性,不就可以了設(shè)置下隱藏的效果,然后自己測試的時候,亂七八糟的小問題,之后覺得這方案太垃圾了,還要判斷好多的隱藏和顯示,而且后臺的接口還不是在配置文件中的而是在第一個主界面里面給定的接口。。最后無奈之下寫一個單利,什么連七八糟的問題都解決了。。小白一個,代碼分享給大家。。有問題多多指點。。求大神輕噴。。《代碼還需要優(yōu)化》
廢話就不多說了直接上效果圖,這是我公司項目中加的
這個只是為了符合產(chǎn)品需求寫的,有需要的小伙伴可以自己修改修改放在window上或者view 上都有方法
#######效果
6月-26-2017 13-44-28.gif
最好真機測試比較好,因為模擬器移動的時候會觸發(fā)點擊的事件
########代碼
CBBDraggableButton.h
@protocol CBBDragButtonDelegate <NSObject>
- (void)cbb_DraggableButtonClicked:(UIButton *)button;
@end
@interface CBBDraggableButton : UIButton
+(instancetype)sharedInstance;
/** 點擊 */
@property (nonatomic,copy) void (^clickBlock) (CBBDraggableButton *clickButton) ;
/** 拖拽 */
@property (nonatomic,copy) void (^draggbleBlock) (CBBDraggableButton*draggbleButton) ;
/** 代理 */
@property (nonatomic,weak) id<CBBDragButtonDelegate> dragButtonDelegate;
- (void)showWindow;
@property (nonatomic) BOOL draggable;
@property (nonatomic) BOOL autoDocking;
----
CBBDraggableButton.m
#import "CBBDraggableButton.h"
@interface CBBDraggableButton ()
{
CGPoint _beginLocation;
UILongPressGestureRecognizer *_longPressGestureRecognizer;
}
@end
@implementation CBBDraggableButton
@synthesize draggable = _draggable;
@synthesize autoDocking = _autoDocking;
@synthesize clickBlock = _clickBlock;
######CBBDraggableButton.m
/** 單利 */
+ (instancetype)sharedInstance
{
static CBBDraggableButton *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[self alloc]init];
});
return manager;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setUpSubViews];
}
return self;
}
- (instancetype)init {
self = [super init];
if (self) {
[self setUpSubViews];
}
return self;
}
- (instancetype)initAtView:(id)view WithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[view addSubview:self];
[self setUpSubViews];
}
return self;
}
- (instancetype)initInKeyWindowWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self performSelector:@selector(addToKeyWindow) withObject:nil afterDelay:0.1];
self.hidden = YES;
[self setUpSubViews];
}
return self;
}
- (void)setUpSubViews
{
[self.layer setMasksToBounds:YES];
_autoDocking = YES;
_draggable = NO;
}
- (void)addToKeyWindow
{
[[UIApplication sharedApplication].keyWindow addSubview:self];
}
- (void)setClickBlock:(void (^)(CBBDraggableButton *))clickBlock
{
_clickBlock = clickBlock;
if (_clickBlock && _draggable) {
[self addTarget:self action:@selector(buttonTouched) forControlEvents:UIControlEventTouchUpInside];
}
}
- (void)buttonTouched {
[self performSelector:@selector(executeButtonTouchedBlock) withObject:nil afterDelay:0];
}
- (void)executeButtonTouchedBlock {
_clickBlock(self);
}
#pragma mark ------ touth 代理 ------
/** 開始 */
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
/** 獲取 開始位置 */
_beginLocation = [[touches anyObject] locationInView:self];
}
/** 移動 */
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:self];
float offsetX = currentLocation.x - _beginLocation.x;
float offsetY = currentLocation.y - _beginLocation.y;
self.center = CGPointMake(self.center.x + offsetX, self.center.y + offsetY);
CGRect superviewFrame = self.superview.frame;
CGRect frame = self.frame;
CGFloat leftLimitX = frame.size.width / 2;
CGFloat rightLimitX = superviewFrame.size.width - leftLimitX;
CGFloat topLimitY = frame.size.height / 2;
CGFloat bottomLimitY = superviewFrame.size.height - topLimitY - 49 ;
if (self.center.x > rightLimitX) {
self.center = CGPointMake(rightLimitX, self.center.y);
}else if (self.center.x <= leftLimitX) {
self.center = CGPointMake(leftLimitX, self.center.y);
}
if (self.center.y > bottomLimitY) {
self.center = CGPointMake(self.center.x, bottomLimitY );
}else if (self.center.y <= topLimitY){
self.center = CGPointMake(self.center.x, topLimitY);
}
}
/** 結(jié)束 */
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded: touches withEvent: event];
UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:self];
/** 獲取偏移 */
float offsetX = currentLocation.x - _beginLocation.x;
float offsetY = currentLocation.y - _beginLocation.y;
NSLog(@"----%f-----%f",offsetY,offsetY);
if (pow(offsetX,2) + pow(offsetY,2) <0.1) {
/** 判斷 如果拖拽 間距 < 0.1 進行點擊 */
_draggable = NO;
[self executeButtonTouchedBlock];
return;
}
CGRect superviewFrame = self.superview.frame;
CGRect frame = self.frame;
CGFloat middleX = superviewFrame.size.width / 2;
if (self.center.x >= middleX) {
[UIView animateWithDuration:0.2f animations:^{
self.center = CGPointMake(superviewFrame.size.width - frame.size.width / 2, self.center.y);
} completion:^(BOOL finished) {
}];
} else {
[UIView animateWithDuration:0.4 animations:^{
self.center = CGPointMake(frame.size.width / 2, self.center.y);
} completion:^(BOOL finished) {
}];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
}
/** 刪除 */
+ (void)removeAllFromView:(id)superView {
for (id view in [superView subviews]) {
if ([view isKindOfClass:[CBBDraggableButton class]]) {
[view removeFromSuperview];
}
}
}
/** 顯示 隱藏 */
- (void)showWindow
{
self.hidden = NO;
}
- (void)dissMissWindow;
{
self.hidden = YES;
}
初始化
CBBDraggableButton *draggableButton = [CBBDraggableButton sharedInstance];
draggableButton.frame = CGRectMake( [UIScreen mainScreen].bounds.size.width - 90, [UIScreen mainScreen].bounds.size.height - 49 - 15 - 75, 75, 75) ;
[draggableButton setAutoDocking:YES];
draggableButton.hidden = YES;
[self.view addSubview:draggableButton];
draggableButton.backgroundColor = [UIColor redColor];
draggableButton.layer.cornerRadius = 75/2;
draggableButton.clipsToBounds = YES;
[[CBBDraggableButton sharedInstance ] showWindow];
[CBBDraggableButton sharedInstance].clickBlock = ^(CBBDraggableButton *clickBlock) {
UIAlertController*alertController = [[UIAlertController alloc] init];
UIAlertAction*alertAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
[alertController addAction:alertAction];
[alertController addAction:({
UIAlertAction*action = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
}];
action;
})];
[alertController addAction:({
UIAlertAction*action = [UIAlertAction actionWithTitle:@"從相冊選擇" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
//UIAlertActionStyleDestructive字體是紅色的
}];
action;
})];
[self presentViewController:alertController animated:YES completion:nil];
};
2017年06月26日 未完待續(xù)。。 有什么問題可以提出意見。。基本上就是所有代碼了,需要源碼留下郵箱。。后續(xù)上傳Git