ZLCellFakeView.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // ZLCellFakeView.m
  3. // ZLCollectionView
  4. //
  5. // Created by zhaoliang chen on 2018/7/25.
  6. // Copyright © 2018年 zhaoliang chen. All rights reserved.
  7. //
  8. #import "ZLCellFakeView.h"
  9. @implementation ZLCellFakeView
  10. - (instancetype)initWithCell:(UICollectionViewCell *)cell{
  11. self = [super initWithFrame:cell.frame];
  12. if (self) {
  13. self.cell = cell;
  14. self.layer.shadowColor = [UIColor blackColor].CGColor;
  15. self.layer.shadowOffset = CGSizeMake(0, 0);
  16. self.layer.shadowOpacity = 0;
  17. self.layer.shadowRadius = 5.0;
  18. self.layer.shouldRasterize = false;
  19. self.layer.masksToBounds = YES;
  20. self.clipsToBounds = YES;
  21. self.cellFakeImageView = [[UIImageView alloc]initWithFrame:self.bounds];
  22. self.cellFakeImageView.contentMode = UIViewContentModeScaleAspectFill;
  23. self.cellFakeImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  24. self.cellFakeHightedView = [[UIImageView alloc]initWithFrame:self.bounds];
  25. self.cellFakeHightedView.contentMode = UIViewContentModeScaleAspectFill;
  26. self.cellFakeHightedView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  27. cell.highlighted = YES;
  28. self.cellFakeHightedView.image = [self getCellImage];
  29. cell.highlighted = NO;
  30. self.cellFakeImageView.image = [self getCellImage];
  31. [self addSubview:self.cellFakeImageView];
  32. [self addSubview:self.cellFakeHightedView];
  33. }
  34. return self;
  35. }
  36. - (void)changeBoundsIfNeeded:(CGRect)bounds{
  37. if (CGRectEqualToRect(self.bounds, bounds)) {
  38. return;
  39. }
  40. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionBeginFromCurrentState animations:^{
  41. self.bounds = bounds;
  42. } completion:nil];
  43. }
  44. - (void)pushFowardView{
  45. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionBeginFromCurrentState animations:^{
  46. self.center = self.originalCenter;
  47. self.transform = CGAffineTransformMakeScale(1.1, 1.1);
  48. self.cellFakeHightedView.alpha = 0;
  49. CABasicAnimation *shadowAnimation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
  50. shadowAnimation.fromValue = @(0);
  51. shadowAnimation.toValue = @(0.7);
  52. shadowAnimation.removedOnCompletion = NO;
  53. shadowAnimation.fillMode = kCAFillModeForwards;
  54. [self.layer addAnimation:shadowAnimation forKey:@"applyShadow"];
  55. } completion:^(BOOL finished) {
  56. [self.cellFakeHightedView removeFromSuperview];
  57. }];
  58. }
  59. - (void)pushBackView:(void(^)(void))completion{
  60. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionBeginFromCurrentState animations:^{
  61. //self.transform = CGAffineTransformIdentity;
  62. //self.frame = self.cellFrame;
  63. CABasicAnimation *shadowAnimation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
  64. shadowAnimation.fromValue = @(0.7);
  65. shadowAnimation.toValue = @(0);
  66. shadowAnimation.removedOnCompletion = NO;
  67. shadowAnimation.fillMode = kCAFillModeForwards;
  68. [self.layer addAnimation:shadowAnimation forKey:@"removeShadow"];
  69. } completion:^(BOOL finished) {
  70. if (completion) {
  71. completion();
  72. }
  73. }];
  74. }
  75. - (UIImage *)getCellImage{
  76. UIGraphicsBeginImageContextWithOptions(_cell.bounds.size, NO, [UIScreen mainScreen].scale * 2);
  77. [self.cell.layer renderInContext:UIGraphicsGetCurrentContext()];
  78. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  79. UIGraphicsEndImageContext();
  80. return image;
  81. }
  82. @end