2013-01-19 59 views
20

में आईओएस इनवर्टर मास्क नीचे दिए गए कोड के साथ, मैं सफलतापूर्वक अपने चित्र का हिस्सा मास्क कर रहा हूं, लेकिन यह मेरे मुखौटा के विपरीत है। यह चित्रकला के आंतरिक भाग को मुखौटा करता है, जहां मैं बाहरी हिस्से को मुखौटा करना चाहता हूं। क्या इस मुखौटा को घुमाने का कोई आसान तरीका है?drawRect

myPath नीचे UIBezierPath है।

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
CGMutablePathRef maskPath = CGPathCreateMutable(); 
CGPathAddPath(maskPath, nil, myPath.CGPath); 
[maskLayer setPath:maskPath]; 
CGPathRelease(maskPath); 
self.layer.mask = maskLayer; 

उत्तर

30
आकार परत ( maskLayer.fillRule = kCAFillRuleEvenOdd;) आप एक बड़े आयत है कि पूरे फ्रेम को शामिल किया गया जोड़ सकते हैं और तब आकार तुम बाहर मास्किंग कर रहे हैं जोड़ने पर भी अजीब भरने के साथ

। यह प्रभावी रूप से मुखौटा उलटा होगा।

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
CGMutablePathRef maskPath = CGPathCreateMutable(); 
CGPathAddRect(maskPath, NULL, someBigRectangle); // this line is new 
CGPathAddPath(maskPath, nil, myPath.CGPath); 
[maskLayer setPath:maskPath]; 
maskLayer.fillRule = kCAFillRuleEvenOdd;   // this line is new 
CGPathRelease(maskPath); 
self.layer.mask = maskLayer; 
+0

हो सकता है आप इस सवाल का भी जवाब दे सकती है: http://stackoverflow.com/questions/30360389/ उपयोग-परत-मास्क-टू-मेक-पार्ट-ऑफ-द-यूविए-पारदर्शी – confile

+0

यह उत्तर बहुत अच्छा है और बेकार ढंग से काम करता है। –

+0

CGPathRelease (मास्कपैथ) हटा दिया गया था? यह काम कर रहा है लेकिन क्या मुझे मेमोरी लीक मिल सकती है? (स्विफ्ट 2.2, आईओएस 9.0) इसका कोई संदर्भ नहीं मिला। – Maik639

7

स्वीकृत उत्तर के आधार पर, स्विफ्ट में एक और मैशप है। मैं इसे एक समारोह में बनाए और उन्हें invert वैकल्पिक

class func mask(viewToMask: UIView, maskRect: CGRect, invert: Bool = false) { 
    let maskLayer = CAShapeLayer() 
    let path = CGPathCreateMutable() 
    if (invert) { 
     CGPathAddRect(path, nil, viewToMask.bounds) 
    } 
    CGPathAddRect(path, nil, maskRect) 

    maskLayer.path = path 
    if (invert) { 
     maskLayer.fillRule = kCAFillRuleEvenOdd 
    } 

    // Set the mask of the view. 
    viewToMask.layer.mask = maskLayer; 
} 
8

बनाया स्विफ्ट 3.0 के लिए

func mask(viewToMask: UIView, maskRect: CGRect, invert: Bool = false) { 
    let maskLayer = CAShapeLayer() 
    let path = CGMutablePath() 
    if (invert) { 
     path.addRect(viewToMask.bounds) 
    } 
    path.addRect(maskRect) 

    maskLayer.path = path 
    if (invert) { 
     maskLayer.fillRule = kCAFillRuleEvenOdd 
    } 

    // Set the mask of the view. 
    viewToMask.layer.mask = maskLayer; 
}