Add inner shadows to media thumbnails.

pull/1/head
Matthew Chen 7 years ago
parent ade88966c8
commit d161e5ff3d

@ -24,11 +24,16 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, nullable) UIColor *strokeColor; @property (nonatomic, nullable) UIColor *strokeColor;
@property (nonatomic) CGFloat strokeThickness; @property (nonatomic) CGFloat strokeThickness;
@property (nonatomic, nullable) UIColor *innerShadowColor;
@property (nonatomic) CGFloat innerShadowRadius;
@property (nonatomic) float innerShadowOpacity;
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
+ (OWSBubbleShapeView *)bubbleDrawView; + (OWSBubbleShapeView *)bubbleDrawView;
+ (OWSBubbleShapeView *)bubbleShadowView; + (OWSBubbleShapeView *)bubbleShadowView;
+ (OWSBubbleShapeView *)bubbleClipView; + (OWSBubbleShapeView *)bubbleClipView;
+ (OWSBubbleShapeView *)bubbleInnerShadowView;
@end @end

@ -13,6 +13,7 @@ typedef NS_ENUM(NSUInteger, OWSBubbleShapeViewMode) {
OWSBubbleShapeViewMode_Draw, OWSBubbleShapeViewMode_Draw,
OWSBubbleShapeViewMode_Shadow, OWSBubbleShapeViewMode_Shadow,
OWSBubbleShapeViewMode_Clip, OWSBubbleShapeViewMode_Clip,
OWSBubbleShapeViewMode_InnerShadow,
}; };
@interface OWSBubbleShapeView () @interface OWSBubbleShapeView ()
@ -71,6 +72,13 @@ typedef NS_ENUM(NSUInteger, OWSBubbleShapeViewMode) {
return instance; return instance;
} }
+ (OWSBubbleShapeView *)bubbleInnerShadowView
{
OWSBubbleShapeView *instance = [OWSBubbleShapeView new];
instance.mode = OWSBubbleShapeViewMode_InnerShadow;
return instance;
}
- (void)setFillColor:(nullable UIColor *)fillColor - (void)setFillColor:(nullable UIColor *)fillColor
{ {
_fillColor = fillColor; _fillColor = fillColor;
@ -92,6 +100,27 @@ typedef NS_ENUM(NSUInteger, OWSBubbleShapeViewMode) {
[self updateLayers]; [self updateLayers];
} }
- (void)setInnerShadowColor:(nullable UIColor *)innerShadowColor
{
_innerShadowColor = innerShadowColor;
[self updateLayers];
}
- (void)setInnerShadowRadius:(CGFloat)innerShadowRadius
{
_innerShadowRadius = innerShadowRadius;
[self updateLayers];
}
- (void)setInnerShadowOpacity:(float)innerShadowOpacity
{
_innerShadowOpacity = innerShadowOpacity;
[self updateLayers];
}
- (void)setFrame:(CGRect)frame - (void)setFrame:(CGRect)frame
{ {
BOOL didChange = !CGRectEqualToRect(self.frame, frame); BOOL didChange = !CGRectEqualToRect(self.frame, frame);
@ -188,6 +217,32 @@ typedef NS_ENUM(NSUInteger, OWSBubbleShapeViewMode) {
self.maskLayer.path = bezierPath.CGPath; self.maskLayer.path = bezierPath.CGPath;
self.layer.mask = self.maskLayer; self.layer.mask = self.maskLayer;
break; break;
case OWSBubbleShapeViewMode_InnerShadow: {
self.maskLayer.path = bezierPath.CGPath;
self.layer.mask = self.maskLayer;
// Inner shadow.
// This should usually not be visible; it is used to distinguish
// profile pics from the background if they are similar.
self.shapeLayer.frame = self.bounds;
self.shapeLayer.masksToBounds = YES;
CGRect shadowBounds = self.bounds;
UIBezierPath *shadowPath = [bezierPath copy];
// This can be any value large enough to cast a sufficiently large shadow.
CGFloat shadowInset = -(self.innerShadowRadius * 4.f);
[shadowPath
appendPath:[UIBezierPath bezierPathWithRect:CGRectInset(shadowBounds, shadowInset, shadowInset)]];
// This can be any color since the fill should be clipped.
self.shapeLayer.fillColor = UIColor.blackColor.CGColor;
self.shapeLayer.path = shadowPath.CGPath;
self.shapeLayer.fillRule = kCAFillRuleEvenOdd;
self.shapeLayer.shadowColor = self.innerShadowColor.CGColor;
self.shapeLayer.shadowRadius = self.innerShadowRadius;
self.shapeLayer.shadowOpacity = self.innerShadowOpacity;
self.shapeLayer.shadowOffset = CGSizeZero;
break;
}
} }
[CATransaction commit]; [CATransaction commit];

@ -352,13 +352,14 @@ const UIDataDetectorTypes kOWSAllowedDataDetectorTypes
if (self.hasBodyMediaWithThumbnail) { if (self.hasBodyMediaWithThumbnail) {
[self.stackView addArrangedSubview:bodyMediaView]; [self.stackView addArrangedSubview:bodyMediaView];
OWSBubbleShapeView *strokeView = [OWSBubbleShapeView bubbleDrawView]; OWSBubbleShapeView *innerShadowView = [OWSBubbleShapeView bubbleInnerShadowView];
strokeView.strokeThickness = CGHairlineWidth(); innerShadowView.innerShadowColor
strokeView.strokeColor = (Theme.isDarkThemeEnabled ? [UIColor colorWithWhite:1.f alpha:0.2f] = (Theme.isDarkThemeEnabled ? UIColor.ows_whiteColor : UIColor.ows_blackColor);
: [UIColor colorWithWhite:0.f alpha:0.2f]); innerShadowView.innerShadowRadius = 0.5f;
[bodyMediaView addSubview:strokeView]; innerShadowView.innerShadowOpacity = 0.15f;
[self.bubbleView addPartnerView:strokeView]; [bodyMediaView addSubview:innerShadowView];
[self.viewConstraints addObjectsFromArray:[strokeView ows_autoPinToSuperviewEdges]]; [self.bubbleView addPartnerView:innerShadowView];
[self.viewConstraints addObjectsFromArray:[innerShadowView ows_autoPinToSuperviewEdges]];
} else { } else {
OWSAssertDebug(self.cellType == OWSMessageCellType_ContactShare); OWSAssertDebug(self.cellType == OWSMessageCellType_ContactShare);

Loading…
Cancel
Save