diff --git a/FloatView/FloatView.xcodeproj/project.pbxproj b/FloatView/FloatView.xcodeproj/project.pbxproj index 683d6a3..d10e188 100644 --- a/FloatView/FloatView.xcodeproj/project.pbxproj +++ b/FloatView/FloatView.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ 1A89477B1E7AE73E00667E81 /* FloatViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A89477A1E7AE73E00667E81 /* FloatViewTests.m */; }; 1A8947861E7AE73E00667E81 /* FloatViewUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A8947851E7AE73E00667E81 /* FloatViewUITests.m */; }; 1AD3D4761E7BD1A200DC965F /* FloatView.m in Sources */ = {isa = PBXBuildFile; fileRef = 1AD3D4751E7BD1A200DC965F /* FloatView.m */; }; + EA1FB0C11F5D49B20074CD3C /* FloatView+Touch.m in Sources */ = {isa = PBXBuildFile; fileRef = EA1FB0C01F5D49B20074CD3C /* FloatView+Touch.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -54,6 +55,8 @@ 1A8947871E7AE73E00667E81 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 1AD3D4741E7BD1A200DC965F /* FloatView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FloatView.h; sourceTree = ""; }; 1AD3D4751E7BD1A200DC965F /* FloatView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FloatView.m; sourceTree = ""; }; + EA1FB0BF1F5D49B20074CD3C /* FloatView+Touch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "FloatView+Touch.h"; sourceTree = ""; }; + EA1FB0C01F5D49B20074CD3C /* FloatView+Touch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "FloatView+Touch.m"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -104,6 +107,7 @@ 1A89475F1E7AE73D00667E81 /* FloatView */ = { isa = PBXGroup; children = ( + EA1FB0BE1F5D49B20074CD3C /* touch */, 1AD3D4741E7BD1A200DC965F /* FloatView.h */, 1AD3D4751E7BD1A200DC965F /* FloatView.m */, 1A8947631E7AE73D00667E81 /* AppDelegate.h */, @@ -145,6 +149,15 @@ path = FloatViewUITests; sourceTree = ""; }; + EA1FB0BE1F5D49B20074CD3C /* touch */ = { + isa = PBXGroup; + children = ( + EA1FB0BF1F5D49B20074CD3C /* FloatView+Touch.h */, + EA1FB0C01F5D49B20074CD3C /* FloatView+Touch.m */, + ); + path = touch; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -212,7 +225,7 @@ TargetAttributes = { 1A89475C1E7AE73D00667E81 = { CreatedOnToolsVersion = 8.1; - DevelopmentTeam = 652U6LDTX4; + DevelopmentTeam = C353C7Q7J9; ProvisioningStyle = Automatic; }; 1A8947751E7AE73E00667E81 = { @@ -281,6 +294,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + EA1FB0C11F5D49B20074CD3C /* FloatView+Touch.m in Sources */, 1A8947681E7AE73E00667E81 /* ViewController.m in Sources */, 1A8947651E7AE73D00667E81 /* AppDelegate.m in Sources */, 1A8947621E7AE73D00667E81 /* main.m in Sources */, @@ -433,7 +447,7 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; - DEVELOPMENT_TEAM = 652U6LDTX4; + DEVELOPMENT_TEAM = C353C7Q7J9; INFOPLIST_FILE = FloatView/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = video.wangrui.com.FloatView; @@ -445,7 +459,7 @@ isa = XCBuildConfiguration; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; - DEVELOPMENT_TEAM = 652U6LDTX4; + DEVELOPMENT_TEAM = C353C7Q7J9; INFOPLIST_FILE = FloatView/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = video.wangrui.com.FloatView; diff --git a/FloatView/FloatView/touch/FloatView+Touch.h b/FloatView/FloatView/touch/FloatView+Touch.h new file mode 100644 index 0000000..76b5ed4 --- /dev/null +++ b/FloatView/FloatView/touch/FloatView+Touch.h @@ -0,0 +1,17 @@ +// +// FloatView+Touch.h +// FloatView +// +// Created by Tony on 2017/8/7. +// Copyright © 2017年 cp9. All rights reserved. +// + +#import "FloatView.h" + +/** + 处理浮动窗和首页Tab切换手势之间的冲突 + */ + +@interface FloatView (Touch) + +@end diff --git a/FloatView/FloatView/touch/FloatView+Touch.m b/FloatView/FloatView/touch/FloatView+Touch.m new file mode 100644 index 0000000..12785b0 --- /dev/null +++ b/FloatView/FloatView/touch/FloatView+Touch.m @@ -0,0 +1,160 @@ +// +// FloatView+Touch.m +// FloatView +// +// Created by Tony on 2017/8/7. +// Copyright © 2017年 cp9. All rights reserved. +// + +#import "FloatView+Touch.h" +#import + + + + + +typedef void(^TapActionWithBlock)(void); + +CGFloat distanceBetweenPoints (CGPoint first, CGPoint second) { + CGFloat deltaX = second.x - first.x; + CGFloat deltaY = second.y - first.y; + return sqrt(deltaX*deltaX + deltaY*deltaY ); +}; + +@interface FloatView () +{ + +} +@property (nonatomic,copy) TapActionWithBlock tapActionWithBlock; + +@property (nonatomic,assign) BOOL isTouchBegin; +@property (nonatomic,assign) BOOL isTouchMoved; +@property (nonatomic,assign) CGPoint touchPoint; + +- (void)moveStay; + +@end + +@implementation FloatView (Touch) + ++ (void)initialize{ + [self aop_changeMethod:@selector(initStayLocation) newMethod:@selector(aop_initStayLocation)]; + [self aop_changeMethod:@selector(touchesEnded:withEvent:) + newMethod:@selector(aop_touchesEnded:withEvent:)]; + [self aop_changeMethod:@selector(touchesMoved:withEvent:) + newMethod:@selector(aop_touchesMoved:withEvent:)]; + +} + ++(void)aop_changeMethod:(SEL)oldMethod newMethod:(SEL)newMethod +{ + Method oldM = class_getInstanceMethod([self class], oldMethod); + Method newM = class_getInstanceMethod([self class], newMethod); + method_exchangeImplementations(oldM, newM); +} + ++(void)aop_changeClassMethod:(SEL)oldMethod newMethod:(SEL)newMethod +{ + Method oldM = class_getClassMethod([self class], oldMethod); + Method newM = class_getClassMethod([self class], newMethod); + method_exchangeImplementations(oldM, newM); +} + +- (void)setTouchPoint:(CGPoint)touchPoint { + + objc_setAssociatedObject(self, @"FloatView_touchPoint", NSStringFromCGPoint(touchPoint), OBJC_ASSOCIATION_RETAIN_NONATOMIC); + +} + +- (CGPoint)touchPoint { + NSString *pointStr = objc_getAssociatedObject(self, @"FloatView_touchPoint"); + return CGPointFromString(pointStr); +} + +- (void)setTapActionWithBlock:(TapActionWithBlock)block{ + objc_setAssociatedObject(self, @"FloatView_tapActionWithBlock", block, OBJC_ASSOCIATION_COPY); +} + +- (TapActionWithBlock)tapActionWithBlock{ + return objc_getAssociatedObject(self, @"FloatView_tapActionWithBlock"); +} + +- (BOOL)isTouchMoved{ + return [objc_getAssociatedObject(self, @"FloatView_isTouchMoved") boolValue]; +} + +- (void)setIsTouchMoved:(BOOL)isTouchMoved{ + objc_setAssociatedObject(self, @"FloatView_isTouchMoved", [NSNumber numberWithBool:isTouchMoved], OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +- (BOOL)isTouchBegin{ + return [objc_getAssociatedObject(self, @"FloatView_isTouchBegin") boolValue]; +} + +- (void)setIsTouchBegin:(BOOL)isTouchBegin{ + objc_setAssociatedObject(self, @"FloatView_isTouchBegin", [NSNumber numberWithBool:isTouchBegin], OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +- (void)aop_initStayLocation{ + + UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] init]; + swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft; + swipe.delegate = self; + [self addGestureRecognizer:swipe]; + [self aop_initStayLocation]; +} + +- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ + self.isTouchBegin = YES; + self.isTouchMoved = NO; + + self.touchPoint = [[touches anyObject] locationInView:self]; +} + +- (void)aop_touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event +{ + self.isTouchBegin = NO; + [self aop_touchesEnded:touches withEvent:event]; + if (!self.isTouchMoved) { + if (self.tapActionWithBlock) { + self.tapActionWithBlock(); + } + } +} + +- (void)aop_touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event +{ + UITouch *touch = [touches anyObject]; + + if (distanceBetweenPoints([touch locationInView:self],self.touchPoint) > 10) { + self.isTouchMoved = YES; + } + [self aop_touchesMoved:touches withEvent:event]; +} + + + +- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event +{ + self.isTouchBegin = NO; + [self moveStay]; +} + +- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{ + if (self.isTouchBegin) { + return YES; + }else{ + return [super pointInside:point withEvent:event]; + } +} + + + +#pragma mark - UIGestureRecognizerDelegate +- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ + self.isTouchBegin = YES; + + return NO; +} + +@end