2016-01-06 8 views
6

मेरे ऐप में, मैंने सरल UIView के उप-वर्गीकरण करके UIView के माध्यम से देखा। हालांकि, अगर मैं UIVisualEffectView का उपयोग करके ऐसा करने की कोशिश करता हूं, तो मैं इसे करने में सक्षम नहीं हूं। के माध्यम से UIView है,UIVisualEffectView पर पारदर्शी कट कैसे करें?

enter image description here

जब मैं हरी UIView के स्थान पर UIVisualEffectView उपयोग करते हैं, मैं नहीं देख सकता UIView के माध्यम से देखते भले ही देखें:

यहाँ मैं का उपयोग कर सामान्य UIView ऐसा करने में सक्षम हूँ UIVisualEffectView में subview के रूप में जोड़ा गया।

enter image description here

कोड:

- (void)drawRect:(CGRect)rect { //this is same for the UIVIew and for the UIVisualEffectView 
    [super drawRect:rect]; 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    // Clear any existing drawing on this view 
    // Remove this if the hole never changes on redraws of the UIView 
    CGContextClearRect(context, self.bounds); 

    // Create a path around the entire view 
    UIBezierPath *clipPath = [UIBezierPath bezierPathWithRect:self.bounds]; 

    // Your transparent window. This is for reference, but set this either as a property of the class or some other way 
    CGRect transparentFrame; 
    // Add the transparent window 
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:transparentFrame cornerRadius:5.0f]; 
    [clipPath appendPath:path]; 

    // NOTE: If you want to add more holes, simply create another UIBezierPath and call [clipPath appendPath:anotherPath]; 

    // This sets the algorithm used to determine what gets filled and what doesn't 
    clipPath.usesEvenOddFillRule = YES; 
    // Add the clipping to the graphics context 
    [clipPath addClip]; 

    // set your color 
    UIColor *tintColor = [UIColor greenColor]; 

    // (optional) set transparency alpha 
    CGContextSetAlpha(context, 0.7f); 
    // tell the color to be a fill color 
    [tintColor setFill]; 
    // fill the path 
    [clipPath fill]; 
} 

प्रश्न: क्यों इस UIVisualEffectView साथ काम नहीं किया?

+0

हाय तेजा, क्या आप इस पोस्ट में उल्लिखित कार्यक्षमता को प्राप्त करने में सक्षम हो सकते हैं। यदि हां, तो क्या आप मेरी मदद कर सकते हैं या निम्नलिखित पोस्ट में दिए गए कोड के लिए कुछ बदलाव सुझा सकते हैं। http://stackoverflow.com/questions/39165751/circle-masking-for-image-cropping-in-ios?noredirect=1#comment65676404_39165751 –

+0

@sree_iphonedev एक बार जब मैं कंप्यूटर के सामने आऊंगा! –

+0

क्या आपने अंततः इसे हल किया? –

उत्तर

0

अपने ViewController.h में वैश्विक चर निम्नलिखित जोड़ें अपने ViewController.m फ़ाइल

-(void)addOverlay:(CGRect)rect{ 

    float x = rect.origin.x; 
    float y = rect.origin.y; 
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) cornerRadius:0]; 
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(x, y, rect.size.width, rect.size.height) cornerRadius:5]; 

    [path appendPath:circlePath]; 
    [path setUsesEvenOddFillRule:YES]; 

    [self removeOverlay]; 
    overlayView = [[UIVisualEffectView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height+64)]; 
    overlayView.backgroundColor = [UIColor clearColor]; 
    [self.view addSubview:overlayView]; 

    fillLayer = [CAShapeLayer layer]; 
    fillLayer.path = path.CGPath; 

    fillLayer.fillRule = kCAFillRuleEvenOdd; 

    fillLayer.fillColor = [UIColor colorWithRed:78/255.0 green:103/255.0 blue:135/255.0 alpha:1.0].CGColor; 
    fillLayer.opacity = 0.85; 
    [[UIApplication sharedApplication].keyWindow.layer addSublayer:fillLayer]; 

} 

-(void)removeOverlay{ 
    [overlayView removeFromSuperview]; 
    [fillLayer removeFromSuperlayer]; 
} 

में तरीकों का पालन फ़ाइल

CAShapeLayer *fillLayer; 
UIVisualEffectView *overlayView; 

जोड़ें और के रूप में यह कहते हैं -

[self addOverlay:rect]; 
संबंधित मुद्दे