Skip to content

Commit 5d22aa6

Browse files
committed
fix: resolve high CPU usage caused by CoreMotion
1 parent d88cebb commit 5d22aa6

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

PlayTools/Controls/PTFakeTouch/NSObject+Swizzle.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#import <PlayTools/PlayTools-Swift.h>
1313
#import "PTFakeMetaTouch.h"
1414
#import <VideoSubscriberAccount/VideoSubscriberAccount.h>
15+
#import <CoreMotion/CoreMotion.h>
1516

1617
__attribute__((visibility("hidden")))
1718
@interface PTSwizzleLoader : NSObject
@@ -126,6 +127,15 @@ - (void) hook_setCurrentSubscription:(VSSubscription *)currentSubscription {
126127
// do nothing
127128
}
128129

130+
- (instancetype)hook_CMMotionManager_init {
131+
CMMotionManager* motionManager = (CMMotionManager*)[self hook_CMMotionManager_init];
132+
// The default update interval is 0, which may lead to high CPU usage
133+
motionManager.accelerometerUpdateInterval = 0.01;
134+
motionManager.deviceMotionUpdateInterval = 0.01;
135+
motionManager.gyroUpdateInterval = 0.01;
136+
return motionManager;
137+
}
138+
129139
- (NSString *)hook_stringByReplacingOccurrencesOfRegularExpressionPattern:(NSString *)pattern
130140
withTemplate:(NSString *)template
131141
options:(NSRegularExpressionOptions)options
@@ -264,6 +274,8 @@ + (void)load {
264274

265275
[objc_getClass("VSSubscriptionRegistrationCenter") swizzleInstanceMethod:@selector(setCurrentSubscription:) withMethod:@selector(hook_setCurrentSubscription:)];
266276

277+
[objc_getClass("CMMotionManager") swizzleInstanceMethod:@selector(init) withMethod:@selector(hook_CMMotionManager_init)];
278+
267279
if (PlayInfo.isUnrealEngine) {
268280
// Fix NSRegularExpression crash when system language is set to Chinese
269281
CFStringEncoding encoding = CFStringGetSystemEncoding();

0 commit comments

Comments
 (0)