AFDescribeObj.m 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // AFDescribeObj.m
  3. // animationFrame
  4. //
  5. // Created by pingshw on 14-5-27.
  6. // Copyright (c) 2014年 pingshw. All rights reserved.
  7. //
  8. #import "AFDescribeObj.h"
  9. @implementation AFDescribeObj
  10. {
  11. NSArray *types;
  12. CGFloat startTime;
  13. CGFloat duration;
  14. }
  15. @synthesize containerView;
  16. @synthesize type;
  17. @synthesize shapeLayers;
  18. @synthesize basicAnimation;
  19. - (id)initWithType:(AFAnimationType)af_type inView:(UIView *)view
  20. {
  21. self = [super init];
  22. if (self)
  23. {
  24. self.type = af_type;
  25. self.containerView = view;
  26. shapeLayers = [NSArray array];
  27. types = @[@"strokeEnd"];
  28. }
  29. return self;
  30. }
  31. - (void)setStartTime:(CGFloat)af_startTime andDuration:(CGFloat)af_duration
  32. {
  33. startTime = af_startTime;
  34. duration = af_duration;
  35. basicAnimation = [CABasicAnimation animationWithKeyPath:types[type]];
  36. basicAnimation.delegate = containerView;
  37. basicAnimation.duration = af_duration+af_startTime;
  38. basicAnimation.fromValue = [NSNumber numberWithFloat:-af_startTime/af_duration];
  39. basicAnimation.toValue = [NSNumber numberWithFloat:1.0];
  40. }
  41. - (void)addLineFrom:(CGPoint)from toPoint:(CGPoint)to color:(UIColor *)color
  42. {
  43. UIBezierPath *path = [UIBezierPath bezierPath];
  44. [path moveToPoint:from];
  45. [path addLineToPoint:to];
  46. CAShapeLayer *pathLayer = [CAShapeLayer layer];
  47. pathLayer.frame = containerView.bounds;
  48. pathLayer.path = path.CGPath;
  49. pathLayer.strokeColor = color.CGColor;
  50. pathLayer.lineWidth = 2.0f;
  51. pathLayer.lineJoin = kCALineJoinBevel;
  52. [pathLayer addAnimation:basicAnimation forKey:types[type]];
  53. NSMutableArray *ary = [NSMutableArray arrayWithArray:shapeLayers];
  54. [ary addObject:pathLayer];
  55. shapeLayers = [NSArray arrayWithArray:ary];
  56. [containerView.layer addSublayer:pathLayer];
  57. }
  58. @end