ShakeLabel2.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // ShakeLabel2.m
  3. // BuguLive
  4. //
  5. // Created by xfg on 16/5/23.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "ShakeLabel2.h"
  9. @implementation ShakeLabel2
  10. - (void)startAnimWithDuration:(NSTimeInterval)duration {
  11. __weak ShakeLabel2 *ws = self;
  12. [UIView animateKeyframesWithDuration:duration delay:0 options:0 animations:^{
  13. [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1/2.0 animations:^{
  14. self.transform = CGAffineTransformMakeScale(5, 5);
  15. }];
  16. [UIView addKeyframeWithRelativeStartTime:1/2.0 relativeDuration:1/2.0 animations:^{
  17. self.transform = CGAffineTransformMakeScale(0.7, 0.7);
  18. }];
  19. } completion:^(BOOL finished) {
  20. [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
  21. self.transform = CGAffineTransformMakeScale(1.0, 1.0);
  22. } completion:^(BOOL finished) {
  23. if (ws.delegate && [ws.delegate respondsToSelector:@selector(shakeLabelAnimateFinished2)]) {
  24. [ws.delegate shakeLabelAnimateFinished2];
  25. }
  26. }];
  27. }];
  28. }
  29. // 重写 drawTextInRect 文字描边效果
  30. - (void)drawTextInRect:(CGRect)rect {
  31. CGSize shadowOffset = self.shadowOffset;
  32. UIColor *textColor = self.textColor;
  33. CGContextRef c = UIGraphicsGetCurrentContext();
  34. CGContextSetLineWidth(c, 2);
  35. CGContextSetLineJoin(c, kCGLineJoinRound);
  36. CGContextSetTextDrawingMode(c, kCGTextStroke);
  37. self.textColor = _borderColor;
  38. [super drawTextInRect:rect];
  39. CGContextSetTextDrawingMode(c, kCGTextFill);
  40. self.textColor = textColor;
  41. self.shadowOffset = CGSizeMake(0, 0);
  42. [super drawTextInRect:rect];
  43. self.shadowOffset = shadowOffset;
  44. }
  45. @end