Skip to content

Commit 2b88885

Browse files
committed
优化MTKView布局和状态管理,确保视频背景色自定义和全屏模式下的布局适配
1 parent 29d936b commit 2b88885

File tree

1 file changed

+59
-100
lines changed

1 file changed

+59
-100
lines changed

DYYY.xm

Lines changed: 59 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -4448,98 +4448,84 @@ static BOOL DYYYIsLandscapeVideoBounds(CGSize size) {
44484448
return aspectRatio >= (referenceAspect - tolerance);
44494449
}
44504450

4451-
static char kDYYYMTKViewShiftAppliedOffsetKey;
4451+
%hook MTKView
44524452

4453-
static void DYYYResetMTKViewShiftState(UIView *view) {
4454-
if (!view) {
4455-
return;
4453+
- (void)layoutSubviews {
4454+
%orig;
4455+
UIViewController *vc = [DYYYUtils firstAvailableViewControllerFromView:self];
4456+
Class playVCClass = NSClassFromString(@"AWEPlayVideoViewController");
4457+
if (vc && playVCClass && [vc isKindOfClass:playVCClass]) {
4458+
NSString *colorHex = [[NSUserDefaults standardUserDefaults] objectForKey:@"DYYYVideoBGColor"];
4459+
if (colorHex && colorHex.length > 0) {
4460+
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
4461+
UIColor *customColor = [DYYYUtils colorFromSchemeHexString:colorHex targetWidth:screenWidth];
4462+
if (customColor)
4463+
self.backgroundColor = customColor;
4464+
}
44564465
}
4457-
objc_setAssociatedObject(view, &kDYYYMTKViewShiftAppliedOffsetKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
44584466
}
44594467

4460-
static CGFloat DYYYDesiredMTKViewShiftOffset(UIView *view) {
4461-
if (!view || !view.superview) {
4462-
return 0.0f;
4468+
- (void)setFrame:(CGRect)frame {
4469+
UIViewController *vc = [DYYYUtils firstAvailableViewControllerFromView:self];
4470+
Class playVCClass = NSClassFromString(@"AWEPlayVideoViewController");
4471+
BOOL isPlayVC = (vc && playVCClass && [vc isKindOfClass:playVCClass]);
4472+
4473+
objc_setAssociatedObject(self, _cmd, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
4474+
%orig(frame);
4475+
4476+
if (!isPlayVC) {
4477+
return;
4478+
}
4479+
4480+
NSNumber *storedValue = objc_getAssociatedObject(self, _cmd);
4481+
if (!self.superview) {
4482+
if (storedValue) {
4483+
objc_setAssociatedObject(self, _cmd, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
4484+
}
4485+
return;
44634486
}
4487+
44644488
if (!DYYYGetBool(@"DYYYEnableFullScreen")) {
4465-
return 0.0f;
4489+
return;
44664490
}
4467-
if (!DYYYIsLandscapeVideoBounds(view.bounds.size)) {
4468-
return 0.0f;
4491+
if (!DYYYIsLandscapeVideoBounds(self.bounds.size)) {
4492+
return;
44694493
}
4470-
CGFloat viewWidth = CGRectGetWidth(view.bounds);
4494+
4495+
CGFloat viewWidth = CGRectGetWidth(self.bounds);
44714496
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
4472-
if (viewWidth < screenWidth * 0.995f) {
4473-
return 0.0f;
4474-
}
4475-
if (gCurrentTabBarHeight <= 0.0f) {
4476-
return 0.0f;
4477-
}
4478-
return gCurrentTabBarHeight * 0.5f;
4479-
}
44804497

4481-
static void DYYYApplyMTKViewShiftIfNeeded(UIView *view) {
4482-
if (!view) {
4498+
if (viewWidth < screenWidth * 0.995f) {
44834499
return;
44844500
}
44854501

4486-
NSNumber *storedValue = objc_getAssociatedObject(view, &kDYYYMTKViewShiftAppliedOffsetKey);
4487-
if (!view.superview) {
4488-
if (storedValue) {
4489-
objc_setAssociatedObject(view, &kDYYYMTKViewShiftAppliedOffsetKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
4490-
}
4502+
CGFloat tabHeight = gCurrentTabBarHeight;
4503+
if (tabHeight <= 0.0f) {
44914504
return;
44924505
}
44934506

4494-
CGFloat desiredOffset = DYYYDesiredMTKViewShiftOffset(view);
4507+
CGFloat desiredOffset = tabHeight * 0.6f;
44954508
CGFloat appliedOffset = storedValue ? storedValue.doubleValue : 0.0f;
44964509
CGFloat delta = desiredOffset - appliedOffset;
44974510
if (fabs(delta) < 0.1f) {
44984511
if (desiredOffset <= 0.0f && storedValue) {
4499-
objc_setAssociatedObject(view, &kDYYYMTKViewShiftAppliedOffsetKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
4512+
objc_setAssociatedObject(self, _cmd, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
45004513
}
45014514
return;
45024515
}
45034516

4504-
CGPoint position = view.layer.position;
4517+
CGPoint position = self.layer.position;
45054518
position.y -= delta;
45064519

45074520
[CATransaction begin];
45084521
[CATransaction setDisableActions:YES];
4509-
view.layer.position = position;
4522+
self.layer.position = position;
45104523
[CATransaction commit];
45114524

45124525
if (desiredOffset > 0.0f) {
4513-
objc_setAssociatedObject(view, &kDYYYMTKViewShiftAppliedOffsetKey, @(desiredOffset), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
4526+
objc_setAssociatedObject(self, _cmd, @(desiredOffset), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
45144527
} else {
4515-
objc_setAssociatedObject(view, &kDYYYMTKViewShiftAppliedOffsetKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
4516-
}
4517-
}
4518-
4519-
%hook MTKView
4520-
4521-
- (void)layoutSubviews {
4522-
%orig;
4523-
UIViewController *vc = [DYYYUtils firstAvailableViewControllerFromView:self];
4524-
Class playVCClass = NSClassFromString(@"AWEPlayVideoViewController");
4525-
if (vc && playVCClass && [vc isKindOfClass:playVCClass]) {
4526-
NSString *colorHex = [[NSUserDefaults standardUserDefaults] objectForKey:@"DYYYVideoBGColor"];
4527-
if (colorHex && colorHex.length > 0) {
4528-
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
4529-
UIColor *customColor = [DYYYUtils colorFromSchemeHexString:colorHex targetWidth:screenWidth];
4530-
if (customColor)
4531-
self.backgroundColor = customColor;
4532-
}
4533-
}
4534-
}
4535-
4536-
- (void)setFrame:(CGRect)frame {
4537-
UIViewController *vc = [DYYYUtils firstAvailableViewControllerFromView:self];
4538-
Class playVCClass = NSClassFromString(@"AWEPlayVideoViewController");
4539-
DYYYResetMTKViewShiftState(self);
4540-
%orig(frame);
4541-
if (vc && playVCClass && [vc isKindOfClass:playVCClass]) {
4542-
DYYYApplyMTKViewShiftIfNeeded(self);
4528+
objc_setAssociatedObject(self, _cmd, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
45434529
}
45444530
}
45454531

@@ -5904,28 +5890,11 @@ static void *DYYYTabBarHeightContext = &DYYYTabBarHeightContext;
59045890
%orig(frame);
59055891
return;
59065892
}
5907-
5908-
if (fabs(frame.origin.x) > 0.1 || fabs(frame.origin.y) > 0.1) {
5909-
%orig(frame);
5910-
return;
5911-
}
5912-
5913-
UIView *superView = self.superview;
5914-
if (!superView) {
5915-
%orig(frame);
5916-
return;
5917-
}
5918-
5919-
CGFloat superHeight = CGRectGetHeight(superView.bounds);
5920-
CGFloat frameHeight = CGRectGetHeight(frame);
5921-
CGFloat frameWidth = CGRectGetWidth(frame);
5922-
5923-
if (superHeight > 0 && frameHeight > 0 && frameHeight < superHeight) {
5924-
CGFloat diff = superHeight - frameHeight;
5925-
BOOL isLandscapeFrame = (frameWidth > frameHeight);
5926-
5927-
if (!isLandscapeFrame && fabs(diff - gCurrentTabBarHeight) < 1.0) {
5928-
frame.size.height = superHeight;
5893+
CGRect superF = self.superview.frame;
5894+
if (CGRectGetHeight(superF) > 0 && CGRectGetHeight(frame) > 0 && CGRectGetHeight(frame) < CGRectGetHeight(superF)) {
5895+
CGFloat diff = CGRectGetHeight(superF) - CGRectGetHeight(frame);
5896+
if (fabs(diff - gCurrentTabBarHeight) < 1.0) {
5897+
frame.size.height = CGRectGetHeight(superF);
59295898
}
59305899
}
59315900

@@ -7040,21 +7009,23 @@ static Class TagViewClass = nil;
70407009
%hook TTPlayerView
70417010

70427011
- (void)setFrame:(CGRect)frame {
7043-
if (DYYYGetBool(@"DYYYEnableFullScreen")) {
7044-
CGFloat currentTabHeight = DYYYCurrentTabHeight();
7045-
if (currentTabHeight > 0.0f) {
7046-
frame.size.height += 25.0f;
7047-
}
7012+
if (DYYYGetBool(@"DYYYEnableFullScreen") && gCurrentTabBarHeight > 0.0f) {
7013+
frame.size.height += 25.0f;
70487014
}
70497015
%orig(frame);
70507016
}
70517017

70527018
%end
70537019

70547020
%hook AWELandscapeFeedEntryView
7021+
70557022
- (void)setCenter:(CGPoint)center {
70567023
if (DYYYGetBool(@"DYYYEnableFullScreen")) {
7057-
center.y += gCurrentTabBarHeight * 0.5;
7024+
UIViewController *vc = [DYYYUtils firstAvailableViewControllerFromView:self];
7025+
Class pureModeVC = NSClassFromString(@"AWEFeedPlayControlImpl.PureModePageCellViewController");
7026+
if (vc && pureModeVC && [vc isKindOfClass:pureModeVC]) {
7027+
center.y += gCurrentTabBarHeight * 0.5;
7028+
}
70587029
}
70597030

70607031
%orig(center);
@@ -7086,18 +7057,6 @@ static Class TagViewClass = nil;
70867057
%orig;
70877058
}
70887059

7089-
- (void)setCenter:(CGPoint)center {
7090-
if (DYYYGetBool(@"DYYYEnableFullScreen")) {
7091-
UIViewController *vc = [DYYYUtils firstAvailableViewControllerFromView:self];
7092-
Class pureModeVC = NSClassFromString(@"AWEFeedPlayControlImpl.PureModePageCellViewController");
7093-
if (vc && pureModeVC && [vc isKindOfClass:pureModeVC]) {
7094-
center.y += tabHeight * 0.5;
7095-
}
7096-
}
7097-
7098-
%orig(center);
7099-
}
7100-
71017060
- (void)layoutSubviews {
71027061
%orig;
71037062
if (DYYYGetBool(@"DYYYRemoveEntry")) {

0 commit comments

Comments
 (0)