猎豆优选

ZLClipItem.m 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // ZLClipItem.m
  3. // ZLPhotoBrowser
  4. //
  5. // Created by long on 2018/5/6.
  6. // Copyright © 2018年 long. All rights reserved.
  7. //
  8. #import "ZLClipItem.h"
  9. @implementation ZLClippingCircle
  10. - (void)drawRect:(CGRect)rect
  11. {
  12. CGContextRef context = UIGraphicsGetCurrentContext();
  13. CGRect rct = self.bounds;
  14. rct.origin.x = rct.size.width/2-rct.size.width/6;
  15. rct.origin.y = rct.size.height/2-rct.size.height/6;
  16. rct.size.width /= 3;
  17. rct.size.height /= 3;
  18. CGContextSetFillColorWithColor(context, self.bgColor.CGColor);
  19. CGContextFillEllipseInRect(context, rct);
  20. }
  21. @end
  22. //!!!!: ZLGridLayar
  23. @implementation ZLGridLayar
  24. + (BOOL)needsDisplayForKey:(NSString*)key
  25. {
  26. if ([key isEqualToString:@"clippingRect"]) {
  27. return YES;
  28. }
  29. return [super needsDisplayForKey:key];
  30. }
  31. - (id)initWithLayer:(id)layer
  32. {
  33. self = [super initWithLayer:layer];
  34. if(self && [layer isKindOfClass:[ZLGridLayar class]]){
  35. self.bgColor = ((ZLGridLayar *)layer).bgColor;
  36. self.gridColor = ((ZLGridLayar *)layer).gridColor;
  37. self.clippingRect = ((ZLGridLayar *)layer).clippingRect;
  38. }
  39. return self;
  40. }
  41. - (void)drawInContext:(CGContextRef)context
  42. {
  43. CGRect rct = self.bounds;
  44. CGContextSetFillColorWithColor(context, self.bgColor.CGColor);
  45. CGContextFillRect(context, rct);
  46. CGContextClearRect(context, _clippingRect);
  47. CGContextSetStrokeColorWithColor(context, self.gridColor.CGColor);
  48. CGContextSetLineWidth(context, 1);
  49. rct = self.clippingRect;
  50. CGContextBeginPath(context);
  51. CGFloat dW = 0;
  52. for(int i=0;i<4;++i){
  53. CGContextMoveToPoint(context, rct.origin.x+dW, rct.origin.y);
  54. CGContextAddLineToPoint(context, rct.origin.x+dW, rct.origin.y+rct.size.height);
  55. dW += _clippingRect.size.width/3;
  56. }
  57. dW = 0;
  58. for(int i=0;i<4;++i){
  59. CGContextMoveToPoint(context, rct.origin.x, rct.origin.y+dW);
  60. CGContextAddLineToPoint(context, rct.origin.x+rct.size.width, rct.origin.y+dW);
  61. dW += rct.size.height/3;
  62. }
  63. CGContextStrokePath(context);
  64. }
  65. @end
  66. //!!!!: ZLClipRatio
  67. @implementation ZLClipRatio
  68. {
  69. CGFloat _longSide;
  70. CGFloat _shortSide;
  71. }
  72. - (id)initWithValue1:(CGFloat)value1 value2:(CGFloat)value2
  73. {
  74. self = [super init];
  75. if(self){
  76. _longSide = MAX(fabs(value1), fabs(value2));
  77. _shortSide = MIN(fabs(value1), fabs(value2));
  78. }
  79. return self;
  80. }
  81. - (NSString*)description
  82. {
  83. NSString *format = (self.titleFormat) ? self.titleFormat : @"%g : %g";
  84. if(self.isLandscape){
  85. return [NSString stringWithFormat:format, _longSide, _shortSide];
  86. }
  87. return [NSString stringWithFormat:format, _shortSide, _longSide];
  88. }
  89. - (CGFloat)ratio
  90. {
  91. if(_longSide==0 || _shortSide==0){
  92. return 0;
  93. }
  94. if(self.isLandscape){
  95. return _shortSide / (CGFloat)_longSide;
  96. }
  97. return _longSide / (CGFloat)_shortSide;
  98. }
  99. @end
  100. //!!!!: ZLClipItem
  101. @implementation ZLClipItem
  102. - (instancetype)initWithFrame:(CGRect)frame
  103. {
  104. return [self initWithFrame:CGRectZero image:nil target:nil action:nil];
  105. }
  106. - (instancetype)initWithCoder:(NSCoder *)coder
  107. {
  108. return [self initWithFrame:CGRectZero image:nil target:nil action:nil];
  109. }
  110. - (instancetype)initWithFrame:(CGRect)frame image:(UIImage *)image target:(id)target action:(SEL)action
  111. {
  112. self = [super initWithFrame:frame];
  113. if(self){
  114. UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];
  115. [self addGestureRecognizer:gesture];
  116. CGFloat W = frame.size.width;
  117. _iconView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, W-20, W-20)];
  118. _iconView.clipsToBounds = YES;
  119. _iconView.image = image;
  120. _iconView.layer.cornerRadius = 5;
  121. _iconView.contentMode = UIViewContentModeScaleAspectFill;
  122. [self addSubview:_iconView];
  123. _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_iconView.frame) + 5, W, 15)];
  124. _titleLabel.backgroundColor = [UIColor clearColor];
  125. _titleLabel.font = [UIFont systemFontOfSize:10];
  126. _titleLabel.textColor = [UIColor whiteColor];
  127. _titleLabel.textAlignment = NSTextAlignmentCenter;
  128. [self addSubview:_titleLabel];
  129. }
  130. return self;
  131. }
  132. - (void)setRatio:(ZLClipRatio *)ratio
  133. {
  134. if(ratio != _ratio){
  135. _ratio = ratio;
  136. }
  137. }
  138. - (void)refreshViews
  139. {
  140. _titleLabel.text = [_ratio description];
  141. CGPoint center = _iconView.center;
  142. CGFloat W, H;
  143. if (_ratio.ratio != 0) {
  144. if(_ratio.isLandscape) {
  145. W = 50;
  146. H = 50*_ratio.ratio;
  147. } else {
  148. W = 50/_ratio.ratio;
  149. H = 50;
  150. }
  151. } else {
  152. CGFloat maxW = MAX(_iconView.image.size.width, _iconView.image.size.height);
  153. W = 50 * _iconView.image.size.width / maxW;
  154. H = 50 * _iconView.image.size.height / maxW;
  155. }
  156. _iconView.frame = CGRectMake(center.x-W/2, center.y-H/2, W, H);
  157. }
  158. - (void)changeOrientation
  159. {
  160. self.ratio.isLandscape = !self.ratio.isLandscape;
  161. [UIView animateWithDuration:0.2 animations:^{
  162. [self refreshViews];
  163. }];
  164. }
  165. @end