cuốn sách gpt4 ai đã làm

Cách vẽ các đường dẫn/hình dạng đầy màu sắc khác nhau

In lại Tác giả: Taklimakan Thời gian cập nhật: 2023-11-01 19:09:51 29 4
mua khóa gpt4 Nike

Tôi cần tô màu các hình trên màn hình bất kỳ màu nào tôi muốn. Tôi hiện đang cố gắng thực hiện việc này bằng UIImage mà tôi muốn đổi màu cho phù hợp với nhu cầu của mình. Theo như tôi biết, cách duy nhất để làm điều này là lấy từng pixel của UIImage, yêu cầu nhiều dòng mã hơn tôi muốn viết để giải quyết vấn đề này. Có cách nào để thay đổi màu của UIImage ngoài những gì tôi đã viết không? Việc vẽ hình theo ngữ cảnh và điền vào nó có dễ dàng hơn không?

TY.

gia hạn:Hình lục giác tự vẽ theo cách chính xác nhưng lại không được điền chính xác chút nào. Đây là mã:

self.Hexagon.lineWidth=10;
[self.Hexagon moveToPoint:CGPointMake(hexWidth/2, 0)];
[self.Hexagon addLineToPoint:CGPointMake(hexWidth, hexHeight/4)];
[self.Hexagon addLineToPoint:CGPointMake(hexWidth, hexHeight*0.75)];
[self.Hexagon addLineToPoint:CGPointMake(hexWidth/2, hexHeight)];
[self.Hexagon addLineToPoint:CGPointMake(0, hexHeight*0.75)];
[self.Hexagon addLineToPoint:CGPointMake(0, hexHeight/4)];
[self.Hexagon addLineToPoint:CGPointMake(hexWidth/2, 0)];

[self.Hexagon closePath];
[[UIColor blackColor] setStroke];
[tự.Nét lục giác];

[[UIColor whiteColor] setFill];
[self.Hexagon điền];

câu trả lời hay nhất

我推荐使用CAShapeLayer. Bạn thậm chí có thể tạo hiệu ứng động cho màu tô. Hiệu suất của bạn sẽ rất tốt và mức sử dụng bộ nhớ của bạn sẽ thấp.

Hàm này tạo CGPath chứa các hình lục giác: (dựa trên câu hỏi OP)

CGPathRef CGPathCreateHexagon( CGFloat hexWidth, CGFloat hexHeight)
{
CGMutablePathRef p = CGPathCreateMutable();
CGPathMoveToPoint(p, NULL, hexWidth * 0,5, 0,0 );
CGPathAddLineToPoint(p, NULL, hexWidth, hexHeight * 0.75 );
CGPathAddLineToPoint(p, NULL, hexWidth, hexHeight * 0.75 );
CGPathAddLineToPoint(p, NULL, hexWidth * 0.5, hexHeight );
CGPathAddLineToPoint(p, NULL, 0,0, hexHeight * 0,75);
CGPathAddLineToPoint(p, NULL, hexWidth * 0,5, 0,0 );
CGPathAddLineToPoint(p, NULL, 0,0, hexHeight * 0,25 );
CGPathAddLineToPoint(p, NULL, hexWidth * 0,5, 0,0 );
CGPathCloseSubpath( p );

trả lại p ;
}

设置 UIView Phương pháp được sử dụng khi:

-(void)addHexagonLayer
{
Lớp CAShapeLayer * = [Lớp CAShapeLayer];
lớp.lineWidth = 10,0;
{
CGPathRef p = CGPathCreateHexagon(100.0, 100.0 );
lớp.path = p;
CGPathRelease( p );
}
layer.fillColor = [[ UIColor redColor ] CGColor ] ;

layer.position = (CGPoint){ CGRectGetMidX( self.view.bounds ), CGRectGetMidY( self.view.bounds ) } ; // định vị lớp hình lục giác của bạn một cách thích hợp.
[ self.view.layer addSublayer:layer ] ; // thêm lớp vào chế độ xem của bạn và đặt vị trí thích hợp

}

biên tậpTôi đã tạo một bản demo hoàn chỉnh chỉ để giải trí:

#import "AppDelegate.h"

