Skip to content

Instantly share code, notes, and snippets.

@VictorZhang2014
Created January 14, 2017 05:01
Show Gist options
  • Save VictorZhang2014/04fa0c8084c86cd4c8ad2445ca850d8b to your computer and use it in GitHub Desktop.
Save VictorZhang2014/04fa0c8084c86cd4c8ad2445ca850d8b to your computer and use it in GitHub Desktop.
Bubble Intersection in iOS
@interface MyView : UIView
@property (nonatomic, assign) BOOL isMySelf;
@end
@implementation MyView
- (instancetype)init
{
self = [super init];
if (self) {
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)drawUI:(CGRect)rect
{
UIView *bottomWhite = [[UIView alloc] initWithFrame:CGRectMake(0, rect.size.height - 20, rect.size.width, 20)];
bottomWhite.backgroundColor = [UIColor whiteColor];
//This is a background image
UIImage *backgroundImg = [UIImage imageNamed:@"redPacket-bg"];
UIImageView *backgroundPic = [[UIImageView alloc] initWithImage:backgroundImg];
CGRect imgRect = rect;
imgRect.size.height -= 20;
backgroundPic.frame = imgRect;
[backgroundPic addSubview:bottomWhite];
[self addSubview:backgroundPic];
//This is a bubble image
UIImage *bubbleImg = [UIImage imageNamed:@"pop_position_pressed_left"];
if (self.isMySelf) {
bubbleImg = [UIImage imageNamed:@"pop_position_pressed_right"];
}
bubbleImg = [bubbleImg stretchableImageWithLeftCapWidth:14 topCapHeight:26];
UIImageView *imgViewBubble = [[UIImageView alloc] initWithImage:bubbleImg];
imgViewBubble.frame = rect;
[self addSubview:imgViewBubble];
//Intersection
CALayer *layer = imgViewBubble.layer;
CGSize size = imgViewBubble.layer.frame.size;
layer.frame = (CGRect){{0,0},size};
backgroundPic.layer.mask = layer;
[backgroundPic setNeedsDisplay];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment