SingleGuideView.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // SingleGuideView.m
  3. // DDTG
  4. //
  5. // Created by 翟玉磊 on 2017/7/10.
  6. // Copyright © 2017年 翟玉磊. All rights reserved.
  7. //
  8. #import "SingleGuideView.h"
  9. @interface SingleGuideView ()
  10. @property (strong, nonatomic) NSArray *imageArray;
  11. @property (nonatomic, strong) UIImageView *imageView;
  12. @property (nonatomic, strong) UIButton *touchButton;
  13. @property (nonatomic, copy) SingleGuideViewBlock callback;
  14. @end
  15. @implementation SingleGuideView
  16. static SingleGuideView *singleGuideView = nil;
  17. static dispatch_once_t onceToken;
  18. + (instancetype)sharedView {
  19. dispatch_once(&onceToken, ^{
  20. CGRect frame = [UIScreen mainScreen].bounds;
  21. singleGuideView = [[SingleGuideView alloc] initWithFrame:frame];
  22. [[UIApplication sharedApplication].delegate.window addSubview:singleGuideView];
  23. });
  24. return singleGuideView;
  25. }
  26. - (instancetype)initWithFrame:(CGRect)frame {
  27. if (self = [super initWithFrame:frame]) {
  28. self.backgroundColor = [UIColor clearColor];
  29. [self buildView];
  30. }
  31. return self;
  32. }
  33. - (void)buildView {
  34. [self addSubview:self.imageView];
  35. [self addSubview:self.touchButton];
  36. }
  37. - (void)setupImage:(NSArray *)images block:(SingleGuideViewBlock)callback {
  38. if (images.count == 0) {
  39. [self dismiss:callback];
  40. return;
  41. }
  42. self.callback = callback;
  43. _imageArray = [NSArray arrayWithArray:images];
  44. [self show];
  45. self.imageView.image = _imageArray[0];
  46. self.touchButton.tag = 0;
  47. }
  48. - (void)touchButtonAction:(UIButton *)sender {
  49. if (sender.tag == self.imageArray.count - 1) {
  50. [self dismiss:self.callback];
  51. } else {
  52. sender.tag += 1;
  53. self.imageView.image = self.imageArray[sender.tag];
  54. }
  55. }
  56. - (void)show {
  57. self.alpha = 1;
  58. self.hidden = NO;
  59. }
  60. - (void)dismiss:(SingleGuideViewBlock)callback {
  61. [UIView animateWithDuration:.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
  62. self.alpha = 0;
  63. } completion:^(BOOL finished) {
  64. self.hidden = YES;
  65. if (callback) {
  66. callback();
  67. }
  68. }];
  69. }
  70. + (void)destroyInstance {
  71. onceToken = 0;
  72. singleGuideView = nil;
  73. }
  74. #pragma mark - Get
  75. - (UIImageView *)imageView {
  76. if (_imageView == nil) {
  77. _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
  78. _imageView.backgroundColor = [UIColor clearColor];
  79. }
  80. return _imageView;
  81. }
  82. - (UIButton *)touchButton {
  83. if (_touchButton == nil) {
  84. _touchButton = [UIButton buttonWithType:UIButtonTypeCustom];
  85. _touchButton.frame = self.bounds;
  86. _touchButton.backgroundColor = [UIColor clearColor];
  87. [_touchButton addTarget:self action:@selector(touchButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  88. }
  89. return _touchButton;
  90. }
  91. @end