-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support shared element transition for images with different resizeMode (
#7045) This PR adds support for animating images with different `resizeMode` which already supported on Android and was missing on iOS.
- Loading branch information
Showing
35 changed files
with
727 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,74 @@ | ||
#import "AnimatedImageView.h" | ||
#import "BoundsTransition.h" | ||
#import "CenterTransition.h" | ||
#import "UIImageView+Transition.h" | ||
|
||
@implementation AnimatedImageView | ||
@implementation AnimatedImageView { | ||
SharedElementTransitionOptions *_transitionOptions; | ||
CGRect _originalBounds; | ||
CGPoint _originalCenter; | ||
UIViewContentMode _originalContentMode; | ||
} | ||
|
||
- (instancetype)initElement:(UIView *)element | ||
toElement:(UIView *)toElement | ||
transitionOptions:(SharedElementTransitionOptions *)transitionOptions { | ||
self = [super initElement:element toElement:toElement transitionOptions:transitionOptions]; | ||
self.contentMode = element.contentMode; | ||
|
||
_transitionOptions = transitionOptions; | ||
|
||
_fromImageView = [self findImageView:element]; | ||
_toImageView = [self findImageView:toElement]; | ||
_originalBounds = _fromImageView.bounds; | ||
_originalContentMode = _fromImageView.contentMode; | ||
_originalCenter = _fromImageView.center; | ||
_fromImageView.autoresizingMask = | ||
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; | ||
|
||
return self; | ||
} | ||
|
||
- (UIImageView *)findImageView:(UIView *)view { | ||
if ([view isKindOfClass:UIImageView.class]) { | ||
return (UIImageView *)view; | ||
} | ||
|
||
for (UIView *subview in view.subviews) { | ||
return [self findImageView:subview]; | ||
} | ||
|
||
return nil; | ||
} | ||
|
||
- (NSArray<id<DisplayLinkAnimation>> *)extraAnimations { | ||
NSMutableArray *animations = NSMutableArray.new; | ||
CGFloat startDelay = [_transitionOptions.startDelay withDefault:0]; | ||
CGFloat duration = [_transitionOptions.duration withDefault:300]; | ||
id<Interpolator> interpolator = _transitionOptions.interpolator; | ||
|
||
[animations addObject:[[BoundsTransition alloc] initWithView:_fromImageView | ||
from:_fromImageView.resolveBounds | ||
to:_toImageView.resolveBounds | ||
startDelay:startDelay | ||
duration:duration | ||
interpolator:interpolator]]; | ||
|
||
[animations addObject:[[CenterTransition alloc] initWithView:_fromImageView | ||
from:_fromImageView.center | ||
to:_toImageView.center | ||
startDelay:startDelay | ||
duration:duration | ||
interpolator:interpolator]]; | ||
_fromImageView.contentMode = UIViewContentModeScaleToFill; | ||
|
||
return animations; | ||
} | ||
|
||
- (void)reset { | ||
[super reset]; | ||
_fromImageView.bounds = _originalBounds; | ||
_fromImageView.contentMode = _originalContentMode; | ||
_fromImageView.center = _originalCenter; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#import "AnimatedImageView.h" | ||
|
||
@interface AnimatedUIImageView : AnimatedImageView | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#import "AnimatedUIImageView.h" | ||
|
||
@implementation AnimatedUIImageView | ||
|
||
- (void)layoutSubviews { | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#import "ElementBaseTransition.h" | ||
|
||
@interface BoundsTransition : ElementBaseTransition | ||
|
||
@property(nonatomic) CGRect fromBounds; | ||
@property(nonatomic) CGRect toBounds; | ||
|
||
- (instancetype)initWithView:(UIView *)view | ||
from:(CGRect)from | ||
to:(CGRect)to | ||
startDelay:(NSTimeInterval)startDelay | ||
duration:(NSTimeInterval)duration | ||
interpolator:(id<Interpolator>)interpolator; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#import "BoundsTransition.h" | ||
|
||
@implementation BoundsTransition | ||
|
||
- (instancetype)initWithView:(UIView *)view | ||
from:(CGRect)from | ||
to:(CGRect)to | ||
startDelay:(NSTimeInterval)startDelay | ||
duration:(NSTimeInterval)duration | ||
interpolator:(id<Interpolator>)interpolator { | ||
self = [super initWithView:view | ||
startDelay:startDelay | ||
duration:duration | ||
interpolator:interpolator]; | ||
_fromBounds = from; | ||
_toBounds = to; | ||
return self; | ||
} | ||
|
||
- (CATransform3D)animateWithProgress:(CGFloat)p { | ||
CGRect toBounds = [RNNInterpolator fromRect:_fromBounds | ||
toRect:_toBounds | ||
precent:p | ||
interpolator:self.interpolator]; | ||
self.view.bounds = toBounds; | ||
|
||
return CATransform3DIdentity; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#import "ElementBaseTransition.h" | ||
|
||
@interface CenterTransition : ElementBaseTransition | ||
|
||
@property(nonatomic) CGPoint fromCenter; | ||
@property(nonatomic) CGPoint toCenter; | ||
|
||
- (instancetype)initWithView:(UIView *)view | ||
from:(CGPoint)from | ||
to:(CGPoint)to | ||
startDelay:(NSTimeInterval)startDelay | ||
duration:(NSTimeInterval)duration | ||
interpolator:(id<Interpolator>)interpolator; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#import "CenterTransition.h" | ||
|
||
@implementation CenterTransition | ||
|
||
- (instancetype)initWithView:(UIView *)view | ||
from:(CGPoint)from | ||
to:(CGPoint)to | ||
startDelay:(NSTimeInterval)startDelay | ||
duration:(NSTimeInterval)duration | ||
interpolator:(id<Interpolator>)interpolator { | ||
self = [super initWithView:view | ||
startDelay:startDelay | ||
duration:duration | ||
interpolator:interpolator]; | ||
self.fromCenter = from; | ||
self.toCenter = to; | ||
return self; | ||
} | ||
|
||
- (CATransform3D)animateWithProgress:(CGFloat)p { | ||
CGPoint toCenter = [RNNInterpolator fromPoint:self.fromCenter | ||
toPoint:self.toCenter | ||
precent:p | ||
interpolator:self.interpolator]; | ||
self.view.center = toCenter; | ||
return CATransform3DIdentity; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#import "RectTransition.h" | ||
|
||
@interface PathTransition : RectTransition | ||
|
||
@property(nonatomic) CGFloat fromCornerRadius; | ||
@property(nonatomic) CGFloat toCornerRadius; | ||
|
||
- (instancetype)initWithView:(UIView *)view | ||
fromPath:(CGRect)fromPath | ||
toPath:(CGRect)toPath | ||
fromCornerRadius:(CGFloat)fromCornerRadius | ||
toCornerRadius:(CGFloat)toCornerRadius | ||
startDelay:(NSTimeInterval)startDelay | ||
duration:(NSTimeInterval)duration | ||
interpolator:(id<Interpolator>)interpolator; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#import "PathTransition.h" | ||
|
||
@implementation PathTransition | ||
|
||
- (instancetype)initWithView:(UIView *)view | ||
fromPath:(CGRect)fromPath | ||
toPath:(CGRect)toPath | ||
fromCornerRadius:(CGFloat)fromCornerRadius | ||
toCornerRadius:(CGFloat)toCornerRadius | ||
startDelay:(NSTimeInterval)startDelay | ||
duration:(NSTimeInterval)duration | ||
interpolator:(id<Interpolator>)interpolator { | ||
self = [super initWithView:view | ||
from:fromPath | ||
to:toPath | ||
startDelay:startDelay | ||
duration:duration | ||
interpolator:interpolator]; | ||
self.fromCornerRadius = fromCornerRadius; | ||
self.toCornerRadius = toCornerRadius; | ||
|
||
return self; | ||
} | ||
|
||
- (CATransform3D)animateWithProgress:(CGFloat)p { | ||
CGRect toPath = [RNNInterpolator fromRect:self.from | ||
toRect:self.to | ||
precent:p | ||
interpolator:self.interpolator]; | ||
CGFloat toRadius = [RNNInterpolator fromFloat:self.fromCornerRadius | ||
toFloat:self.toCornerRadius | ||
precent:p | ||
interpolator:self.interpolator]; | ||
CAShapeLayer *mask = [CAShapeLayer new]; | ||
mask.path = [UIBezierPath bezierPathWithRoundedRect:toPath cornerRadius:toRadius].CGPath; | ||
self.view.layer.mask = mask; | ||
return CATransform3DIdentity; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.