SNKNinePatchButton.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // SNKNinePatchButton.m
  3. // TestImageResible
  4. //
  5. // Created by tu jinqiu on 2018/11/7.
  6. // Copyright © 2018年 tu jinqiu. All rights reserved.
  7. //
  8. #import "SNKNinePatchButton.h"
  9. #import "SNKNinePatchImageCache.h"
  10. #import "UIButton+YYWebImage.h"
  11. #import "YYDiskCache.h"
  12. @interface SNKNinePatchButton ()
  13. @property(nonatomic, weak) NSLayoutConstraint *topConstraint;
  14. @property(nonatomic, weak) NSLayoutConstraint *leftConstraint;
  15. @property(nonatomic, weak) NSLayoutConstraint *bottomConstraint;
  16. @property(nonatomic, weak) NSLayoutConstraint *rightConstraint;
  17. @end
  18. @implementation SNKNinePatchButton
  19. - (instancetype)initWithFrame:(CGRect)frame
  20. {
  21. if (self = [super initWithFrame:frame]) {
  22. _imageScale = 1;
  23. }
  24. return self;
  25. }
  26. - (instancetype)initWithCoder:(NSCoder *)aDecoder
  27. {
  28. if (self = [super initWithCoder:aDecoder]) {
  29. _imageScale = 1;
  30. }
  31. return self;
  32. }
  33. - (void)addConstraintsWithPaddingView:(UIView *)paddingView
  34. {
  35. NSAssert(self.superview && paddingView.superview && self.superview == paddingView.superview, @"paddingView 和 SNKNinePatchButton 应该公有一个父 view");
  36. self.translatesAutoresizingMaskIntoConstraints = NO;
  37. [self setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
  38. [self setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisVertical];
  39. [self setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
  40. [self setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisVertical];
  41. NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:paddingView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
  42. self.topConstraint = topConstraint;
  43. NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:paddingView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
  44. self.leftConstraint = leftConstraint;
  45. NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:paddingView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
  46. self.bottomConstraint = bottomConstraint;
  47. NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:paddingView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
  48. self.rightConstraint = rightConstraint;
  49. [self.superview addConstraints:@[topConstraint, leftConstraint, bottomConstraint, rightConstraint]];
  50. if (self.ninePatchImage) {
  51. [self p_updateConstraints];
  52. }
  53. }
  54. - (void)setBackgroundImageWithUrlStr:(NSString *)urlStr
  55. {
  56. SNKNinePatchImage *ninePatchImage = [SNKNinePatchImageCache ninePatchImageNamed:urlStr];
  57. if (ninePatchImage) {
  58. self.ninePatchImage = ninePatchImage;
  59. return;
  60. }
  61. __weak SNKNinePatchButton *weakSelf = self;
  62. [self setBackgroundImageWithURL:[NSURL URLWithString:urlStr]
  63. forState:UIControlStateNormal
  64. placeholder:nil
  65. options:kNilOptions
  66. progress:nil
  67. transform:^UIImage * _Nullable(UIImage * _Nonnull image, NSURL * _Nonnull url) {
  68. [weakSelf p_checkSetImageForUrl:url urlStr:urlStr];
  69. return image;
  70. }
  71. completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
  72. [weakSelf p_checkSetImageForUrl:url urlStr:urlStr];
  73. }];
  74. }
  75. - (void)setNinePatchImage:(SNKNinePatchImage *)ninePatchImage
  76. {
  77. _ninePatchImage = ninePatchImage;
  78. [self setBackgroundImage:ninePatchImage.image forState:UIControlStateNormal];
  79. if (ninePatchImage) {
  80. [self p_updateConstraints];
  81. }
  82. }
  83. #pragma mark - private
  84. - (void)p_updateConstraints
  85. {
  86. UIEdgeInsets padding = self.ninePatchImage.paddingCap.padding;
  87. self.topConstraint.constant = padding.top;
  88. self.leftConstraint.constant = padding.left;
  89. self.bottomConstraint.constant = -padding.bottom;
  90. self.rightConstraint.constant = -padding.right;
  91. }
  92. - (void)p_checkSetImageForUrl:(NSURL *)url urlStr:(NSString *)urlStr
  93. {
  94. YYWebImageManager *manager = [YYWebImageManager sharedManager];
  95. NSString *key = [manager cacheKeyForURL:url];
  96. NSData *data = (NSData *)[[YYWebImageManager sharedManager].cache getImageDataForKey:key];
  97. if (data) {
  98. [self p_checkSetImageWithData:data urlStr:urlStr];
  99. } else {
  100. __weak SNKNinePatchButton *weakSelf = self;
  101. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  102. NSData *data = (NSData *)[[YYWebImageManager sharedManager].cache getImageDataForKey:key];
  103. [weakSelf p_checkSetImageWithData:data urlStr:urlStr];
  104. });
  105. }
  106. }
  107. - (void)p_checkSetImageWithData:(NSData *)data urlStr:(NSString *)urlStr
  108. {
  109. SNKNinePatchImage *ninePatchImage = [SNKNinePatchImage ninePatchImageWithImageData:data scale:self.imageScale];
  110. [SNKNinePatchImageCache setNinePatchImage:ninePatchImage forName:urlStr];
  111. self.ninePatchImage = ninePatchImage;
  112. }
  113. @end