SGActivePopupsView.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // SGActivePopupsView.m
  3. // SGAnimationExample
  4. //
  5. // Created by apple on 2017/6/13.
  6. // Copyright © 2017年 Sorgle. All rights reserved.
  7. //
  8. // - - - - - - - - - - - - - - 交流QQ:1357127436 - - - - - - - - - - - - - - - //
  9. //
  10. // - - 如在使用中, 遇到什么问题或者有更好建议者, 请于 kingsic@126.com 邮箱联系 - - - - //
  11. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  12. // - - GitHub下载地址 https://github.com/kingsic/SGAnimation.git - — - - - - - //
  13. //
  14. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
  15. #import "SGActivePopupsView.h"
  16. #import "CALayer+SGAnimation.h"
  17. #define activePopupsViewWidth [UIScreen mainScreen].bounds.size.width
  18. #define activePopupsViewHeight [UIScreen mainScreen].bounds.size.height
  19. @interface SGActivePopupsView ()
  20. @property (nonatomic, strong) UIImageView *imageView;
  21. @property (nonatomic, strong) UIButton *cancelBtn;
  22. @end
  23. @implementation SGActivePopupsView
  24. - (instancetype)initWithFrame:(CGRect)frame {
  25. if (self = [super initWithFrame:frame]) {
  26. self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
  27. [self initialization];
  28. [self setupSubviews];
  29. }
  30. return self;
  31. }
  32. - (void)awakeFromNib {
  33. [super awakeFromNib];
  34. [self initialization];
  35. [self setupSubviews];
  36. }
  37. - (void)initialization {
  38. _popupsTimeInterval = 0.3;
  39. }
  40. - (void)setupSubviews {
  41. [self addSubview:self.imageView];
  42. [self addSubview:self.cancelBtn];
  43. }
  44. - (void)layoutSubviews {
  45. [super layoutSubviews];
  46. CGFloat spacing = 10;
  47. CGFloat imageViewW = activePopupsViewWidth * 3 / 4;
  48. CGFloat imageViewH = _imageView.image.size.height;
  49. CGFloat imageViewX = 0.5 * (activePopupsViewWidth - imageViewW);
  50. CGFloat imageViewY = 0.5 * (activePopupsViewHeight - imageViewH);
  51. _imageView.frame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
  52. CGRect tempFrame = _cancelBtn.frame;
  53. tempFrame.origin.x = CGRectGetMaxX(_imageView.frame);
  54. tempFrame.origin.y = imageViewY - 3 * spacing;
  55. _cancelBtn.frame = tempFrame;
  56. [self addAnimationWithImageView];
  57. }
  58. - (void)addAnimationWithImageView {
  59. [_imageView.layer SG_animationWithDuration:self.popupsTimeInterval values:@[@0.1, @1.05, @1]];
  60. [_cancelBtn.layer SG_animationWithDuration:self.popupsTimeInterval values:@[@0.1, @1.05, @1]];
  61. }
  62. - (UIImageView *)imageView {
  63. if (!_imageView) {
  64. _imageView = [[UIImageView alloc] init];
  65. _imageView.userInteractionEnabled = YES;
  66. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didselectImage)];
  67. [_imageView addGestureRecognizer:tap];
  68. }
  69. return _imageView;
  70. }
  71. - (UIButton *)cancelBtn {
  72. if (!_cancelBtn) {
  73. _cancelBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
  74. [_cancelBtn setBackgroundImage:[UIImage imageNamed:@"activePopus_cancelBtn_icon"] forState:(UIControlStateNormal)];
  75. [_cancelBtn setBackgroundImage:[UIImage imageNamed:@"activePopus_cancelBtn_icon"] forState:(UIControlStateHighlighted)];
  76. [_cancelBtn addTarget:self action:@selector(cancelBtn_action) forControlEvents:(UIControlEventTouchUpInside)];
  77. [_cancelBtn sizeToFit];
  78. }
  79. return _cancelBtn;
  80. }
  81. - (void)cancelBtn_action {
  82. [self dismiss];
  83. }
  84. - (void)SG_show {
  85. if (self.superview != nil) return;
  86. self.frame = [UIScreen mainScreen].bounds;
  87. UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
  88. [keyWindow addSubview:self];
  89. }
  90. - (void)dismiss {
  91. [_imageView.layer SG_animationWithDuration:self.popupsTimeInterval values:@[@1, @0.5, @0.1]];
  92. [_cancelBtn.layer SG_animationWithDuration:self.popupsTimeInterval values:@[@1, @0.5, @0.1]];
  93. __weak typeof(self) weakSelf = self;
  94. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.popupsTimeInterval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  95. [weakSelf removeFromSuperview];
  96. });
  97. }
  98. #pragma mark - - - 图片的点击事件
  99. - (void)didselectImage {
  100. [self removeFromSuperview];
  101. if (self.selectedImageBlock) {
  102. self.selectedImageBlock();
  103. }
  104. }
  105. - (void)setImageName:(UIImage *)imageName {
  106. _imageName = imageName;
  107. if (imageName) {
  108. _imageView.image = imageName;
  109. }
  110. }
  111. - (void)setPopupsTimeInterval:(CGFloat)popupsTimeInterval {
  112. _popupsTimeInterval = popupsTimeInterval;
  113. }
  114. @end