SCShapeView.m 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. Copyright 2013 Scott Logic Ltd
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #import "SCShapeView.h"
  14. @interface SCShapeView () {
  15. CAShapeLayer *_outline;
  16. }
  17. @end
  18. @implementation SCShapeView
  19. - (id)initWithFrame:(CGRect)frame
  20. {
  21. self = [super initWithFrame:frame];
  22. if (self) {
  23. // Initialization code
  24. _outline = [CAShapeLayer new];
  25. _outline.strokeColor = [[[UIColor blueColor] colorWithAlphaComponent:0.8] CGColor];
  26. _outline.lineWidth = 2.0;
  27. _outline.fillColor = [[UIColor clearColor] CGColor];
  28. [self.layer addSublayer:_outline];
  29. }
  30. return self;
  31. }
  32. - (void)setCorners:(NSArray *)corners
  33. {
  34. if(corners != _corners) {
  35. _corners = corners;
  36. _outline.path = [[self createPathFromPoints:corners] CGPath];
  37. }
  38. }
  39. - (UIBezierPath *)createPathFromPoints:(NSArray *)points
  40. {
  41. UIBezierPath *path = [UIBezierPath new];
  42. // Start at the first corner
  43. [path moveToPoint:[[points firstObject] CGPointValue]];
  44. // Now draw lines around the corners
  45. for (NSUInteger i = 1; i < [points count]; i++) {
  46. [path addLineToPoint:[points[i] CGPointValue]];
  47. }
  48. // And join it back to the first corner
  49. [path addLineToPoint:[[points firstObject] CGPointValue]];
  50. return path;
  51. }
  52. @end