No Description

FKPaySuccessAnimView.m 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // FKPaySuccessAnimView.m
  3. // FirstLink
  4. //
  5. // Created by jack on 15/9/16.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "FKPaySuccessAnimView.h"
  9. @interface FKPaySuccessAnimView ()
  10. //@property (nonatomic, weak) id delegate;
  11. @property (nonatomic, strong) CAShapeLayer *animLayer;
  12. @property (nonatomic, strong) UILabel *titleLabel;
  13. @property (nonatomic, copy) void(^finish)(BOOL done);
  14. @end
  15. @implementation FKPaySuccessAnimView
  16. - (instancetype)initWithFrame:(CGRect)frame{
  17. if (self = [super initWithFrame:frame]) {
  18. [self addAllSubViews];
  19. }
  20. return self;
  21. }
  22. - (void)addAllSubViews{
  23. [self.layer addSublayer:self.animLayer];
  24. [self addSubview:self.titleLabel];
  25. }
  26. + (void)showInView:(UIView *)view finish:(void(^)(BOOL done))finish{
  27. for (UIView *subView in view.subviews) {
  28. if ([subView isKindOfClass:[FKPaySuccessAnimView class]]){
  29. [subView removeFromSuperview];
  30. }
  31. }
  32. CGPoint center = [view convertPoint:view.window.center fromView:view.window];
  33. FKPaySuccessAnimView *animView = [[FKPaySuccessAnimView alloc]init];
  34. [view addSubview:animView];
  35. animView.bounds = CGRectMake(0, 0, 200, 130);
  36. animView.center = center;
  37. animView.finish = finish;
  38. [animView startAnimatrion];
  39. }
  40. - (void)startAnimatrion{
  41. CABasicAnimation *tailAnimation = [CABasicAnimation animation];
  42. tailAnimation.keyPath = @"strokeEnd";
  43. tailAnimation.duration = 1.f;
  44. tailAnimation.fromValue = @(0.f);
  45. tailAnimation.toValue = @(1.f);
  46. tailAnimation.timingFunction = [CAMediaTimingFunction functionWithName:@"easeOut"];
  47. tailAnimation.delegate = self;
  48. [self.animLayer addAnimation:tailAnimation forKey:nil];
  49. }
  50. - (void)layoutSubviews{
  51. [super layoutSubviews];
  52. self.titleLabel.bounds = CGRectMake(0, 0, CGRectGetWidth(self.bounds), 30);
  53. self.titleLabel.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMaxY(self.bounds) - 30);
  54. self.animLayer.bounds = self.bounds;
  55. self.animLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
  56. [self configPath];
  57. }
  58. - (void)configPath{
  59. CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), 45);
  60. CGFloat radius = 30;
  61. CGFloat startAngle = (CGFloat)(M_PI);
  62. CGFloat endAngle = (CGFloat)(3*M_PI + M_PI_4);
  63. UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
  64. CGPoint point1 = CGPointMake(CGRectGetMidX(self.bounds) - 17, 48);
  65. CGPoint point2 = CGPointMake(CGRectGetMidX(self.bounds) - 5, 60);
  66. CGPoint point3 = CGPointMake(CGRectGetMidX(self.bounds) + 20, 37);
  67. [path moveToPoint:point1];
  68. [path addLineToPoint:point2];
  69. [path addLineToPoint:point3];
  70. self.animLayer.path = path.CGPath;
  71. }
  72. #pragma mark - animation delegate
  73. - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
  74. WeakSelf(weakSelf);
  75. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  76. if (weakSelf.finish) weakSelf.finish(flag);
  77. [weakSelf removeFromSuperview];
  78. });
  79. }
  80. #pragma mark - property
  81. - (CAShapeLayer *)animLayer{
  82. if (_animLayer == nil) {
  83. _animLayer = [[CAShapeLayer alloc]init];
  84. _animLayer.strokeColor = [UIColorFromRGB(0x00c8a9) CGColor];
  85. _animLayer.fillColor = [UIColor clearColor].CGColor;
  86. _animLayer.lineWidth = 2.5f;
  87. _animLayer.anchorPoint = CGPointMake(0.5, 0.5);
  88. _animLayer.backgroundColor = [UIColor whiteColor].CGColor;
  89. }
  90. return _animLayer;
  91. }
  92. - (UILabel *)titleLabel{
  93. if (_titleLabel == nil) {
  94. _titleLabel = [[UILabel alloc]init];
  95. _titleLabel.text = @"支付成功";
  96. _titleLabel.textColor = UIColorFromRGB(0x333333);
  97. _titleLabel.font = [UIFont systemFontOfSize:15];
  98. _titleLabel.textAlignment = NSTextAlignmentCenter;
  99. }
  100. return _titleLabel;
  101. }
  102. @end