gpt4 book ai didi

ios - 在 UIView 中绘制用于选择指示的三角形切口

转载 作者:IT王子 更新时间:2023-10-29 08:17:22 28 4
gpt4 key购买 nike

我正在创建一个 View ,用作我的应用程序的类别选择器。我希望它有一个镂空三角形作为选择指示,如下图所示:

Custom UI design

我不确定如何绘制三角形,使其成为一个切口,露出下面的主视图。下面的主视图很可能有一个自定义的、可能不重复的(我还没有决定)图像作为背景。此外,我希望三角形在选择发生变化时动画到一个新位置,这会使事情进一步复杂化。

我意识到 subview 会使动画更容易,但会使绘图复杂化;直接绘图可能会使动画更难一些。而且我对 Quartz 不太熟悉,所以我不确定如何使用直接绘图路线。

提前致谢!

更新:我查看了 Matt Gallagher 在 drawing shapes with holes 上的帖子,但它并没有真正回答我的问题。有没有办法让我“看到”形状内某条路径下方的内容,并复制它? …然后支持动画化?

更新 2:我通过简单地绘制一条附加路径完成了部分工作。结果如下所示: http://dl.dropbox.com/u/7828009/Category%20Selector.mov

代码:

CGRect cellRect = [self rectForCategoryNumber:(selectedCategoryIndex + 1)];
[[UIColor scrollViewTexturedBackgroundColor] setFill];
CGContextMoveToPoint(currentContext, self.frame.size.width, (cellRect.origin.y + cellRect.size.height * 0.15));
CGContextAddLineToPoint(currentContext, self.frame.size.width, (cellRect.origin.y + cellRect.size.height * 0.65));
CGContextAddLineToPoint(currentContext, self.frame.size.width * 0.8, (cellRect.origin.y + cellRect.size.height * 0.4));
CGContextClosePath(currentContext);
CGContextFillPath(currentContext);
[[UIColor darkGrayColor] setStroke];
CGContextSetLineCap(currentContext, kCGLineCapRound);
CGContextMoveToPoint(currentContext, self.frame.size.width, (cellRect.origin.y + cellRect.size.height * 0.15));
CGContextAddLineToPoint(currentContext, self.frame.size.width * 0.8, (cellRect.origin.y + cellRect.size.height * 0.4));
CGContextSetLineWidth(currentContext, 2.5);
CGContextStrokePath(currentContext);
[[UIColor lightGrayColor] setStroke];
CGContextMoveToPoint(currentContext,self.frame.size.width * 0.8, (cellRect.origin.y + cellRect.size.height * 0.4));
CGContextAddLineToPoint(currentContext, self.frame.size.width, (cellRect.origin.y + cellRect.size.height * 0.65));
CGContextSetLineWidth(currentContext, 1.0);
CGContextStrokePath(currentContext);

显然,在这种情况下,它之所以有效,是因为我在两种情况下都使用了相同的填充颜色;如果可能的话,我想消除这种依赖。另外,我当然想为该三角形的位置设置动画。

更新 3:我尝试制作动画的内容:

static CALayer *previousLayer = nil;
static CGMutablePathRef previousPath = nil;
// … Get context, etc.
CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
shapeLayer.path = shapePath;
[shapeLayer setFillColor:[[UIColor redColor] CGColor]];
[shapeLayer setStrokeColor:[[UIColor blackColor] CGColor]];
[shapeLayer setBounds:self.bounds];
[shapeLayer setAnchorPoint:self.bounds.origin];
[shapeLayer setPosition:self.bounds.origin];
if (previousPath) { // Animate change
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"changePath"];
animation.duration = 0.5;
animation.fromValue = (id)previousPath;
animation.toValue = (id)shapePath;
[shapeLayer addAnimation:animation forKey:@"animatePath"];
previousPath = shapePath;
}

if (previousLayer)
[previousLayer removeFromSuperlayer];
previousLayer = shapeLayer;
[self.layer addSublayer:shapeLayer];

最佳答案

你看过CAShapeLayer了吗? ?您可以为您的选择 Pane 创建一个路径,该路径定义整个轮廓,包括排除您需要屏蔽的每个状态的三角形。然后,如果您想要在图像中显示轮廓,您可以通过设置 lineWidthstrokeColor 属性来描边图层。那应该能够给你你所需要的。 CAShapeLayer 中的 path 属性是可动画的,这意味着您所要做的就是设置 path 属性,它将动画化(假设您的层是 View 的子层而不是根层)。

使用代码更新

这段代码:

- (void)viewDidLoad
{
[super viewDidLoad];

[[self view] setBackgroundColor:[UIColor blueColor]];

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path,NULL,0.0,0.0);
CGPathAddLineToPoint(path, NULL, 160.0f, 0.0f);
CGPathAddLineToPoint(path, NULL, 160.0f, 100.0f);
CGPathAddLineToPoint(path, NULL, 110.0f, 150.0f);
CGPathAddLineToPoint(path, NULL, 160.0f, 200.0f);
CGPathAddLineToPoint(path, NULL, 160.0f, 480.0f);
CGPathAddLineToPoint(path, NULL, 0.0f, 480.0f);
CGPathAddLineToPoint(path, NULL, 0.0f, 0.0f);


CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setPath:path];
[shapeLayer setFillColor:[[UIColor redColor] CGColor]];
[shapeLayer setStrokeColor:[[UIColor blackColor] CGColor]];
[shapeLayer setBounds:CGRectMake(0.0f, 0.0f, 160.0f, 480)];
[shapeLayer setAnchorPoint:CGPointMake(0.0f, 0.0f)];
[shapeLayer setPosition:CGPointMake(0.0f, 0.0f)];
[[[self view] layer] addSublayer:shapeLayer];

CGPathRelease(path);

}

此显示的结果:

enter image description here

您可以在这里下载示例项目:

https://dzwonsemrish7.cloudfront.net/items/180s2a200N2i3b0y1g37/ArrowIndicator.zip

关于ios - 在 UIView 中绘制用于选择指示的三角形切口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7626364/

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