UIView
放射状のグラデーションを作成したいのですが、どうすればよいのでしょうか?
24581 次
5 に答える
18
Swift 3 - @IBDesignable
私はカーリスとアレクサンダーの答えに取り組みました。できるだけシンプルにすることを目指しました。色空間と位置 ( ) を削除しnil
て、グラデーションがデフォルトを使用するようにします。
使い方
ステップ1
ファイルを作成し、次のコードを追加します: import UIKit
@IBDesignable
class RadialGradientView: UIView {
@IBInspectable var InsideColor: UIColor = UIColor.clear
@IBInspectable var OutsideColor: UIColor = UIColor.clear
override func draw(_ rect: CGRect) {
let colors = [InsideColor.cgColor, OutsideColor.cgColor] as CFArray
let endRadius = min(frame.width, frame.height) / 2
let center = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 2)
let gradient = CGGradient(colorsSpace: nil, colors: colors, locations: nil)
UIGraphicsGetCurrentContext()!.drawRadialGradient(gradient!, startCenter: center, startRadius: 0.0, endCenter: center, endRadius: endRadius, options: CGGradientDrawingOptions.drawsBeforeStartLocation)
}
}
ステップ2
Storyboard で、RadialGradientView
Identity Inspector で UIView を上記のように設定します。
ステップ 3
属性インスペクターでグラデーションの内側の色と外側の色を設定し、ストーリーボードで変更を確認します。
(注: ストーリーボードの UIView を十分に大きくして、全体を埋めるようにしました。
于 2017-01-09T19:47:09.247 に答える
16
最初に UIView をサブクラス化します。
@implementation UIRadialView
- (void)drawRect:(CGRect)rect
{
// Setup view
CGFloat colorComponents[] = {0.0, 0.0, 0.0, 1.0, // First color: R, G, B, ALPHA (currently opaque black)
0.0, 0.0, 0.0, 0.0}; // Second color: R, G, B, ALPHA (currently transparent black)
CGFloat locations[] = {0, 1}; // {0, 1) -> from center to outer edges, {1, 0} -> from outer edges to center
CGFloat radius = MIN((self.bounds.size.height / 2), (self.bounds.size.width / 2));
CGPoint center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
// Prepare a context and create a color space
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Create gradient object from our color space, color components and locations
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colorComponents, locations, 2);
// Draw a gradient
CGContextDrawRadialGradient(context, gradient, center, 0.0, center, radius, 0);
CGContextRestoreGState(context);
// Release objects
CGColorSpaceRelease(colorSpace);
CGGradientRelease(gradient);
}
@end
そして、それをビューに追加します:
UIRadialView *radialView = [[UIRadialView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
radialView.backgroundColor = [UIColor redColor];
[self.view addSubview:radialView];
結果:
于 2016-02-19T11:32:43.373 に答える
6
Swift 3 での Karlis の回答は次のとおりです。
override func draw(_ rect: CGRect) {
// Setup view
let colors = [UIColor.white.cgColor, UIColor.black.cgColor] as CFArray
let locations = [ 0.0, 1.0 ] as [CGFloat]
let radius = min((self.bounds.size.height / 2), (self.bounds.size.width / 2))
let center = CGPoint.init(x: self.bounds.size.width / 2, y: self.bounds.size.height / 2)
// Prepare a context and create a color space
let context = UIGraphicsGetCurrentContext()
context!.saveGState()
let colorSpace = CGColorSpaceCreateDeviceRGB()
// Create gradient object from our color space, color components and locations
let gradient = CGGradient.init(colorsSpace: colorSpace, colors: colors, locations: locations)
// Draw a gradient
context!.drawRadialGradient(gradient!, startCenter: center, startRadius: 0.0, endCenter: center, endRadius: radius, options: CGGradientDrawingOptions(rawValue: 0))
context?.restoreGState()
}
于 2016-10-26T22:02:23.897 に答える