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中绘制形状所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)