Skip to content

Commit 9ce0c98

Browse files
committed
Support SwiftUI/LNPopupUI popup bar images for crossfade
Improve `LNPopupImageView` content mode support
1 parent 3b2335e commit 9ce0c98

11 files changed

+86
-26
lines changed

LNPopupController/LNPopupController/Private/LNPopupImageView.mm

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,37 @@ - (void)setContents:(id)contents
5454
[_imageContentsLayer setContents:contents];
5555
}
5656

57+
- (void)setContentsRect:(CGRect)contentsRect
58+
{
59+
[_imageContentsLayer setContentsRect:contentsRect];
60+
}
61+
62+
- (void)setContentsScale:(CGFloat)contentsScale
63+
{
64+
[_imageContentsLayer setContentsScale:contentsScale];
65+
}
66+
67+
- (void)setContentsCenter:(CGRect)contentsCenter
68+
{
69+
[_imageContentsLayer setContentsCenter:contentsCenter];
70+
}
71+
72+
- (void)setContentsFormat:(CALayerContentsFormat)contentsFormat
73+
{
74+
[_imageContentsLayer setContentsFormat:contentsFormat];
75+
}
76+
77+
- (void)setContentsGravity:(CALayerContentsGravity)contentsGravity
78+
{
79+
[_imageContentsLayer setContentsGravity:contentsGravity];
80+
}
81+
82+
- (void)setWantsExtendedDynamicRangeContent:(BOOL)wantsExtendedDynamicRangeContent
83+
{
84+
[super setWantsExtendedDynamicRangeContent:wantsExtendedDynamicRangeContent];
85+
[_imageContentsLayer setWantsExtendedDynamicRangeContent:wantsExtendedDynamicRangeContent];
86+
}
87+
5788
- (void)setImageContentsLayer:(CALayer*)layer
5889
{
5990
_imageContentsLayer = layer;

LNPopupController/LNPopupController/Private/_LNPopupTransitionAnimator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ NS_ASSUME_NONNULL_BEGIN
2929
@property (nonatomic, strong, readonly) LNPopupContentView* popupContentView;
3030

3131
@property (nonatomic, strong, readonly, nullable) _LNPopupTransitionView* transitionView;
32-
@property (nonatomic, strong, readonly, nullable) UIImageView* crossfadeImageView;
32+
@property (nonatomic, strong, readonly, nullable) UIView<LNPopupTransitionView>* crossfadeView;
3333
@property (nonatomic, readonly) CGRect sourceFrame;
3434
@property (nonatomic, readonly) CGRect targetFrame;
3535
@property (nonatomic, readonly) CGAffineTransform transform;

LNPopupController/LNPopupController/Private/_LNPopupTransitionAnimator.mm

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import "_LNPopupTransitionAnimator.h"
10+
#import "LNPopupBar+Private.h"
1011
#import <LNPopupController/UIViewController+LNPopupSupport.h>
1112
#import <objc/runtime.h>
1213

@@ -43,18 +44,35 @@ - (void)animateWithAnimator:(UIViewPropertyAnimator *)animator otherAnimations:(
4344
_transitionView = [[_LNPopupTransitionView alloc] initWithSourceView:self.view];
4445
}
4546

46-
_crossfadeImageView = [[UIImageView alloc] initWithImage:self.popupBar.imageView.image];
47-
_crossfadeImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
48-
_crossfadeImageView.contentMode = self.popupBar.imageView.contentMode;
49-
_crossfadeImageView.frame = _transitionView.bounds;
50-
_crossfadeImageView.layer.masksToBounds = YES;
51-
[_transitionView addSubview:_crossfadeImageView];
47+
UIImage* image;
48+
if(self.popupBar.swiftuiImageController != nil)
49+
{
50+
id contents = self.popupBar.swiftuiImageController.view.subviews.firstObject.layer.contents;
51+
if(contents != nil && CFGetTypeID((__bridge CFTypeRef)contents) == CGImageGetTypeID())
52+
{
53+
image = [[UIImage alloc] initWithCGImage:(__bridge CGImageRef)contents];
54+
}
55+
else
56+
{
57+
image = [[[UIGraphicsImageRenderer alloc] initWithSize:self.popupBar.imageView.bounds.size] imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
58+
[self.popupBar.imageView drawViewHierarchyInRect:self.popupBar.imageView.bounds afterScreenUpdates:NO];
59+
}];
60+
}
61+
}
62+
else
63+
{
64+
image = self.popupBar.imageView.image;
65+
}
66+
67+
_crossfadeView = [[LNPopupImageView alloc] initWithImage:image];
68+
_crossfadeView.contentMode = self.popupBar.imageView.contentMode;
69+
_crossfadeView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
70+
_crossfadeView.frame = _transitionView.bounds;
71+
[_transitionView addSubview:_crossfadeView];
5272

