Skip to content

Commit

Permalink
fix: 添加滑动等事件touch的监听,处理圈选时滑动不刷新界面问题
Browse files Browse the repository at this point in the history
  • Loading branch information
CaicaiNo committed Dec 22, 2020
1 parent c4d8126 commit ab7019d
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ NS_ASSUME_NONNULL_BEGIN
from:(nullable id)sender
forEvent:(nullable UIEvent *)event;

//- (void)growing_setDelegate:(id<UIApplicationDelegate>)delegate;

//- (BOOL)growing_application:(UIApplication *)application
// openURL:(NSURL *)url
// sourceApplication:(NSString *)sourceApplication
// annotation:(id)annotation;
- (void)growing_sendEvent:(UIEvent *)event;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#import "GrowingApplicationEventManager.h"
#import "GrowingCocoaLumberjack.h"
#import "GrowingNodeHelper.h"
#import "GrowingNodeProtocol.h"
Expand All @@ -25,6 +26,12 @@

@implementation UIApplication (GrowingAutotracker)

- (void)growing_sendEvent:(UIEvent *)event {
[self growing_sendEvent:event];
//触摸 滑动都会在这里触发
[[GrowingApplicationEventManager sharedInstance] dispatchApplicationEventSendEvent:event];
}

- (BOOL)growing_sendAction:(SEL)action to:(id)target from:(id)sender forEvent:(UIEvent *)event {
BOOL result = YES;
// 切换 tab,采集切换之后的页面信息,所以需要先调用 growing_sendAction 完成切换
Expand Down Expand Up @@ -60,14 +67,15 @@ - (void)growing_trackAction:(SEL)action to:(id)target from:(id)sender forEvent:(
if ([sender isKindOfClass:UISwitch.class] || [sender isKindOfClass:UIStepper.class] ||
[sender isKindOfClass:UIPageControl.class]) {
[GrowingViewClickProvider viewOnClick:node];
return;
}

if ([event isKindOfClass:[UIEvent class]] && event.type == UIEventTypeTouches &&
[[[event allTouches] anyObject] phase] == UITouchPhaseEnded) {
} else if ([event isKindOfClass:[UIEvent class]] && event.type == UIEventTypeTouches &&
[[[event allTouches] anyObject] phase] == UITouchPhaseEnded) {
[GrowingViewClickProvider viewOnClick:node];
return;
}

[[GrowingApplicationEventManager sharedInstance] dispatchApplicationEventSendAction:action
to:target
from:sender
forEvent:event];
}

@end
7 changes: 6 additions & 1 deletion GrowingAutotrackerCore/GrowingRealAutotracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ - (void)addAutoTrackSwizzles {
if (applicatonError) {
GIOLogError(@"Failed to swizzle UIApplication. Details: %@", applicatonError);
}

[UIApplication growing_swizzleMethod:@selector(sendEvent:)
withMethod:@selector(growing_sendEvent:)
error:&applicatonError];
if (applicatonError) {
GIOLogError(@"Failed to swizzle UIApplication sendEvent. Details: %@", applicatonError);
}
// UISegmentControl
NSError *segmentControlError = NULL;
[UISegmentedControl growing_swizzleMethod:@selector(initWithCoder:)
Expand Down
49 changes: 49 additions & 0 deletions GrowingAutotrackerCore/Manager/GrowingApplicationEventManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// GrowingApplicationEventManager.h
// GrowingAnalytics
//
// Created by sheng on 2020/12/22.
// Copyright (C) 2017 Beijing Yishu Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN
@protocol GrowingApplicationEventProtocol <NSObject>
@optional
- (void)growingApplicationEventSendAction:(SEL)action
to:(nullable id)target
from:(nullable id)sender
forEvent:(nullable UIEvent *)event;

- (void)growingApplicationEventSendEvent:(UIEvent *)event;
@end
@interface GrowingApplicationEventManager : NSObject

+ (instancetype)sharedInstance;

- (void)addApplicationEventObserver:(id<GrowingApplicationEventProtocol>)delegate;

- (void)removeApplicationEventObserver:(id<GrowingApplicationEventProtocol>)delegate;

- (void)dispatchApplicationEventSendAction:(SEL)action
to:(nullable id)target
from:(nullable id)sender
forEvent:(nullable UIEvent *)event;

- (void)dispatchApplicationEventSendEvent:(UIEvent *)event;

@end

NS_ASSUME_NONNULL_END
103 changes: 103 additions & 0 deletions GrowingAutotrackerCore/Manager/GrowingApplicationEventManager.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//
// GrowingApplicationEventManager.m
// GrowingAnalytics
//
// Created by sheng on 2020/12/22.
// Copyright (C) 2017 Beijing Yishu Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import "GrowingApplicationEventManager.h"

@interface GrowingApplicationEventManager ()
@property (strong, nonatomic, readonly) NSPointerArray *observers;
@property (strong, nonatomic, readonly) NSLock *observerLock;
@end

@implementation GrowingApplicationEventManager

- (instancetype)init {
self = [super init];
if (self) {
_observers = [NSPointerArray pointerArrayWithOptions:NSPointerFunctionsWeakMemory];
_observerLock = [[NSLock alloc] init];
}
return self;
}

+ (instancetype)sharedInstance {
static id _sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[super allocWithZone:NULL] init];
});

return _sharedInstance;
}
// for safe sharedInstance
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
return [self sharedInstance];
}

