HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在UIImageView IOS中绘制形状大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在UI ImageView中使用特定图像绘制一些圆圈.这就是我想要做的事情:
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充当它的遮罩.因此,例如,如果我使用动画将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,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:.

大佬总结

以上是大佬教程为你收集整理的在UIImageView IOS中绘制形状全部内容,希望文章能够帮你解决在UIImageView IOS中绘制形状所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。