|
2 | 2 |
|
3 | 3 | #import "TGColor.h" |
4 | 4 |
|
| 5 | +#import "LegacyComponentsInternal.h" |
| 6 | + |
| 7 | +@interface TGVideoMessageShimmerEffectForegroundView : UIView |
| 8 | +{ |
| 9 | + UIView *_imageContainerView; |
| 10 | + UIView *_imageView; |
| 11 | + |
| 12 | + CGFloat _size; |
| 13 | + bool _hasContainerSize; |
| 14 | + CGRect _absoluteRect; |
| 15 | + CGSize _containerSize; |
| 16 | +} |
| 17 | + |
| 18 | +- (instancetype)initWithSize:(CGFloat)size alpha:(CGFloat)alpha; |
| 19 | + |
| 20 | +@end |
| 21 | + |
| 22 | +@implementation TGVideoMessageShimmerEffectForegroundView |
| 23 | + |
| 24 | +- (instancetype)initWithSize:(CGFloat)size alpha:(CGFloat)alpha { |
| 25 | + self = [super initWithFrame:CGRectZero]; |
| 26 | + if (self != nil) { |
| 27 | + _size = size; |
| 28 | + |
| 29 | + _imageContainerView = [[UIView alloc] init]; |
| 30 | + _imageView = [[UIView alloc] init]; |
| 31 | + |
| 32 | + self.clipsToBounds = true; |
| 33 | + |
| 34 | + [_imageContainerView addSubview:_imageView]; |
| 35 | + [self addSubview:_imageContainerView]; |
| 36 | + |
| 37 | + UIGraphicsBeginImageContextWithOptions(CGSizeMake(size, 16), false, 0.0f); |
| 38 | + CGContextRef context = UIGraphicsGetCurrentContext(); |
| 39 | + |
| 40 | + CGRect bounds = CGRectMake(0, 0, size, 16); |
| 41 | + CGContextClearRect(context, bounds); |
| 42 | + CGContextClipToRect(context, bounds); |
| 43 | + |
| 44 | + UIColor *transparentColor = [UIColor colorWithWhite:1.0 alpha:0.0]; |
| 45 | + UIColor *peakColor = [UIColor colorWithWhite:1.0 alpha:alpha]; |
| 46 | + |
| 47 | + CGColorRef colors[3] = { |
| 48 | + CGColorRetain(transparentColor.CGColor), |
| 49 | + CGColorRetain(peakColor.CGColor), |
| 50 | + CGColorRetain(transparentColor.CGColor) |
| 51 | + }; |
| 52 | + |
| 53 | + CFArrayRef colorsArray = CFArrayCreate(kCFAllocatorDefault, (const void **)&colors, 3, NULL); |
| 54 | + CGFloat locations[3] = {0.0f, 0.5, 1.0}; |
| 55 | + |
| 56 | + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
| 57 | + CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, colorsArray, (CGFloat const *)&locations); |
| 58 | + |
| 59 | + CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(size, 0), kNilOptions); |
| 60 | + |
| 61 | + CFRelease(colorsArray); |
| 62 | + CFRelease(colors[0]); |
| 63 | + CFRelease(colors[1]); |
| 64 | + |
| 65 | + CGColorSpaceRelease(colorSpace); |
| 66 | + CFRelease(gradient); |
| 67 | + |
| 68 | + UIImage *image = [UIGraphicsGetImageFromCurrentImageContext() stretchableImageWithLeftCapWidth:25 topCapHeight:25]; |
| 69 | + UIGraphicsEndImageContext(); |
| 70 | + |
| 71 | + _imageView.backgroundColor = [UIColor colorWithPatternImage:image]; |
| 72 | + } |
| 73 | + return self; |
| 74 | +} |
| 75 | + |
| 76 | +- (void)updateAbsoluteRect:(CGRect)absoluteRect containerSize:(CGSize)containerSize { |
| 77 | + _hasContainerSize = true; |
| 78 | + |
| 79 | + CGRect previousAbsoluteRect = _absoluteRect; |
| 80 | + CGSize previousContainerSize = _containerSize; |
| 81 | + _absoluteRect = absoluteRect; |
| 82 | + _containerSize = containerSize; |
| 83 | + |
| 84 | + if (!CGSizeEqualToSize(previousContainerSize, containerSize)) { |
| 85 | + [self setupAnimation]; |
| 86 | + } |
| 87 | + |
| 88 | + if (!CGRectEqualToRect(previousAbsoluteRect, absoluteRect)) { |
| 89 | + _imageContainerView.frame = CGRectMake(-absoluteRect.origin.x, -absoluteRect.origin.y, containerSize.width, containerSize.height); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +- (void)setupAnimation { |
| 95 | + if (!_hasContainerSize) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + CGFloat gradientHeight = _size; |
| 100 | + _imageView.frame = CGRectMake(-gradientHeight, 0, gradientHeight, _containerSize.height); |
| 101 | + |
| 102 | + CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.x"]; |
| 103 | + animation.fromValue = @(_imageView.center.x); |
| 104 | + animation.toValue = @(_imageView.center.x + _containerSize.width + gradientHeight); |
| 105 | + animation.duration = 1.3f; |
| 106 | + animation.repeatCount = INFINITY; |
| 107 | + animation.beginTime = 1.0; |
| 108 | + [_imageView.layer addAnimation:animation forKey:@"position"]; |
| 109 | +} |
| 110 | + |
| 111 | +@end |
| 112 | + |
| 113 | +@interface TGVideoMessageShimmerView () |
| 114 | +{ |
| 115 | + TGVideoMessageShimmerEffectForegroundView *_effectView; |
| 116 | + UIImageView *_imageView; |
| 117 | + |
| 118 | + UIView *_borderView; |
| 119 | + UIView *_borderMaskView; |
| 120 | + TGVideoMessageShimmerEffectForegroundView *_borderEffectView; |
| 121 | +} |
| 122 | +@end |
| 123 | + |
| 124 | +@implementation TGVideoMessageShimmerView |
| 125 | + |
| 126 | +- (instancetype)initWithFrame:(CGRect)frame { |
| 127 | + self = [super initWithFrame:frame]; |
| 128 | + if (self != nil) { |
| 129 | + self.clipsToBounds = true; |
| 130 | + self.layer.cornerRadius = frame.size.width / 2.0f; |
| 131 | + if (iosMajorVersion() >= 13) { |
| 132 | + self.layer.cornerCurve = kCACornerCurveCircular; |
| 133 | + } |
| 134 | + |
| 135 | + _effectView = [[TGVideoMessageShimmerEffectForegroundView alloc] initWithSize:320 alpha:0.3]; |
| 136 | + _effectView.layer.compositingFilter = @"screenBlendMode"; |
| 137 | + _effectView.frame = self.bounds; |
| 138 | + |
| 139 | + _borderView = [[UIView alloc] initWithFrame:self.bounds]; |
| 140 | + _borderMaskView = [[UIView alloc] initWithFrame:self.bounds]; |
| 141 | + _borderMaskView.layer.borderWidth = 1.0; |
| 142 | + _borderMaskView.layer.borderColor = [UIColor whiteColor].CGColor; |
| 143 | + _borderMaskView.layer.cornerRadius = frame.size.width / 2.0f; |
| 144 | + if (iosMajorVersion() >= 13) { |
| 145 | + _borderMaskView.layer.cornerCurve = kCACornerCurveCircular; |
| 146 | + } |
| 147 | + _borderView.maskView = _borderMaskView; |
| 148 | + |
| 149 | + _borderEffectView = [[TGVideoMessageShimmerEffectForegroundView alloc] initWithSize:400 alpha:0.45]; |
| 150 | + _borderEffectView.layer.compositingFilter = @"screenBlendMode"; |
| 151 | + _borderEffectView.frame = self.bounds; |
| 152 | + |
| 153 | + [self addSubview:_effectView]; |
| 154 | + [self addSubview:_borderView]; |
| 155 | + [_borderView addSubview:_borderEffectView]; |
| 156 | + } |
| 157 | + return self; |
| 158 | +} |
| 159 | + |
| 160 | +- (void)updateAbsoluteRect:(CGRect)absoluteRect containerSize:(CGSize)containerSize { |
| 161 | + [_effectView updateAbsoluteRect:absoluteRect containerSize:containerSize]; |
| 162 | + [_borderEffectView updateAbsoluteRect:absoluteRect containerSize:containerSize]; |
| 163 | +} |
| 164 | + |
| 165 | +@end |
| 166 | + |
5 | 167 | @interface TGVideoMessageRingView () |
6 | 168 | { |
7 | 169 | CGFloat _value; |
|
0 commit comments