5373
_transitionView.frame = self.sourceFrame;
5474
[self beforeAnyAnimation];
5575

56-
_crossfadeImageView.layer.cornerCurve = kCACornerCurveContinuous;
57-
5876
objc_setAssociatedObject(self.transitionView.sourceView, _LNPopupOpenCloseTransitionViewKey, _transitionView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
5977
}];
6078

LNPopupController/LNPopupController/Private/_LNPopupTransitionCloseAnimator.m

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ - (CGRect)targetFrame
3636

3737
- (CGFloat)scaledBarImageViewCornerRadius
3838
{
39-
return self.popupBar.imageView.cornerRadius * self.sourceFrame.size.width / self.popupBar.imageView.bounds.size.width;
39+
if(self.sourceFrame.size.width > self.sourceFrame.size.height)
40+
{
41+
return self.popupBar.imageView.cornerRadius * self.sourceFrame.size.width / self.popupBar.imageView.bounds.size.width;
42+
}
43+
else
44+
{
45+
return self.popupBar.imageView.cornerRadius * self.sourceFrame.size.height / self.popupBar.imageView.bounds.size.height;
46+
}
4047
}
4148

4249
- (NSShadow *)scaledBarImageViewShadow
@@ -55,15 +62,15 @@ - (void)beforeAnyAnimation
5562
{
5663
[super beforeAnyAnimation];
5764

58-
self.crossfadeImageView.alpha = 0.0;
59-
self.crossfadeImageView.layer.cornerRadius = self.transitionView.cornerRadius;
65+
self.crossfadeView.alpha = 0.0;
66+
self.crossfadeView.cornerRadius = self.transitionView.cornerRadius;
6067
}
6168

6269
- (void)performAdditionalAnimations
6370
{
6471
[self.transitionView setTargetFrameUpdatingTransform:self.targetFrame];
6572

66-
self.crossfadeImageView.layer.cornerRadius = self.popupBar.imageView.cornerRadius;
73+
self.crossfadeView.cornerRadius = self.popupBar.imageView.cornerRadius;
6774

6875
[super performAdditionalAnimations];
6976
}
@@ -84,7 +91,7 @@ - (void)performAdditionalDelayed015Animations
8491
}
8592
}
8693

87-
self.crossfadeImageView.alpha = 1.0;
94+
self.crossfadeView.alpha = 1.0;
8895
}
8996

9097
- (void)performAdditionalCompletion

LNPopupController/LNPopupController/Private/_LNPopupTransitionGenericOpenAnimator.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@ - (void)beforeAnyAnimation
1818
[super beforeAnyAnimation];
1919

2020
self.transitionView.shadow = self.popupBar.imageView.shadow.copy;
21+
self.transitionView.cornerRadius = self.scaledBarImageViewCornerRadius;
2122

2223
_targetShadow = [self.transitionView.shadow copy];
2324
_targetShadow.shadowColor = [_targetShadow.shadowColor colorWithAlphaComponent:0.0];
2425
}
2526

2627
- (void)performAdditionalAnimations
2728
{
28-
self.transitionView.shadow = _targetShadow;
29-
3029
[super performAdditionalAnimations];
30+
31+
self.transitionView.shadow = _targetShadow;
32+
self.transitionView.cornerRadius = 0.000001;
3133
}
3234

3335
@end

