Created
September 21, 2016 15:17
-
-
Save emrahgunduz/2c7ed2165956b41a7441a89af0efca42 to your computer and use it in GitHub Desktop.
Playground example for working UIBlurEffect and UIVisualEffectView masking on iOS10 using Swift 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import PlaygroundSupport | |
let generalFrame = CGRect(x: 0, y: 0, width: 500, height: 500) | |
let containerView = UIView(frame: generalFrame) | |
containerView.backgroundColor = UIColor.black; | |
PlaygroundPage.current.liveView = containerView | |
let parentView = UIView(frame: generalFrame) | |
containerView.addSubview(parentView) | |
let url = URL(string: "https://static.pexels.com/photos/168066/pexels-photo-168066-large.jpeg") | |
let data = try Data(contentsOf: url!); | |
let imageView = UIImageView(frame:parentView.bounds) | |
imageView.image = UIImage(data: data) | |
imageView.contentMode = .scaleAspectFill | |
let maskView = UIView(frame:parentView.bounds) | |
maskView.backgroundColor = UIColor.black | |
maskView.layer.mask = {() -> CALayer in | |
var roundedRect = CGRect ( | |
x: 0.0, | |
y: 0.0, | |
width: parentView.bounds.size.width * 0.5, | |
height: parentView.bounds.size.width * 0.5 | |
); | |
roundedRect.origin.x = parentView.frame.size.width / 2 - roundedRect.size.width / 2; | |
roundedRect.origin.y = parentView.frame.size.height / 2 - roundedRect.size.height / 2; | |
let cornerRadius = roundedRect.size.height / 2.0; | |
let path = UIBezierPath(rect:parentView.bounds) | |
let croppedPath = UIBezierPath(roundedRect: roundedRect, cornerRadius: cornerRadius) | |
path.append(croppedPath) | |
path.usesEvenOddFillRule = true | |
let maskLayer = CAShapeLayer() | |
maskLayer.path = path.cgPath; | |
maskLayer.fillRule = kCAFillRuleEvenOdd | |
return maskLayer | |
}() | |
let blurView = UIBlurEffect(style: .light) | |
let effectView = UIVisualEffectView(effect: blurView) | |
effectView.frame = generalFrame | |
effectView.mask = maskView | |
parentView.addSubview(imageView) | |
parentView.addSubview(effectView) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Merhaba Emrah,
XCode 10 (Swift 4) playground ile ön bir kod çalışması yapmaktayım. Tek bir UIView üzerine BezierPath ile serbest çizgiler çizmekteyim. İstiyorum ki kırmızı renkteki kalın çizgileri çizerken, beyaz renk zemini olan UIView elementini mask yapsın. Yani, beyaz zeminde çizdiğim çizgiler beyaz zemini silsin. Aşağıdaki kodlamamda tam tersi oluyor. Yardımcı olabilir misin? Erhan Üzümcü