PDFDrawBackgroundView.m 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // CPDFDrawBackgroundView.m
  3. // PDFViewer
  4. //
  5. // Created by kdanmobile_2 on 2023/1/3.
  6. //
  7. #import "PDFDrawBackgroundView.h"
  8. @implementation PDFDrawBackgroundView
  9. - (instancetype)initWithFrame:(CGRect)frame {
  10. self = [super initWithFrame:frame];
  11. if (self) {
  12. self.backgroundColor = [UIColor whiteColor];
  13. }
  14. return self;
  15. }
  16. - (void)drawRect:(CGRect)rect {
  17. [super drawRect:rect];
  18. CGContextRef context = UIGraphicsGetCurrentContext();
  19. [self showDraw:context];
  20. }
  21. - (void)showDraw:(CGContextRef)context {
  22. CGContextSetFillColorWithColor(context, self.dataModel.backgroundColor.CGColor);
  23. CGContextSetAlpha(context, self.dataModel.backgroundOpacity);
  24. CGFloat ctm = (self.dataModel.backgroundRotation * M_PI) / 180;
  25. CGFloat centerWidth = self.bounds.size.width / 2;
  26. CGFloat centerHeigth = self.bounds.size.height / 2;
  27. CGContextTranslateCTM(context, self.dataModel.horizontalSpacing, - self.dataModel.verticalSpacing);
  28. CGContextTranslateCTM(context, centerWidth, centerHeigth);
  29. CGContextRotateCTM(context, ctm);
  30. CGContextScaleCTM(context, self.dataModel.backgroudScale, self.dataModel.backgroudScale);
  31. if (_dataModel.image) {
  32. [_dataModel.image drawInRect:CGRectMake(- centerWidth, - centerHeigth, self.frame.size.width,self.frame.size.height) blendMode:kCGBlendModeNormal alpha:self.dataModel.backgroundOpacity];
  33. } else {
  34. CGContextFillRect(context, CGRectMake( - centerWidth, - centerHeigth, self.bounds.size.width, self.bounds.size.height));
  35. }
  36. CGContextRotateCTM(context, -ctm);
  37. CGContextTranslateCTM(context, - centerWidth, - centerHeigth);
  38. }
  39. @end