LNPopupController/LNPopupController/Private/_LNPopupTransitionOpenAnimator.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ - (void)beforeAnyAnimation
4949
{
5050
[super beforeAnyAnimation];
5151

52-
self.crossfadeImageView.alpha = 1.0;
53-
self.crossfadeImageView.layer.cornerRadius = self.popupBar.imageView.cornerRadius;
52+
self.crossfadeView.alpha = 1.0;
53+
self.crossfadeView.cornerRadius = self.popupBar.imageView.cornerRadius;
5454
}
5555

5656
- (void)performBeforeAdditionalAnimations
@@ -66,14 +66,14 @@ - (void)performAdditionalAnimations
6666

6767
self.transitionView.frame = self.targetFrame;
6868
self.transitionView.sourceViewTransform = CGAffineTransformIdentity;
69+
self.crossfadeView.cornerRadius = self.transitionView.cornerRadius;
6970
}
7071

7172
- (void)performAdditional01Animations
7273
{
7374
[super performAdditional01Animations];
7475

75-
self.crossfadeImageView.alpha = 0.0;
76-
self.crossfadeImageView.layer.cornerRadius = self.transitionView.cornerRadius;
76+
self.crossfadeView.alpha = 0.0;
7777
}
7878

7979
@end

LNPopupController/LNPopupController/Private/_LNPopupTransitionPreferredCloseAnimator.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ - (void)beforeAnyAnimation
4040
_originalShadow = nil;
4141
}
4242

43-
self.crossfadeImageView.layer.cornerRadius = self.view.cornerRadius;
43+
self.crossfadeView.cornerRadius = self.view.cornerRadius;
4444
}
4545

4646
- (void)performAdditionalAnimations
4747
{
4848
[super performAdditionalAnimations];
4949

5050
self.view.cornerRadius = self.scaledBarImageViewCornerRadius;
51+
self.crossfadeView.cornerRadius = self.popupBar.imageView.cornerRadius;
5152
if(_supportsShadow)
5253
{
5354
self.view.shadow = self.scaledBarImageViewShadow;

LNPopupController/LNPopupController/Private/_LNPopupTransitionPreferredOpenAnimator.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ - (void)performAdditionalAnimations
6767
self.transitionView.shadow = _originalShadow;
6868
}
6969

70-
self.crossfadeImageView.layer.cornerRadius = self.view.cornerRadius;
70+
self.crossfadeView.cornerRadius = self.view.cornerRadius;
7171
}
7272

7373
@end

LNPopupController/LNPopupController/Private/_LNPopupTransitionView.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
//
88

99
#import <UIKit/UIKit.h>
10+
#import <LNPopupController/UIViewController+LNPopupSupport.h>
1011

1112
NS_ASSUME_NONNULL_BEGIN
1213

13-
@interface _LNPopupTransitionView : UIView
14+
@interface _LNPopupTransitionView : UIView <LNPopupTransitionView>
1415

1516
+ (instancetype)transitionViewWithSourceView:(UIView*)sourceView;
1617

@@ -20,7 +21,7 @@ NS_ASSUME_NONNULL_BEGIN
2021

2122
@property (nonatomic, strong, readonly) UIView* sourceView;
2223

23-
@property (nonatomic, strong) NSShadow* shadow;
24+
@property (nonatomic, copy) NSShadow* shadow;
2425
@property (nonatomic, assign) CGFloat cornerRadius;
2526
@property (nonatomic, assign) BOOL layerAlwaysMasksToBounds;
2627

LNPopupController/LNPopupController/Private/_LNPopupTransitionView.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ - (instancetype)initWithSourceView:(UIView*)sourceView
3333
[_portalView setValue:sourceView forKey:LNPopupHiddenString("sourceView")];
3434
[_portalView setValue:@YES forKey:LNPopupHiddenString("hidesSourceView")];
3535
[_portalView setValue:@YES forKey:LNPopupHiddenString("matchesTransform")];
36-
_portalView.layer.contentsGravity = kCAGravityCenter;
36+
_portalView.layer.contentsGravity = kCAGravityResize;
3737

3838
_radiusContainerView = [[UIView alloc] initWithFrame:CGRectZero];
3939
_radiusContainerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

0 commit comments

Comments
 (0)