- (id)copyWithZone:(NSZone *)zone {
return self;
}

- (id)mutableCopyWithZone:(NSZone *)zone {
return self;
}

- (void)addApplicationEventObserver:(id<GrowingApplicationEventProtocol>)delegate {
[self.observerLock lock];
if (![self.observers.allObjects containsObject:delegate]) {
[self.observers addPointer:(__bridge void *)delegate];
}
[self.observerLock unlock];
}

- (void)removeApplicationEventObserver:(id<GrowingApplicationEventProtocol>)delegate {
[self.observerLock lock];
[self.observers.allObjects enumerateObjectsWithOptions:NSEnumerationReverse
usingBlock:^(NSObject *obj, NSUInteger idx, BOOL *_Nonnull stop) {
if (delegate == obj) {
[self.observers removePointerAtIndex:idx];
*stop = YES;
}
}];
[self.observerLock unlock];
}

- (void)dispatchApplicationEventSendAction:(SEL)action
to:(nullable id)target
from:(nullable id)sender
forEvent:(nullable UIEvent *)event {
[self.observerLock lock];
for (id observer in self.observers) {
if ([observer respondsToSelector:@selector(growingApplicationEventSendAction:to:from:forEvent:)]) {
[observer growingApplicationEventSendAction:action to:target from:sender forEvent:event];
}
}
[self.observerLock unlock];
}

- (void)dispatchApplicationEventSendEvent:(UIEvent *)event {
[self.observerLock lock];
for (id observer in self.observers) {
if ([observer respondsToSelector:@selector(growingApplicationEventSendEvent:)]) {
[observer growingApplicationEventSendEvent:event];
}
}
[self.observerLock unlock];
}

@end
34 changes: 23 additions & 11 deletions GrowingAutotrackerCore/Manager/GrowingViewControllerLifecycle.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#import "GrowingViewControllerLifecycle.h"

@interface GrowingViewControllerLifecycle ()
@property(strong, nonatomic, readonly) NSPointerArray *viewControllerLifecycleDelegates;
@property(strong, nonatomic, readonly) NSLock *delegateLock;
@property (strong, nonatomic, readonly) NSPointerArray *viewControllerLifecycleDelegates;
@property (strong, nonatomic, readonly) NSLock *delegateLock;
@end

@implementation GrowingViewControllerLifecycle
Expand All @@ -24,11 +24,23 @@ + (instancetype)sharedInstance {
static id _sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[self alloc] init];
_sharedInstance = [[super allocWithZone:NULL] init];
});

return _sharedInstance;
}
// for safe sharedInstance
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
return [self sharedInstance];
}

- (id)copyWithZone:(NSZone *)zone {
return self;
}

- (id)mutableCopyWithZone:(NSZone *)zone {
return self;
}

- (void)addViewControllerLifecycleDelegate:(id)delegate {
[self.delegateLock lock];
Expand All @@ -41,14 +53,14 @@ - (void)addViewControllerLifecycleDelegate:(id)delegate {
- (void)removeViewControllerLifecycleDelegate:(id)delegate {
[self.delegateLock lock];
[self.viewControllerLifecycleDelegates.allObjects
enumerateObjectsWithOptions:NSEnumerationReverse
usingBlock:^(NSObject *obj, NSUInteger idx, BOOL *_Nonnull stop) {
if (delegate == obj) {
[self.viewControllerLifecycleDelegates removePointerAtIndex:idx];
*stop = YES;
}
}];
enumerateObjectsWithOptions:NSEnumerationReverse
usingBlock:^(NSObject *obj, NSUInteger idx, BOOL *_Nonnull stop) {
if (delegate == obj) {
[self.viewControllerLifecycleDelegates removePointerAtIndex:idx];
*stop = YES;
}
}];

[self.delegateLock unlock];
}

Expand Down
10 changes: 2 additions & 8 deletions GrowingAutotrackerCore/WebCircle/GrowingWebCircle.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// limitations under the License.

#import <Foundation/Foundation.h>

#import "GrowingNode.h"
#import "GrowingSRWebSocket.h"

Expand All @@ -27,14 +28,7 @@
+ (instancetype)shareInstance;

+ (BOOL)isRunning;
+ (void)runWithCircle:(NSURL *)url readyBlock:(void(^)(void))readyBlock finishBlock:(void(^)(void))finishBlock;
+ (void)runWithCircle:(NSURL *)url readyBlock:(void (^)(void))readyBlock finishBlock:(void (^)(void))finishBlock;
+ (void)stop;

+ (void)setNeedUpdateScreen;

// for webview based add-tag-menu
+ (CGFloat)impressScale;
+ (BOOL)isContainer:(id<GrowingNode>)node;
+ (void)retrieveAllElementsAsync:(void(^)(NSString *))callback;

@end
Loading

0 comments on commit ab7019d

Please sign in to comment.