Merge MatrixKit develop with commit hash: b85b736313bec0592bd1cabc68035d97f5331137

This commit is contained in:
SBiOSoftWhare
2021-12-03 11:47:24 +01:00
parent af65f71177
commit f57941177e
475 changed files with 87437 additions and 0 deletions
@@ -0,0 +1,45 @@
/*
Copyright 2017 Aram Sargsyan
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>
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, PhotoBrowserAnimationType) {
PhotoBrowserZoomInAnimation,
PhotoBrowserZoomOutAnimation
};
@protocol MXKSourceAttachmentAnimatorDelegate <NSObject>
- (UIImageView *)originalImageView;
- (CGRect)convertedFrameForOriginalImageView;
@end
@protocol MXKDestinationAttachmentAnimatorDelegate <NSObject>
- (UIImageView *)finalImageView;
@end
@interface MXKAttachmentAnimator : NSObject <UIViewControllerAnimatedTransitioning>
- (instancetype)initWithAnimationType:(PhotoBrowserAnimationType)animationType sourceViewController:(UIViewController <MXKSourceAttachmentAnimatorDelegate> *)viewController;
+ (CGRect)aspectFitImage:(UIImage *)image inFrame:(CGRect)targetFrame;
@end
@@ -0,0 +1,161 @@
/*
Copyright 2017 Aram Sargsyan
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 "MXKAttachmentAnimator.h"
#import "MXLog.h"
@interface MXKAttachmentAnimator ()
@property (nonatomic) PhotoBrowserAnimationType animationType;
@property (nonatomic, weak) UIViewController <MXKSourceAttachmentAnimatorDelegate> *sourceViewController;
@end
@implementation MXKAttachmentAnimator
#pragma mark - Lifecycle
- (instancetype)initWithAnimationType:(PhotoBrowserAnimationType)animationType sourceViewController:(UIViewController <MXKSourceAttachmentAnimatorDelegate> *)viewController
{
self = [self init];
if (self) {
self.animationType = animationType;
self.sourceViewController = viewController;
}
return self;
}
#pragma mark - Public
+ (CGRect)aspectFitImage:(UIImage *)image inFrame:(CGRect)targetFrame
{
// Sanity check
if (!image)
{
MXLogDebug(@"[MXKAttachmentAnimator] aspectFitImage failed: image is nil");
return CGRectZero;
}
if (CGSizeEqualToSize(image.size, targetFrame.size))
{
return targetFrame;
}
CGFloat targetWidth = CGRectGetWidth(targetFrame);
CGFloat targetHeight = CGRectGetHeight(targetFrame);
CGFloat imageWidth = image.size.width;
CGFloat imageHeight = image.size.height;
CGFloat factor = MIN(targetWidth/imageWidth, targetHeight/imageHeight);
CGSize finalSize = CGSizeMake(imageWidth * factor, imageHeight * factor);
CGRect finalFrame = CGRectMake((targetWidth - finalSize.width)/2 + targetFrame.origin.x, (targetHeight - finalSize.height)/2 + targetFrame.origin.y, finalSize.width, finalSize.height);
return finalFrame;
}
#pragma mark - Animations
- (void)animateZoomInAnimation:(id<UIViewControllerContextTransitioning>)transitionContext
{
//originalImageView
UIImageView *originalImageView = [self.sourceViewController originalImageView];
originalImageView.hidden = YES;
CGRect convertedFrame = [self.sourceViewController convertedFrameForOriginalImageView];
//toViewController
UIViewController<MXKDestinationAttachmentAnimatorDelegate> *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
toViewController.view.frame = [transitionContext finalFrameForViewController:toViewController];
[[transitionContext containerView] addSubview:toViewController.view];
toViewController.view.alpha = 0.0;
//destinationImageView
UIImageView *destinationImageView = [toViewController finalImageView];
destinationImageView.hidden = YES;
//transitioningImageView
UIImageView *transitioningImageView = [[UIImageView alloc] initWithImage:originalImageView.image];
transitioningImageView.frame = convertedFrame;
[[transitionContext containerView] addSubview:transitioningImageView];
CGRect finalFrameForTransitioningView = [[self class] aspectFitImage:originalImageView.image inFrame:toViewController.view.frame];
//animation
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
toViewController.view.alpha = 1.0;
transitioningImageView.frame = finalFrameForTransitioningView;
} completion:^(BOOL finished) {
[transitioningImageView removeFromSuperview];
destinationImageView.hidden = NO;
originalImageView.hidden = NO;
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
- (void)animateZoomOutAnimation:(id<UIViewControllerContextTransitioning>)transitionContext
{
//fromViewController
UIViewController<MXKDestinationAttachmentAnimatorDelegate> *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIImageView *destinationImageView = [fromViewController finalImageView];
destinationImageView.hidden = YES;
//toViewController
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
toViewController.view.frame = [transitionContext finalFrameForViewController:toViewController];
[[transitionContext containerView] insertSubview:toViewController.view belowSubview:fromViewController.view];
UIImageView *originalImageView = [self.sourceViewController originalImageView];
originalImageView.hidden = YES;
CGRect convertedFrame = [self.sourceViewController convertedFrameForOriginalImageView];
//transitioningImageView
UIImageView *transitioningImageView = [[UIImageView alloc] initWithImage:destinationImageView.image];
transitioningImageView.frame = [[self class] aspectFitImage:destinationImageView.image inFrame:destinationImageView.frame];
[[transitionContext containerView] addSubview:transitioningImageView];
//animation
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
fromViewController.view.alpha = 0.0;
transitioningImageView.frame = convertedFrame;
} completion:^(BOOL finished) {
[transitioningImageView removeFromSuperview];
destinationImageView.hidden = NO;
originalImageView.hidden = NO;
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
#pragma mark - UIViewControllerAnimatedTransitioning
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 0.3;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
switch (self.animationType) {
case PhotoBrowserZoomInAnimation:
[self animateZoomInAnimation:transitionContext];
break;
case PhotoBrowserZoomOutAnimation:
[self animateZoomOutAnimation:transitionContext];
break;
}
}
@end
@@ -0,0 +1,26 @@
/*
Copyright 2017 Aram Sargsyan
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 <UIKit/UIKit.h>
#import "MXKAttachmentAnimator.h"
@interface MXKAttachmentInteractionController : UIPercentDrivenInteractiveTransition
@property (nonatomic) BOOL interactionInProgress;
- (instancetype)initWithDestinationViewController:(UIViewController <MXKDestinationAttachmentAnimatorDelegate> *)viewController sourceViewController:(UIViewController <MXKSourceAttachmentAnimatorDelegate> *)sourceViewController;
@end
@@ -0,0 +1,204 @@
/*
Copyright 2017 Aram Sargsyan
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 "MXKAttachmentInteractionController.h"
#import "MXLog.h"
@interface MXKAttachmentInteractionController ()
@property (nonatomic, weak) UIViewController <MXKDestinationAttachmentAnimatorDelegate> *destinationViewController;
@property (nonatomic, weak) UIViewController <MXKSourceAttachmentAnimatorDelegate> *sourceViewController;
@property (nonatomic) UIImageView *transitioningImageView;
@property (nonatomic, weak) id <UIViewControllerContextTransitioning> transitionContext;
@property (nonatomic) CGPoint translation;
@property (nonatomic) CGPoint delta;
@end
@implementation MXKAttachmentInteractionController
#pragma mark - Lifecycle
- (instancetype)initWithDestinationViewController:(UIViewController <MXKDestinationAttachmentAnimatorDelegate> *)viewController sourceViewController:(UIViewController <MXKSourceAttachmentAnimatorDelegate> *)sourceViewController
{
self = [super init];
if (self) {
self.destinationViewController = viewController;
self.sourceViewController = sourceViewController;
self.interactionInProgress = NO;
[self preparePanGestureRecognizerInView:viewController.view];
}
return self;
}
#pragma mark - Gesture recognizer
- (void)preparePanGestureRecognizerInView:(UIView *)view
{
UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
recognizer.minimumNumberOfTouches = 1;
recognizer.maximumNumberOfTouches = 3;
[view addGestureRecognizer:recognizer];
}
- (void)handleGesture:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:self.destinationViewController.view];
self.delta = CGPointMake(translation.x - self.translation.x, translation.y - self.translation.y);
self.translation = translation;
switch (recognizer.state) {
case UIGestureRecognizerStateBegan:
self.interactionInProgress = YES;
if (self.destinationViewController.navigationController) {
[self.destinationViewController.navigationController popViewControllerAnimated:YES];
} else {
[self.destinationViewController dismissViewControllerAnimated:YES completion:nil];
}
break;
case UIGestureRecognizerStateChanged:
[self updateInteractiveTransition:(ABS(translation.y) / (CGRectGetHeight(self.destinationViewController.view.frame) / 2))];
break;
case UIGestureRecognizerStateCancelled:
self.interactionInProgress = NO;
[self cancelInteractiveTransition];
break;
case UIGestureRecognizerStateEnded:
self.interactionInProgress = NO;
if (ABS(self.translation.y) < CGRectGetHeight(self.destinationViewController.view.frame)/6) {
[self cancelInteractiveTransition];
} else {
[self finishInteractiveTransition];
}
break;
default:
MXLogDebug(@"UIGestureRecognizerState not handled");
break;
}
}
#pragma mark - UIPercentDrivenInteractiveTransition
- (void)startInteractiveTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
self.transitionContext = transitionContext;
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIImageView *destinationImageView = [self.destinationViewController finalImageView];
destinationImageView.hidden = YES;
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
toViewController.view.frame = [transitionContext finalFrameForViewController:toViewController];
[[transitionContext containerView] insertSubview:toViewController.view belowSubview:fromViewController.view];
UIImageView *originalImageView = [self.sourceViewController originalImageView];
originalImageView.hidden = YES;
self.transitioningImageView = [[UIImageView alloc] initWithImage:destinationImageView.image];
self.transitioningImageView.frame = [MXKAttachmentAnimator aspectFitImage:destinationImageView.image inFrame:destinationImageView.frame];
[[transitionContext containerView] addSubview:self.transitioningImageView];
}
- (void)updateInteractiveTransition:(CGFloat)percentComplete {
self.destinationViewController.view.alpha = MAX(0, (1 - percentComplete));
CGRect newFrame = CGRectMake(self.transitioningImageView.frame.origin.x, self.transitioningImageView.frame.origin.y + self.delta.y, CGRectGetWidth(self.transitioningImageView.frame), CGRectGetHeight(self.transitioningImageView.frame));
self.transitioningImageView.frame = newFrame;
}
- (void)cancelInteractiveTransition {
UIViewController *fromViewController = [self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIImageView *destinationImageView = [self.destinationViewController finalImageView];
UIImageView *originalImageView = [self.sourceViewController originalImageView];
__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:([self transitionDuration:self.transitionContext]/2) animations:^{
if (weakSelf)
{
typeof(self) self = weakSelf;
fromViewController.view.alpha = 1;
self.transitioningImageView.frame = [MXKAttachmentAnimator aspectFitImage:destinationImageView.image inFrame:destinationImageView.frame];
}
} completion:^(BOOL finished) {
if (weakSelf)
{
typeof(self) self = weakSelf;
destinationImageView.hidden = NO;
originalImageView.hidden = NO;
[self.transitioningImageView removeFromSuperview];
[self.transitionContext cancelInteractiveTransition];
[self.transitionContext completeTransition:NO];
}
}];
}
- (void)finishInteractiveTransition
{
UIViewController *fromViewController = [self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIImageView *destinationImageView = [self.destinationViewController finalImageView];
UIImageView *originalImageView = [self.sourceViewController originalImageView];
CGRect originalImageViewFrame = [self.sourceViewController convertedFrameForOriginalImageView];
__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:[self transitionDuration:self.transitionContext] animations:^{
if (weakSelf)
{
typeof(self) self = weakSelf;
fromViewController.view.alpha = 0.0;
self.transitioningImageView.frame = originalImageViewFrame;
}
} completion:^(BOOL finished) {
if (weakSelf)
{
typeof(self) self = weakSelf;
[self.transitioningImageView removeFromSuperview];
destinationImageView.hidden = NO;
originalImageView.hidden = NO;
[self.transitionContext finishInteractiveTransition];
[self.transitionContext completeTransition:YES];
}
}];
}
#pragma mark - UIViewControllerAnimatedTransitioning
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 0.3;
}
@end