diff --git a/keyBoard.xcodeproj/project.pbxproj b/keyBoard.xcodeproj/project.pbxproj index a8d8b35..392c881 100644 --- a/keyBoard.xcodeproj/project.pbxproj +++ b/keyBoard.xcodeproj/project.pbxproj @@ -15,6 +15,10 @@ 04122F702EC5F40800EF7AB3 /* FGIAPService.m in Sources */ = {isa = PBXBuildFile; fileRef = 04122F672EC5F40800EF7AB3 /* FGIAPService.m */; }; 04122F7E2EC5FC5500EF7AB3 /* KBJfPayCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 04122F7C2EC5FC5500EF7AB3 /* KBJfPayCell.m */; }; 04122F822EC5FC6F00EF7AB3 /* KBJfPay.m in Sources */ = {isa = PBXBuildFile; fileRef = 04122F802EC5FC6F00EF7AB3 /* KBJfPay.m */; }; + 04122F872EC6198C00EF7AB3 /* WMDragView.m in Sources */ = {isa = PBXBuildFile; fileRef = 04122F852EC6198C00EF7AB3 /* WMDragView.m */; }; + 04122F882EC6F07F00EF7AB3 /* KBFullAccessManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 04A9FE112EB4D0D20020DB6D /* KBFullAccessManager.m */; }; + 04122F8B2EC6F7C800EF7AB3 /* IAPVerifyTransactionObj.m in Sources */ = {isa = PBXBuildFile; fileRef = 04122F8A2EC6F7C800EF7AB3 /* IAPVerifyTransactionObj.m */; }; + 04122F8E2EC6F83F00EF7AB3 /* PayVM.m in Sources */ = {isa = PBXBuildFile; fileRef = 04122F8D2EC6F83F00EF7AB3 /* PayVM.m */; }; 043FBCD22EAF97630036AFE1 /* KBPermissionViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 04C6EAE12EAF940F0089C901 /* KBPermissionViewController.m */; }; 0459D1B42EBA284C00F2D189 /* KBSkinCenterVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 0459D1B32EBA284C00F2D189 /* KBSkinCenterVC.m */; }; 0459D1B72EBA287900F2D189 /* KBSkinManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 0459D1B62EBA287900F2D189 /* KBSkinManager.m */; }; @@ -203,6 +207,12 @@ 04122F7C2EC5FC5500EF7AB3 /* KBJfPayCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBJfPayCell.m; sourceTree = ""; }; 04122F7F2EC5FC6F00EF7AB3 /* KBJfPay.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBJfPay.h; sourceTree = ""; }; 04122F802EC5FC6F00EF7AB3 /* KBJfPay.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBJfPay.m; sourceTree = ""; }; + 04122F842EC6198C00EF7AB3 /* WMDragView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WMDragView.h; sourceTree = ""; }; + 04122F852EC6198C00EF7AB3 /* WMDragView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = WMDragView.m; sourceTree = ""; }; + 04122F892EC6F7C800EF7AB3 /* IAPVerifyTransactionObj.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = IAPVerifyTransactionObj.h; sourceTree = ""; }; + 04122F8A2EC6F7C800EF7AB3 /* IAPVerifyTransactionObj.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = IAPVerifyTransactionObj.m; sourceTree = ""; }; + 04122F8C2EC6F83F00EF7AB3 /* PayVM.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PayVM.h; sourceTree = ""; }; + 04122F8D2EC6F83F00EF7AB3 /* PayVM.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PayVM.m; sourceTree = ""; }; 0459D1B22EBA284C00F2D189 /* KBSkinCenterVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBSkinCenterVC.h; sourceTree = ""; }; 0459D1B32EBA284C00F2D189 /* KBSkinCenterVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBSkinCenterVC.m; sourceTree = ""; }; 0459D1B52EBA287900F2D189 /* KBSkinManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBSkinManager.h; sourceTree = ""; }; @@ -502,6 +512,8 @@ 04122F642EC5F40600EF7AB3 /* M */ = { isa = PBXGroup; children = ( + 04122F892EC6F7C800EF7AB3 /* IAPVerifyTransactionObj.h */, + 04122F8A2EC6F7C800EF7AB3 /* IAPVerifyTransactionObj.m */, ); path = M; sourceTree = ""; @@ -509,6 +521,8 @@ 04122F652EC5F40600EF7AB3 /* VM */ = { isa = PBXGroup; children = ( + 04122F8C2EC6F83F00EF7AB3 /* PayVM.h */, + 04122F8D2EC6F83F00EF7AB3 /* PayVM.m */, ); path = VM; sourceTree = ""; @@ -548,6 +562,15 @@ path = VC; sourceTree = ""; }; + 04122F862EC6198C00EF7AB3 /* WMDragView */ = { + isa = PBXGroup; + children = ( + 04122F842EC6198C00EF7AB3 /* WMDragView.h */, + 04122F852EC6198C00EF7AB3 /* WMDragView.m */, + ); + path = WMDragView; + sourceTree = ""; + }; 0477BD942EBAFF4E0055D639 /* Utils */ = { isa = PBXGroup; children = ( @@ -763,6 +786,7 @@ 048908D32EBF618E00FABA60 /* Vender */ = { isa = PBXGroup; children = ( + 04122F862EC6198C00EF7AB3 /* WMDragView */, 04122F6C2EC5F40800EF7AB3 /* FGIAPService */, 049FB2162EC20A6600FAB05D /* BMLongPressDragCellCollectionView */, 048908D92EBF61AF00FABA60 /* UICollectionViewLeftAlignedLayout */, @@ -1559,7 +1583,9 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 04122F882EC6F07F00EF7AB3 /* KBFullAccessManager.m in Sources */, 04122F622EC5F41D00EF7AB3 /* KBUser.m in Sources */, + 04122F8B2EC6F7C800EF7AB3 /* IAPVerifyTransactionObj.m in Sources */, 049FB31D2EC21BCD00FAB05D /* KBMyKeyboardCell.m in Sources */, 048909F62EC0AAAA00FABA60 /* KBCategoryTitleCell.m in Sources */, 048909F72EC0AAAA00FABA60 /* KBCategoryTitleView.m in Sources */, @@ -1605,6 +1631,7 @@ 049FB2292EC31BB000FAB05D /* KBChangeNicknamePopView.m in Sources */, 048908EC2EBF849300FABA60 /* KBSkinTagsContainerCell.m in Sources */, 049FB2172EC20A6600FAB05D /* BMLongPressDragCellCollectionView.m in Sources */, + 04122F8E2EC6F83F00EF7AB3 /* PayVM.m in Sources */, 0477BDF02EBB76E30055D639 /* HomeSheetVC.m in Sources */, 048908E62EBF841B00FABA60 /* KBSkinDetailTagCell.m in Sources */, 04FC97002EB30A00007BD342 /* KBGuideTopCell.m in Sources */, @@ -1631,6 +1658,7 @@ 0477BDFD2EBC6A170055D639 /* HomeHotVC.m in Sources */, 049FB2432EC4BBB700FAB05D /* KBLoginPopView.m in Sources */, 048908CC2EBE373500FABA60 /* KBSearchBarView.m in Sources */, + 04122F872EC6198C00EF7AB3 /* WMDragView.m in Sources */, 048908CD2EBE373500FABA60 /* KBSearchSectionHeader.m in Sources */, 049FB2202EC30D2700FAB05D /* HomeRankDetailPopView.m in Sources */, 048908CE2EBE373500FABA60 /* KBSkinCardCell.m in Sources */, diff --git a/keyBoard/Assets.xcassets/Home/key_permiss_icon.imageset/Contents.json b/keyBoard/Assets.xcassets/Home/key_permiss_icon.imageset/Contents.json new file mode 100644 index 0000000..085ed8a --- /dev/null +++ b/keyBoard/Assets.xcassets/Home/key_permiss_icon.imageset/Contents.json @@ -0,0 +1,22 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "filename" : "key_permiss_icon@2x.png", + "idiom" : "universal", + "scale" : "2x" + }, + { + "filename" : "key_permiss_icon@3x.png", + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/keyBoard/Assets.xcassets/Home/key_permiss_icon.imageset/key_permiss_icon@2x.png b/keyBoard/Assets.xcassets/Home/key_permiss_icon.imageset/key_permiss_icon@2x.png new file mode 100644 index 0000000..95809c4 Binary files /dev/null and b/keyBoard/Assets.xcassets/Home/key_permiss_icon.imageset/key_permiss_icon@2x.png differ diff --git a/keyBoard/Assets.xcassets/Home/key_permiss_icon.imageset/key_permiss_icon@3x.png b/keyBoard/Assets.xcassets/Home/key_permiss_icon.imageset/key_permiss_icon@3x.png new file mode 100644 index 0000000..24c54a3 Binary files /dev/null and b/keyBoard/Assets.xcassets/Home/key_permiss_icon.imageset/key_permiss_icon@3x.png differ diff --git a/keyBoard/Class/Home/VC/HomeMainVC.m b/keyBoard/Class/Home/VC/HomeMainVC.m index e8fdf54..4e6c27e 100644 --- a/keyBoard/Class/Home/VC/HomeMainVC.m +++ b/keyBoard/Class/Home/VC/HomeMainVC.m @@ -9,11 +9,14 @@ #import "HomeHeadView.h" #import "KBPanModalView.h" #import "KBGuideVC.h" // 首次安装指引页 +#import "WMDragView.h" @interface HomeMainVC () @property (nonatomic, strong) HomeHeadView *headView; @property (nonatomic, strong) KBPanModalView *simplePanModalView; +/// 权限按钮 +@property (nonatomic, strong) WMDragView *keyPermissButton; @end @implementation HomeMainVC @@ -27,8 +30,6 @@ [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.view); }]; -// CGFloat topV = (500); - [self.view addSubview:self.headView]; [self setupMas]; // 创建sheetVC @@ -42,6 +43,14 @@ } self.simplePanModalView.topInset = 100; [self.simplePanModalView presentInView:self.view]; + [self.view addSubview:self.keyPermissButton]; + self.keyPermissButton.frame = CGRectMake(KB_SCREEN_WIDTH - 92, KB_SCREEN_HEIGHT - 78 - 120, 92, 78); + + KBWeakSelf + self.keyPermissButton.clickDragViewBlock = ^(WMDragView *dragView){ + KBGuideVC *vc = [KBGuideVC new]; + [weakSelf.navigationController pushViewController:vc animated:true]; + }; } // 在界面可见后做“首次安装”检查,避免在 viewDidLoad 期间 push 引起的转场警告 @@ -87,4 +96,15 @@ } +- (WMDragView *)keyPermissButton{ + if (!_keyPermissButton) { + _keyPermissButton = [[WMDragView alloc] init]; + _keyPermissButton.backgroundColor = [UIColor clearColor]; + _keyPermissButton.isKeepBounds = true; + [_keyPermissButton.button setImage:[UIImage imageNamed:@"key_permiss_icon"] forState:UIControlStateNormal]; + + } + return _keyPermissButton; +} + @end diff --git a/keyBoard/Class/Pay/M/IAPVerifyTransactionObj.m b/keyBoard/Class/Pay/M/IAPVerifyTransactionObj.m index 291252a..b277af3 100644 --- a/keyBoard/Class/Pay/M/IAPVerifyTransactionObj.m +++ b/keyBoard/Class/Pay/M/IAPVerifyTransactionObj.m @@ -42,8 +42,8 @@ __weak typeof(self) weakSelf = self; [self.payVM applePayReqWithParams:params needShow:NO completion:^(NSInteger sta, NSString * _Nullable msg) { [KBHUD dismiss]; - [KBHUD showInfo:(sta == KB_PAY_ERROR_CODE ? @"支付失败" : @"支付成功")]; - if (sta == KB_PAY_SUCC_CODE) { + [KBHUD showInfo:(sta == ERROR_CODE ? @"支付失败" : @"支付成功")]; + if (sta == SUCCESS_CODE) { [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; if (handler) handler(@"成功", nil); } else { diff --git a/keyBoard/Class/Pay/VM/PayVM.m b/keyBoard/Class/Pay/VM/PayVM.m index 7e386d2..42ddd16 100644 --- a/keyBoard/Class/Pay/VM/PayVM.m +++ b/keyBoard/Class/Pay/VM/PayVM.m @@ -18,12 +18,12 @@ if (needShow) { [KBHUD dismiss]; } if (error) { - if (completion) completion(KB_PAY_ERROR_CODE, error.localizedDescription ?: @"网络错误"); + if (completion) completion(ERROR_CODE, error.localizedDescription ?: @"网络错误"); return; } NSInteger sta = [self.class extractStatusFromResponseObject:jsonOrData response:response]; - NSString *msg = [self.class extractMessageFromResponseObject:jsonOrData] ?: (sta == KB_PAY_SUCC_CODE ? @"OK" : @"失败"); + NSString *msg = [self.class extractMessageFromResponseObject:jsonOrData] ?: (sta == SUCCESS_CODE ? @"OK" : @"失败"); if (completion) completion(sta, msg); }]; } @@ -36,21 +36,21 @@ NSDictionary *d = (NSDictionary *)obj; id code = d[@"code"] ?: d[@"status"] ?: d[@"retcode"]; if ([code isKindOfClass:NSNumber.class]) { - return [((NSNumber *)code) integerValue] == 0 ? KB_PAY_SUCC_CODE : KB_PAY_ERROR_CODE; + return [((NSNumber *)code) integerValue] == 0 ? SUCCESS_CODE : ERROR_CODE; } if ([code isKindOfClass:NSString.class]) { // 常见:"0" 视为成功 - return [((NSString *)code) integerValue] == 0 ? KB_PAY_SUCC_CODE : KB_PAY_ERROR_CODE; + return [((NSString *)code) integerValue] == 0 ? SUCCESS_CODE : ERROR_CODE; } id success = d[@"success"] ?: d[@"ok"]; if ([success respondsToSelector:@selector(boolValue)]) { - return [success boolValue] ? KB_PAY_SUCC_CODE : KB_PAY_ERROR_CODE; + return [success boolValue] ? SUCCESS_CODE : ERROR_CODE; } } // 无明显字段,按 HTTP 2xx 视为成功 NSInteger http = 0; if ([resp isKindOfClass:NSHTTPURLResponse.class]) { http = ((NSHTTPURLResponse *)resp).statusCode; } - return (http >= 200 && http < 300) ? KB_PAY_SUCC_CODE : KB_PAY_ERROR_CODE; + return (http >= 200 && http < 300) ? SUCCESS_CODE : ERROR_CODE; } + (NSString *)extractMessageFromResponseObject:(id)obj { diff --git a/keyBoard/Class/Vender/WMDragView/WMDragView.h b/keyBoard/Class/Vender/WMDragView/WMDragView.h new file mode 100644 index 0000000..761dc35 --- /dev/null +++ b/keyBoard/Class/Vender/WMDragView/WMDragView.h @@ -0,0 +1,77 @@ +// +// WMDragView.h +// WMDragView +// +// Created by zhengwenming on 2016/12/16. +// +// + +#import + + +// 拖曳view的方向 +typedef NS_ENUM(NSInteger, WMDragDirection) { + WMDragDirectionAny, /**< 任意方向 */ + WMDragDirectionHorizontal, /**< 水平方向 */ + WMDragDirectionVertical, /**< 垂直方向 */ +}; + +@interface WMDragView : UIView +/** + 是不是能拖曳,默认为YES + YES,能拖曳 + NO,不能拖曳 + */ +@property (nonatomic,assign) BOOL dragEnable; + +/** + 活动范围,默认为父视图的frame范围内(因为拖出父视图后无法点击,也没意义) + 如果设置了,则会在给定的范围内活动 + 如果没设置,则会在父视图范围内活动 + 注意:设置的frame不要大于父视图范围 + 注意:设置的frame为0,0,0,0表示活动的范围为默认的父视图frame,如果想要不能活动,请设置dragEnable这个属性为NO + */ +@property (nonatomic,assign) CGRect freeRect; + +/** + 拖曳的方向,默认为any,任意方向 + */ +@property (nonatomic,assign) WMDragDirection dragDirection; + +/** + contentView内部懒加载的一个UIImageView + 开发者也可以自定义控件添加到本view中 + 注意:最好不要同时使用内部的imageView和button + */ +@property (nonatomic,strong) UIImageView *imageView; +/** + contentView内部懒加载的一个UIButton + 开发者也可以自定义控件添加到本view中 + 注意:最好不要同时使用内部的imageView和button + */ +@property (nonatomic,strong) UIButton *button; +/** + 是不是总保持在父视图边界,默认为NO,没有黏贴边界效果 + isKeepBounds = YES,它将自动黏贴边界,而且是最近的边界 + isKeepBounds = NO, 它将不会黏贴在边界,它是free(自由)状态,跟随手指到任意位置,但是也不可以拖出给定的范围frame + */ +@property (nonatomic,assign) BOOL isKeepBounds; +/** + 点击的回调block + */ +@property (nonatomic,copy) void(^clickDragViewBlock)(WMDragView *dragView); +/** + 开始拖动的回调block + */ +@property (nonatomic,copy) void(^beginDragBlock)(WMDragView *dragView); +/** + 拖动中的回调block + */ +@property (nonatomic,copy) void(^duringDragBlock)(WMDragView *dragView); +/** + 结束拖动的回调block + */ +@property (nonatomic,copy) void(^endDragBlock)(WMDragView *dragView); +@end + + diff --git a/keyBoard/Class/Vender/WMDragView/WMDragView.m b/keyBoard/Class/Vender/WMDragView/WMDragView.m new file mode 100644 index 0000000..848a2bd --- /dev/null +++ b/keyBoard/Class/Vender/WMDragView/WMDragView.m @@ -0,0 +1,235 @@ +// +// WMDragView.m +// WMDragView +// +// Created by zhengwenming on 2016/12/16. +// +// + +#import "WMDragView.h" + +@interface WMDragView () +/** + 内容view,命名为contentViewForDrag,因为很多其他开源的第三方的库,里面同样有contentView这个属性 + ,这里特意命名为contentViewForDrag以防止冲突 + */ +@property (nonatomic,strong) UIView *contentViewForDrag; +@property (nonatomic,assign) CGPoint startPoint; +@property (nonatomic,strong) UIPanGestureRecognizer *panGestureRecognizer; +@property (nonatomic,assign) CGFloat previousScale; +@end + +@implementation WMDragView +-(UIImageView *)imageView{ + if (_imageView==nil) { + _imageView = [[UIImageView alloc]init]; + _imageView.userInteractionEnabled = YES; + _imageView.clipsToBounds = YES; + [self.contentViewForDrag addSubview:_imageView]; + } + return _imageView; +} +-(UIButton *)button{ + if (_button==nil) { + _button = [UIButton buttonWithType:UIButtonTypeCustom]; + _button.clipsToBounds = YES; + _button.userInteractionEnabled = NO; + [self.contentViewForDrag addSubview:_button]; + } + return _button; +} +-(UIView *)contentViewForDrag{ + if (_contentViewForDrag==nil) { + _contentViewForDrag = [[UIView alloc]init]; + _contentViewForDrag.clipsToBounds = YES; + } + return _contentViewForDrag; +} +- (instancetype)initWithFrame:(CGRect)frame{ + self = [super initWithFrame:frame]; + if (self) { + [self addSubview:self.contentViewForDrag]; + [self setUp]; + } + return self; +} +- (instancetype)initWithCoder:(NSCoder *)coder{ + self = [super initWithCoder:coder]; + if (self) { + [self setUp]; + } + return self; +} + +-(void)layoutSubviews{ + [super layoutSubviews]; + if (self.freeRect.origin.x!=0||self.freeRect.origin.y!=0||self.freeRect.size.height!=0||self.freeRect.size.width!=0) { + //设置了freeRect--活动范围 + }else{ + //没有设置freeRect--活动范围,则设置默认的活动范围为父视图的frame + self.freeRect = (CGRect){CGPointZero,self.superview.bounds.size}; + } + _imageView.frame = (CGRect){CGPointZero,self.bounds.size}; + _button.frame = (CGRect){CGPointZero,self.bounds.size}; + self.contentViewForDrag.frame = (CGRect){CGPointZero,self.bounds.size}; +} +-(void)setUp{ + self.dragEnable = YES;//默认可以拖曳 + self.clipsToBounds = YES; + self.isKeepBounds = NO; + self.backgroundColor = [UIColor lightGrayColor]; + UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickDragView)]; + [self addGestureRecognizer:singleTap]; + + //添加移动手势可以拖动 + self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragAction:)]; + self.panGestureRecognizer.minimumNumberOfTouches = 1; + self.panGestureRecognizer.maximumNumberOfTouches = 1; + self.panGestureRecognizer.delegate = self; + [self addGestureRecognizer:self.panGestureRecognizer]; +} +- (void)setIsKeepBounds:(BOOL)isKeepBounds{ + _isKeepBounds = isKeepBounds; + if(isKeepBounds){ + [self keepBounds]; + } +} +-(void)setFreeRect:(CGRect)freeRect{ + _freeRect = freeRect; + [self keepBounds]; + +} +//-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ +// return self.dragEnable; +//} +/** + 拖动事件 + @param pan 拖动手势 + */ +-(void)dragAction:(UIPanGestureRecognizer *)pan{ + if(self.dragEnable==NO)return; + switch (pan.state) { + case UIGestureRecognizerStateBegan:{//开始拖动 + if (self.beginDragBlock) { + self.beginDragBlock(self); + } + //注意完成移动后,将translation重置为0十分重要。否则translation每次都会叠加 + [pan setTranslation:CGPointZero inView:self]; + //保存触摸起始点位置 + self.startPoint = [pan translationInView:self]; + break; + } + case UIGestureRecognizerStateChanged:{//拖动中 + //计算位移 = 当前位置 - 起始位置 + if (self.duringDragBlock) { + self.duringDragBlock(self); + } + CGPoint point = [pan translationInView:self]; + float dx; + float dy; + switch (self.dragDirection) { + case WMDragDirectionAny: + dx = point.x - self.startPoint.x; + dy = point.y - self.startPoint.y; + break; + case WMDragDirectionHorizontal: + dx = point.x - self.startPoint.x; + dy = 0; + break; + case WMDragDirectionVertical: + dx = 0; + dy = point.y - self.startPoint.y; + break; + default: + dx = point.x - self.startPoint.x; + dy = point.y - self.startPoint.y; + break; + } + + //计算移动后的view中心点 + CGPoint newCenter = CGPointMake(self.center.x + dx, self.center.y + dy); + //移动view + self.center = newCenter; + // 注意完成上述移动后,将translation重置为0十分重要。否则translation每次都会叠加 + [pan setTranslation:CGPointZero inView:self]; + break; + } + case UIGestureRecognizerStateEnded:{//拖动结束 + [self keepBounds]; + if (self.endDragBlock) { + self.endDragBlock(self); + } + break; + } + default: + break; + } +} +//单击事件 +-(void)clickDragView{ + if (self.clickDragViewBlock) { + self.clickDragViewBlock(self); + } +} +//黏贴边界效果 +- (void)keepBounds{ + //中心点判断 + float centerX = self.freeRect.origin.x+(self.freeRect.size.width - self.frame.size.width)/2; + CGRect rect = self.frame; + if (self.isKeepBounds==NO) {//没有设置黏贴边界的效果 + if (self.frame.origin.x < self.freeRect.origin.x) { + CGContextRef context = UIGraphicsGetCurrentContext(); + [UIView beginAnimations:@"leftMove" context:context]; + [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; + [UIView setAnimationDuration:0.5]; + rect.origin.x = self.freeRect.origin.x; + self.frame = rect; + [UIView commitAnimations]; + } else if(self.freeRect.origin.x+self.freeRect.size.width < self.frame.origin.x+self.frame.size.width){ + CGContextRef context = UIGraphicsGetCurrentContext(); + [UIView beginAnimations:@"rightMove" context:context]; + [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; + [UIView setAnimationDuration:0.5]; + rect.origin.x = self.freeRect.origin.x+self.freeRect.size.width-self.frame.size.width; + self.frame = rect; + [UIView commitAnimations]; + } + }else if(self.isKeepBounds==YES){//设置了自动粘边的效果 + if (self.frame.origin.x< centerX) { + CGContextRef context = UIGraphicsGetCurrentContext(); + [UIView beginAnimations:@"leftMove" context:context]; + [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; + [UIView setAnimationDuration:0.5]; + rect.origin.x = self.freeRect.origin.x; + self.frame = rect; + [UIView commitAnimations]; + } else { + CGContextRef context = UIGraphicsGetCurrentContext(); + [UIView beginAnimations:@"rightMove" context:context]; + [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; + [UIView setAnimationDuration:0.5]; + rect.origin.x =self.freeRect.origin.x+self.freeRect.size.width - self.frame.size.width; + self.frame = rect; + [UIView commitAnimations]; + } + } + + if (self.frame.origin.y < self.freeRect.origin.y) { + CGContextRef context = UIGraphicsGetCurrentContext(); + [UIView beginAnimations:@"topMove" context:context]; + [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; + [UIView setAnimationDuration:0.5]; + rect.origin.y = self.freeRect.origin.y; + self.frame = rect; + [UIView commitAnimations]; + } else if(self.freeRect.origin.y+self.freeRect.size.height< self.frame.origin.y+self.frame.size.height){ + CGContextRef context = UIGraphicsGetCurrentContext(); + [UIView beginAnimations:@"bottomMove" context:context]; + [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; + [UIView setAnimationDuration:0.5]; + rect.origin.y = self.freeRect.origin.y+self.freeRect.size.height-self.frame.size.height; + self.frame = rect; + [UIView commitAnimations]; + } +} +@end