Last active
September 7, 2016 02:08
-
-
Save quesera2/fb00ac792e6b3625cf12 to your computer and use it in GitHub Desktop.
片道UIViewControllerTransition
This file contains hidden or 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
#import <UIKit/UIKit.h> | |
@interface FromViewController : UIViewController | |
@end |
This file contains hidden or 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
#import "FromViewController.h" | |
#import "ToViewController.h" | |
#import "TransitionController.h" | |
#import "MyImageCell.h" | |
@interface FromViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UINavigationControllerDelegate> | |
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView; | |
@property (strong, nonatomic) NSMutableArray *images; | |
@end | |
@implementation FromViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
self.images = [NSMutableArray array]; | |
for(int i=1; i<=6; i++){ | |
[self.images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d", i]]]; | |
} | |
} | |
#pragma mark - UIViewController LifeCycle | |
-(void)viewDidAppear:(BOOL)animated | |
{ | |
[super viewDidAppear:animated]; | |
self.navigationController.delegate = self; | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
} | |
#pragma mark - UIViewController Transition | |
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender | |
{ | |
if([segue.identifier isEqualToString:@"detailImage"]){ | |
ToViewController *vc = (ToViewController *)[segue destinationViewController]; | |
NSIndexPath *idx = self.collectionView.indexPathsForSelectedItems[0]; | |
vc.imageName = [NSString stringWithFormat:@"%d", (int)(idx.row + 1)]; | |
} | |
} | |
- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController | |
animationControllerForOperation:(UINavigationControllerOperation)operation | |
fromViewController:(UIViewController *)fromVC | |
toViewController:(UIViewController *)toVC | |
{ | |
NSIndexPath *idx = self.collectionView.indexPathsForSelectedItems[0]; | |
MyImageCell *aCell = (MyImageCell *)[self.collectionView cellForItemAtIndexPath:idx]; | |
return [[TransitionController alloc] initWithImageView:aCell.imageView]; | |
} | |
#pragma mark - UICollectionViewDataSource/UICollectionViewDelegate | |
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section | |
{ | |
return section == 0 ? self.images.count : 0; | |
} | |
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath; | |
{ | |
MyImageCell *aCell = (MyImageCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath: indexPath]; | |
aCell.imageView.image = self.images[indexPath.row]; | |
return aCell; | |
} | |
- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
CGFloat width = self.view.bounds.size.width; | |
return CGSizeMake(width/2-10, width/2-10); | |
} | |
@end |
This file contains hidden or 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
#import <UIKit/UIKit.h> | |
@interface MyImageCell : UICollectionViewCell | |
@property (weak, nonatomic) IBOutlet UIImageView *imageView; | |
@end |
This file contains hidden or 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
#import "MyImageCell.h" | |
@implementation MyImageCell | |
@end |
This file contains hidden or 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
#import <UIKit/UIKit.h> | |
@interface ToViewController : UIViewController | |
@property (copy, nonatomic) NSString *imageName; | |
@end |
This file contains hidden or 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
#import "ToViewController.h" | |
@interface ToViewController () | |
@property (weak, nonatomic) IBOutlet UIImageView *imageView; | |
@end | |
@implementation ToViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
self.imageView.image = [UIImage imageNamed:self.imageName]; | |
} | |
- (void)viewDidAppear:(BOOL)animated | |
{ | |
[super viewDidAppear:animated]; | |
self.navigationController.delegate = nil; | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
} | |
@end |
This file contains hidden or 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
#import <UIKit/UIKit.h> | |
@interface TransitionController : NSObject <UIViewControllerAnimatedTransitioning> | |
-(instancetype)initWithImageView:(UIImageView *)imageView; | |
@end |
This file contains hidden or 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
#import "TransitionController.h" | |
@interface TransitionController () | |
@property (strong, nonatomic) UIImageView *imageView; | |
@end | |
@implementation TransitionController | |
-(instancetype)initWithImageView:(UIImageView *)imageView | |
{ | |
self = [super init]; | |
if (self) { | |
self.imageView = imageView; | |
} | |
return self; | |
} | |
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext | |
{ | |
return 0.5; | |
} | |
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext | |
{ | |
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey]; | |
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey]; | |
UIView *containerView = [transitionContext containerView]; | |
CGRect frame = [self.imageView convertRect:self.imageView.bounds toView:containerView]; | |
UIImageView *movingImageView = [[UIImageView alloc] initWithImage:self.imageView.image]; | |
movingImageView.frame = frame; | |
movingImageView.contentMode = self.imageView.contentMode; | |
[containerView addSubview:movingImageView]; | |
self.imageView.hidden = YES; | |
// StatusBar And NavigationBar | |
CGRect toFrame = toView.frame; | |
toFrame.origin.y += 64; | |
toFrame.size.height -= 64; | |
[UIView animateWithDuration:0.5 | |
animations:^{ | |
fromView.alpha = 0.0; | |
movingImageView.frame = toFrame; | |
} completion:^(BOOL finished) { | |
fromView.alpha = 1.0; | |
self.imageView.hidden = NO; | |
[containerView addSubview:toView]; | |
[movingImageView removeFromSuperview]; | |
BOOL isComplete = ![transitionContext transitionWasCancelled]; | |
[transitionContext completeTransition:isComplete]; | |
} | |
]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment