gpt4 book ai didi

ios - 在 UIImageView IOS 中绘制形状

转载 作者:可可西里 更新时间:2023-11-01 03:42:08 25 4
gpt4 key购买 nike

我正在尝试在具有特定图像的 UIImageView 中绘制一些圆圈。这就是我想要做的:

UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(contextRef, 2.0);
CGContextSetStrokeColorWithColor(contextRef, [color CGColor]);
CGRect circlePoint = (CGRectMake(coordsFinal.x, coordsFinal.y, 50.0, 50.0));

CGContextStrokeEllipseInRect(contextRef, circlePoint);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[photoView addSubview:image];

圆圈绘制得很好,但我希望 PhotoView 充当它的 mask 。因此,例如,如果我使用动画将 UIImageView 移出 UIView,我希望圆圈随之移动。重要的是坐标是相对于整个屏幕的。

最佳答案

改用 Core Animation 的形状层。

CAShapeLayer *circleLayer = [CAShapeLayer layer];
// Give the layer the same bounds as your image view
[circleLayer setBounds:CGRectMake(0.0f, 0.0f, [photoView bounds].size.width,
[photoView bounds].size.height)];
// Position the circle anywhere you like, but this will center it
// In the parent layer, which will be your image view's root layer
[circleLayer setPosition:CGPointMake([photoView bounds].size.width/2.0f,
[photoView bounds].size.height/2.0f)];
// Create a circle path.
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:
CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)];
// Set the path on the layer
[circleLayer setPath:[path CGPath]];
// Set the stroke color
[circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
// Set the stroke line width
[circleLayer setLineWidth:2.0f];

// Add the sublayer to the image view's layer tree
[[photoView layer] addSublayer:circleLayer];

现在,如果您为包含该层的 UIImageView 设置动画,该层将随之移动,因为它是一个子层。现在无需覆盖 drawRect:

关于ios - 在 UIImageView IOS 中绘制形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15096063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com