QuartzFunView.m
上传用户:jjjjag8
上传日期:2017-04-17
资源大小:1443k
文件大小:4k
源码类别:

iPhone

开发平台:

MultiPlatform

  1. //
  2. //  QuartzFunView.m
  3. //  QuartzFun
  4. //
  5. //  Created by Jeff LaMarche on 7/31/08.
  6. //  Copyright 2008 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "QuartzFunView.h"
  9. #import "UIColor-Random.h"
  10. @implementation QuartzFunView
  11. @synthesize firstTouch;
  12. @synthesize lastTouch;
  13. @synthesize currentColor;
  14. @synthesize shapeType;
  15. @synthesize drawImage;
  16. @synthesize redrawRect;
  17. @dynamic currentRect;
  18. @dynamic redrawRect;
  19. @synthesize useRandomColor;
  20. // This version is a little different that the code in the book. This
  21. // version is more efficient, but the change was made too late to get
  22. // it into the book. In the book, we loaded a UIImage every time we 
  23. // needed to draw it, which is inefficient. In this version, we use 
  24. // an instance variable to store the UIImage while the program is
  25. // running, and use that image to draw
  26. - (id)initWithCoder:(NSCoder*)coder
  27. {
  28. if((self = [super initWithCoder:coder])) {
  29. self.currentColor = [UIColor redColor];
  30. self.useRandomColor = NO;
  31. if (drawImage == nil)
  32. self.drawImage = [UIImage imageNamed:@"iphone.png"];
  33. }
  34. return self;
  35. }
  36. - (CGRect)currentRect {
  37. return CGRectMake (
  38. (firstTouch.x > lastTouch.x) ? lastTouch.x : firstTouch.x,
  39. (firstTouch.y > lastTouch.y) ? lastTouch.y : firstTouch.y,
  40. fabsf(firstTouch.x - lastTouch.x),
  41. fabsf(firstTouch.y - lastTouch.y));
  42. }
  43. - (void)drawRect:(CGRect)rect {
  44. CGContextRef context = UIGraphicsGetCurrentContext();
  45. CGContextSetLineWidth(context, 2.0);
  46. CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
  47. CGContextSetFillColorWithColor(context, currentColor.CGColor);
  48. switch (shapeType) {
  49. case kLineShape:
  50. CGContextMoveToPoint(context, firstTouch.x, firstTouch.y);
  51. CGContextAddLineToPoint(context, lastTouch.x, lastTouch.y);
  52. CGContextStrokePath(context);
  53. break;
  54. case kRectShape:
  55. CGContextAddRect(context, self.currentRect);
  56. CGContextDrawPath(context, kCGPathFillStroke);
  57. break;
  58. case kEllipseShape:
  59. CGContextAddEllipseInRect(context, self.currentRect);
  60. CGContextDrawPath(context, kCGPathFillStroke);
  61. break;
  62. case kImageShape: {
  63. CGFloat horizontalOffset = drawImage.size.width / 2;
  64. CGFloat verticalOffset = drawImage.size.height / 2;
  65. CGPoint drawPoint = CGPointMake(lastTouch.x - horizontalOffset,
  66. lastTouch.y - verticalOffset);
  67. [drawImage drawAtPoint:drawPoint];
  68. break;
  69. }
  70. default:
  71. break;
  72. }
  73. }
  74. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  75. {
  76. if (useRandomColor)
  77. self.currentColor = [UIColor randomColor];
  78. UITouch *touch = [touches anyObject];
  79. firstTouch = [touch locationInView:self];
  80. lastTouch = [touch locationInView:self];
  81. if (shapeType == kImageShape) {
  82. CGFloat horizontalOffset = drawImage.size.width / 2;
  83. CGFloat verticalOffset = drawImage.size.height / 2;
  84. redrawRect = CGRectMake(firstTouch.x - horizontalOffset, firstTouch.y - verticalOffset, drawImage.size.width, drawImage.size.height);
  85. }
  86. else
  87. redrawRect = CGRectMake(firstTouch.x, firstTouch.y, 0, 0);
  88. [self setNeedsDisplay];
  89. }
  90. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  91. {
  92. UITouch *touch = [touches anyObject];
  93. lastTouch = [touch locationInView:self];
  94. if (shapeType == kImageShape) {
  95. CGFloat horizontalOffset = drawImage.size.width / 2;
  96. CGFloat verticalOffset = drawImage.size.height / 2;
  97. redrawRect = CGRectUnion(redrawRect, CGRectMake(lastTouch.x - horizontalOffset, lastTouch.y - verticalOffset, drawImage.size.width, drawImage.size.height));
  98. }
  99. else
  100. redrawRect = CGRectUnion(redrawRect, self.currentRect);
  101. [self setNeedsDisplayInRect:redrawRect];
  102. }
  103. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  104. {
  105. UITouch *touch = [touches anyObject];
  106. lastTouch = [touch locationInView:self];
  107. if (shapeType == kImageShape) {
  108. CGFloat horizontalOffset = drawImage.size.width / 2;
  109. CGFloat verticalOffset = drawImage.size.height / 2;
  110. redrawRect = CGRectUnion(redrawRect, CGRectMake(lastTouch.x - horizontalOffset, lastTouch.y - verticalOffset, drawImage.size.width, drawImage.size.height));
  111. }
  112. redrawRect = CGRectUnion(redrawRect, self.currentRect);
  113. [self setNeedsDisplayInRect:redrawRect];
  114. }
  115. - (void)dealloc {
  116. [currentColor release];
  117. [drawImage release];
  118. [super dealloc];
  119. }
  120. @end