Ios SWIFT:如何在中间创建一个透明圈的UIVIEW?
对于目标c有一些答案,但没有找到任何关于swift的答案 我想创建一个中间有透明圆圈的暗视图,这样用户可以看到子视图并与之交互。如何使用swift实现这一点 更确切地说,我正在寻找像whatsapp配置文件图片实现这样的结果。中间有一个透明的圆圈,用户可以看到图片和滚动。Ios SWIFT:如何在中间创建一个透明圈的UIVIEW?,ios,swift,uiview,drawrect,alpha-transparency,Ios,Swift,Uiview,Drawrect,Alpha Transparency,对于目标c有一些答案,但没有找到任何关于swift的答案 我想创建一个中间有透明圆圈的暗视图,这样用户可以看到子视图并与之交互。如何使用swift实现这一点 更确切地说,我正在寻找像whatsapp配置文件图片实现这样的结果。中间有一个透明的圆圈,用户可以看到图片和滚动。 谢谢你们的帮助,伙计们 以下是我在项目中使用的创建圆形遮罩的方法(这不是Swift,但易于翻译): 我基本上创建了一个子视图,添加到图像滚动视图上方,如下所示: UIImageView *maskView = [[UIImag
谢谢你们的帮助,伙计们 以下是我在项目中使用的创建圆形遮罩的方法(这不是Swift,但易于翻译): 我基本上创建了一个子视图,添加到图像滚动视图上方,如下所示:
UIImageView *maskView = [[UIImageView alloc] initWithImage:[self overlayMask]];
maskView.userInteractionEnabled = NO;
[self.view insertSubview:maskView aboveSubview:_scrollView];
希望有帮助
(最初发现于)西里尔的答案被翻译成了swift,以防止额外的打字:
func circularOverlayMask() -> UIImage {
let bounds = self.view.bounds
let width = bounds.size.width
let height = bounds.size.height
let diameter = width
let radius = diameter / 2
let center = CGPointMake(width / 2, height / 2)
// Create the image context
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)
// Create the bezier paths
let clipPath = UIBezierPath(rect: bounds)
let maskPath = UIBezierPath(ovalInRect: CGRectMake(center.x - radius, center.y - radius, diameter, diameter))
clipPath.appendPath(maskPath)
clipPath.usesEvenOddFillRule = true
clipPath.addClip()
UIColor(white: 0, alpha: 0.5).setFill()
clipPath.fill()
let finalImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
return finalImage
}
谢谢Cyril,您知道如何使用圆圈中显示的内容(作为遮罩后面较大图像的一部分)创建图像对象吗?您应该看看
editeImage
方法中的DZNPhotoPickerController实现:
func circularOverlayMask() -> UIImage {
let bounds = self.view.bounds
let width = bounds.size.width
let height = bounds.size.height
let diameter = width
let radius = diameter / 2
let center = CGPointMake(width / 2, height / 2)
// Create the image context
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)
// Create the bezier paths
let clipPath = UIBezierPath(rect: bounds)
let maskPath = UIBezierPath(ovalInRect: CGRectMake(center.x - radius, center.y - radius, diameter, diameter))
clipPath.appendPath(maskPath)
clipPath.usesEvenOddFillRule = true
clipPath.addClip()
UIColor(white: 0, alpha: 0.5).setFill()
clipPath.fill()
let finalImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
return finalImage
}