// // sample3View.m // sample3 // // Created by ?? ?? on 05/06/18. // Copyright (c) 2005, __MyCompanyName__. All rights reserved. // #import "sample3View.h" @implementation sample3View - (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview { self = [super initWithFrame:frame isPreview:isPreview]; if (self) { [self setAnimationTimeInterval:2/30.0]; } return self; } - (void)startAnimation { [super startAnimation]; } - (void)stopAnimation { [super stopAnimation]; } - (void)drawRect:(NSRect)rect { [super drawRect:rect]; } - (NSPoint)rand_point//ランダムなポイントを返す { int width = [self frame].size.width; int height = [self frame].size.height; NSPoint point = NSMakePoint(SSRandomFloatBetween(0.0, width),SSRandomFloatBetween(0.0, height));//ランダム座標 return point; } - (NSColor*)rand_color//ランダムなカラーを作成 { float red = SSRandomFloatBetween(0.0, 1.0); float blue = SSRandomFloatBetween(0.0, 1.0); float green = SSRandomFloatBetween(0.0, 1.0); float alpha = SSRandomFloatBetween(0.0, 1.0); NSColor* r_color = [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:alpha];//先ほどのパラメタを使用して色を作る return r_color; } - (NSBezierPath*)makePoly:(NSPoint)pt polynum:(int)num angle:(int)ang r:(float)r { int i; if(num==0){ num=SSRandomIntBetween(3, 8); } if(r==0.0){ r=SSRandomFloatBetween(10, 64); } NSBezierPath* bezierPath = [NSBezierPath bezierPath]; ang+=90; for(i = 0; i <= num; i++) { NSPoint tmpPoint = pt; tmpPoint.x += r * cos(2 * M_PI * (i / (float)num)-(M_PI_2/90.0)*ang);//floatで割らないとずっと0になってしまう。 tmpPoint.y += r * sin(2 * M_PI * (i / (float)num)-(M_PI_2/90.0)*ang);//0以下の桁が四捨五入される、切り捨てかも。 if(i == 0) { [bezierPath moveToPoint:tmpPoint]; } else { [bezierPath lineToPoint:tmpPoint]; } } [bezierPath closePath];//始点と終点を繋ぐ。 return bezierPath; } - (void)animateOneFrame { NSPoint point = NSMakePoint(SSRandomFloatBetween(0.0, [self frame].size.width), SSRandomFloatBetween(0.0, [self frame].size.height));//描く座標を決める //NSPoint tpt = NSMakePoint(50, 50); int a = SSRandomFloatBetween(0.0, 100); int b = SSRandomFloatBetween(0.0, 100); NSBezierPath *circle = [NSBezierPath bezierPathWithOvalInRect:NSMakeRect(point.y, point.x, a, b)];//円を描く NSBezierPath *bpath = [NSBezierPath bezierPathWithRect:NSMakeRect(point.x, point.y, 30, 30)];//四角を描く NSBezierPath *circle2 = [NSBezierPath bezierPathWithOvalInRect:NSMakeRect(point.x+point.y, point.x, 30, 30)];//円を描く NSBezierPath *bpath2 = [NSBezierPath bezierPathWithRect:NSMakeRect(point.y, point.y+point.x, 30, 30)];//四角を描く //フェードアウトさせる NSColor* fade_c = [NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.0 alpha:0.05]; NSRect fade_s = NSMakeRect(0, 0, [self frame].size.width, [self frame].size.height); [[self rand_color] set]; [circle2 fill]; [bpath2 fill]; [circle stroke]; [bpath stroke];//描く [ [self makePoly: [self rand_point] polynum:0 angle:0 r:0] stroke]; [fade_c set]; [NSBezierPath fillRect:fade_s]; return; } - (BOOL)hasConfigureSheet { return NO; } - (NSWindow*)configureSheet { return nil; } @end