を使用して角度グラデーションを描画しようとしていCaGradientLayer
ます。startPoint
と を使用して角度を定義できることを知っていendPoint
ます。0、90、180、360 などのいくつかの標準的な角度についてこれらの点を計算できますが、任意の角度についてこれらの点を定式化したいと考えています。いくつかの三角法を使用して計算しようとしましたが、成功しませんでした。任意の角度に対してこれらの点を計算する方法について誰か教えてもらえますか?
11054 次
3 に答える
8
スライダー (または何か) からの入力に基づいて、2 色のグラデーションを 360 度回転できるビューを作成するメソッドを次に示します。入力スライダー値 (以下の「x」変数) は 0.0 から 1.0 の間です。
0.0 の場合、グラデーションは水平 (色 A が上、色 B が下) で、値 1.0 まで 360 度回転します (値 0.0 と同じ - または完全な回転)。
たとえば、x = 0.25 の場合、色 A が左で、色 B が右です。0.5 ではカラー A が下、カラー B が上、0.75 ではカラー A が右、カラー B が左です。右から左へ反時計回りに回転します。
フレーム、colorA、colorB、および入力値 (0-1) の 4 つの引数を取ります。
-(UIView *)gradientViewWithFrame:(CGRect)frame colourA:(UIColor *)A colourB:(UIColor *)B rotation:(float)x {
//x is between 0 and 1, eg. from a slider, representing 0 - 360 degrees
//colour A starts on top, with colour B below
//rotations move anti-clockwise
//1. create the colour view
UIView * colourView = [UIView new];
colourView.frame = frame;
//2. create the gradient layer
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = colourView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[A CGColor], (id)[B CGColor], nil];
[colourView.layer insertSublayer:gradient atIndex:0];
//3. create coordinates
float a = pow(sinf((2*M_PI*((x+0.75)/2))),2);
float b = pow(sinf((2*M_PI*((x+0.0)/2))),2);
float c = pow(sinf((2*M_PI*((x+0.25)/2))),2);
float d = pow(sinf((2*M_PI*((x+0.5)/2))),2);
//4. set the gradient direction
[gradient setStartPoint:CGPointMake(a, b)];
[gradient setEndPoint:CGPointMake(c, d)];
return colourView;
}
于 2015-03-20T14:16:20.477 に答える