Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Class to display the animated gold roundrect used on OS X for matches.
- */
- @interface FindHighlightLayer : CAGradientLayer
- {
- @private
- NSString *sFind;
- int positionFind;
- BOOL retaining;
- CGFloat widthText;
- CGFloat heightLine;
- NSString *sFont;
- CGFloat fontSize;
- }
- @property (copy) NSString *sFind;
- @property (assign) int positionFind;
- @property (assign) BOOL retaining;
- @property (assign) CGFloat widthText;
- @property (assign) CGFloat heightLine;
- @property (copy) NSString *sFont;
- @property (assign) CGFloat fontSize;
- - (void) animateMatch: (CGPoint)ptText bounce:(BOOL)bounce;
- - (void) hideMatch;
- @end
- //--------------------------------------------------------------------------------------------------
- @implementation FindHighlightLayer
- @synthesize sFind, positionFind, retaining, widthText, heightLine, sFont, fontSize;
- -(id) init {
- if (self = [super init]) {
- [self setNeedsDisplayOnBoundsChange: YES];
- // A gold to slightly redder gradient to match other applications
- CGColorRef colGold = CGColorCreateGenericRGB(1.0, 1.0, 0, 1.0);
- CGColorRef colGoldRed = CGColorCreateGenericRGB(1.0, 0.8, 0, 1.0);
- self.colors = [NSArray arrayWithObjects:(id)colGoldRed, (id)colGold, nil];
- CGColorRelease(colGoldRed);
- CGColorRelease(colGold);
- CGColorRef colGreyBorder = CGColorCreateGenericGray(0.756f, 0.5f);
- self.borderColor = colGreyBorder;
- CGColorRelease(colGreyBorder);
- self.borderWidth = 1.0;
- self.cornerRadius = 5.0f;
- self.shadowRadius = 1.0f;
- self.shadowOpacity = 0.9f;
- self.shadowOffset = CGSizeMake(0.0f, -2.0f);
- self.anchorPoint = CGPointMake(0.5, 0.5);
- }
- return self;
- }
- const CGFloat paddingHighlightX = 4;
- const CGFloat paddingHighlightY = 2;
- -(void) drawInContext:(CGContextRef)context {
- if (!sFind || !sFont)
- return;
- CFStringRef str = CFStringRef(sFind);
- CFMutableDictionaryRef styleDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 2,
- &kCFTypeDictionaryKeyCallBacks,
- &kCFTypeDictionaryValueCallBacks);
- CGColorRef color = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0);
- CFDictionarySetValue(styleDict, kCTForegroundColorAttributeName, color);
- CTFontRef fontRef = ::CTFontCreateWithName((CFStringRef)sFont, fontSize, NULL);
- CFDictionaryAddValue(styleDict, kCTFontAttributeName, fontRef);
- CFAttributedStringRef attrString = ::CFAttributedStringCreate(NULL, str, styleDict);
- CTLineRef textLine = ::CTLineCreateWithAttributedString(attrString);
- // Indent from corner of bounds
- CGContextSetTextPosition(context, paddingHighlightX, 3 + paddingHighlightY);
- CTLineDraw(textLine, context);
- CFRelease(textLine);
- CFRelease(attrString);
- CFRelease(fontRef);
- CGColorRelease(color);
- CFRelease(styleDict);
- }
- - (void) animateMatch: (CGPoint)ptText bounce:(BOOL)bounce {
- if (!self.sFind || ![self.sFind length])
- return;
- CGFloat width = self.widthText + paddingHighlightX * 2;
- CGFloat height = self.heightLine + paddingHighlightY * 2;
- // Adjust for padding
- ptText.x -= paddingHighlightX;
- ptText.y += paddingHighlightY;
- // Shift point to centre as expanding about centre
- ptText.x += width / 2.0;
- ptText.y -= height / 2.0;
- [CATransaction begin];
- [CATransaction setValue:[NSNumber numberWithFloat:0.0] forKey:kCATransactionAnimationDuration];
- self.bounds = CGRectMake(0,0, width, height);
- self.position = ptText;
- if (bounce) {
- // Do not reset visibility when just moving
- self.hidden = NO;
- self.opacity = 1.0;
- }
- [self setNeedsDisplay];
- [CATransaction commit];
- if (bounce) {
- CABasicAnimation *animBounce = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
- animBounce.duration = 0.15;
- animBounce.autoreverses = YES;
- animBounce.removedOnCompletion = NO;
- animBounce.fromValue = [NSNumber numberWithFloat: 1.0];
- animBounce.toValue = [NSNumber numberWithFloat: 1.25];
- if (self.retaining) {
- [self addAnimation: animBounce forKey:@"animateFound"];
- } else {
- CABasicAnimation *animFade = [CABasicAnimation animationWithKeyPath:@"opacity"];
- animFade.duration = 0.1;
- animFade.beginTime = 0.4;
- animFade.removedOnCompletion = NO;
- animFade.fromValue = [NSNumber numberWithFloat: 1.0];
- animFade.toValue = [NSNumber numberWithFloat: 0.0];
- CAAnimationGroup *group = [CAAnimationGroup animation];
- [group setDuration:0.5];
- group.removedOnCompletion = NO;
- group.fillMode = kCAFillModeForwards;
- [group setAnimations:[NSArray arrayWithObjects:animBounce, animFade, nil]];
- [self addAnimation:group forKey:@"animateFound"];
- }
- }
- }
- - (void) hideMatch {
- self.sFind = @"";
- self.positionFind = INVALID_POSITION;
- self.hidden = YES;
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement