1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| #import "Coslayer.h"
#import <UIKit/UIKit.h> @implementation Coslayer
+ (BOOL)needsDisplayForKey:(NSString *)key{ if ([key isEqualToString:@"offsetAngle"]) { return YES; } return [super needsDisplayForKey:key]; }
- (instancetype)init{ self = [super init]; self.backgroundColor = [UIColor yellowColor].CGColor; self.offsetAngle = 0; return self; }
- (void)setOffsetAngle:(CGFloat)offsetAngle{ _offsetAngle = offsetAngle; [self setNeedsDisplay]; }
- (void)drawInContext:(CGContextRef)ctx{ int widthPixel = (int)CGRectGetWidth(self.bounds); if (widthPixel <1) { return; } CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, NULL, 0, 0); CGPathMoveToPoint(path, NULL, 0, CGRectGetMidY(self.bounds)); for (int i = 0; i < widthPixel; i++) { CGFloat y = 50 + 8*cos(2*(i*M_PI/360.0+self.offsetAngle)); if (i == 0) { CGPathMoveToPoint(path, NULL, i, y); }else{ CGPathAddLineToPoint(path, NULL, i, y); } } CGPathAddLineToPoint(path, NULL, CGRectGetWidth(self.bounds), 0); CGPathAddLineToPoint(path, NULL, 0, 0); CGContextAddPath(ctx, path); CGContextSetRGBFillColor(ctx, 0.8, 0.5, 0.7, 1); CGContextDrawPath(ctx, kCGPathFill); CGPathRelease(path); }
@end
|