CGPathRef tĩnh CGPathCreateHexagon( CGAffineTransform * t, CGFloat w, CGFloat h )
{
CGFloat w_4 = w * 0,25;
CGFloat w_2 = w * 0,5f;
CGFloat h_2 = h * 0,5f;

CGMutablePathRef p = CGPathCreateMutable();
CGPathAddLines( p, t, (CGPoint[]){
{ -w_4, h_2 }, { w_4, h_2 }, { w_2, 0 }, { w_4, -h_2 }, { -w_4, -h_2 }, { -w_2, 0 }
}, 6 ) ;
CGPathCloseSubpath( p );

trả lại p ;
}

@implementation CALayer (SetPositionPixelAligned)

-(CGPoint)pixelAlignedPositionForPoint:(CGPoint)p
{
Kích thước CGSize = self.bounds.size;
CGPoint AnchorPoint = self.anchorPoint;

Kết quả CGPoint = (CGPoint){ roundf( px ) + AnchorPoint.x * fmodf( size.width, 2.0f ), roundf( py ) + AnchorPoint.y * fmodf( size.height, 2.0f ) } ;
return result;
}

@kết thúc

@interface HexagonsView : UIView
@property (không nguyên tử) CGFloat hexHeight;
@property (không nguyên tử) CGFloat hexWidth;
@property (không nguyên tử, chỉ đọc) CGPathRef hexPath;
@kết thúc

@implementation HexagonsView
@synthesize hexagonPath = _hexagonPath;

-(void)dealloc
{
CGPathRelease( _hexagonPath );
_hexagonPath = NULL;
}

-(CGPathRef)hexagonPath
{
nếu ( !_hexagonPath )
{
_hexagonPath = CGPathCreateHexagon( NULL, self.hexWidth, self.hexHeight );
}

trả về _hexagonPath ;
}

-(void)setHexWidth:(CGFloat)w
{
_hexWidth = w;
CGPathRelease( _hexagonPath );
_hexagonPath = NULL;
}

-(void)setHexHeight:(CGFloat)h
{
_hexHeight = h;
CGPathRelease( _hexagonPath );
_hexagonPath = NULL;
}

-(void)layoutSubviews
{
[ siêu bố cụcSubviews ] ;
Giới hạn CGRect = self.bounds;
bounds.size.height += self.hexHeight * 0.5; // đảm bảo chúng tôi bao gồm hàng cuối cùng;

CGPoint p;
px = CGRectGetMinY(giới hạn);

while( py < CGRectGetMaxY( giới hạn ) )
{
px = CGRectGetMinX(giới hạn);
while( px < CGRectGetMaxX( giới hạn ) )
{
{
Lớp CAShapeLayer * = [Lớp CAShapeLayer];
layer.path = self.hexagonPath;
layer.fillColor = [[ UIColor colorWithHue:(CGFloat)arc4random_uniform( 100 ) / 256.0 độ bão hòa:1.0 độ sáng:1.0 alpha:1.0 ] CGColor ] ;
layer.position = [ lớp pixelAlignedPositionForPoint:p ] ;
[ self.layer addSublayer:layer ] ;
}

CGPoint p2 = { px + self.hexWidth * 0,75f, py + self.hexHeight * 0,5f };

if ( p2.y < CGRectGetMaxY( giới hạn )) // không có hình lục giác không cần thiết
{
Lớp CAShapeLayer * = [Lớp CAShapeLayer];
layer.path = self.hexagonPath;
layer.fillColor = [[ UIColor colorWithHue:(CGFloat)arc4random_uniform( 256 ) / 256.0 độ bão hòa:1.0 độ sáng:1.0 alpha:1.0 ] CGColor ] ;
layer.position = [ lớp pixelAlignedPositionForPoint:p2 ] ;
[ self.layer addSublayer:layer ] ;
}

px += self.hexWidth * 1.5;
}
py += self.hexHeight;
}
}

@kết thúc

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] giới hạn]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];

HexagonsView * view = [[ HexagonsView phân bổ ] initWithFrame:self.window.bounds ] ;
view.hexHeight = 100.0f;
view.hexWidth = 100.0f;

[ self.window addSubview:view ];

[self.window makeKeyAndVisible];
return YES;
}

@kết thúc

Về ios - Cách vẽ các đường dẫn/hình được tô bằng các màu khác nhau, chúng tôi đã tìm thấy một câu hỏi tương tự trên Stack Overflow: https://stackoverflow.com/questions/17415730/

29 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress