गोलाकार UIView का उपयोग करते हुए - केवल कुछ कोनों - कैसे?


121

मेरे आवेदन में - इस प्रकार चार बटन दिए गए हैं:

  • बाएं से बाएं
  • नीचे बाएँ
  • ठीक तरह से ऊपर
  • नीचे दाएं

बटन के ऊपर एक छवि दृश्य (या एक UIView) है।

अब, मान लीजिए कि उपर्युक्त बाएँ बटन पर एक उपयोगकर्ता टैप करता है। छवि / दृश्य के ऊपर उस विशेष कोने में गोल होना चाहिए।

मुझे गोल कोनों को UIView को लागू करने में कुछ कठिनाई हो रही है।

अभी मैं प्रत्येक दृश्य के लिए गोल कोनों को लागू करने के लिए निम्नलिखित कोड का उपयोग कर रहा हूं:

    // imgVUserImg is a image view on IB.
    imgVUserImg.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"any Url Here"];
    CALayer *l = [imgVUserImg layer];
    [l setMasksToBounds:YES];
    [l setCornerRadius:5.0];  
    [l setBorderWidth:2.0];
    [l setBorderColor:[[UIColor darkGrayColor] CGColor]];

उपरोक्त कोड आपूर्ति किए गए दृश्य के प्रत्येक कोने में गोलाई को लागू कर रहा है। इसके बजाय मैं केवल चयनित कोनों के लिए गोलाई लागू करना चाहता था जैसे - शीर्ष / शीर्ष + बाएं / नीचे + दाएं आदि।

क्या यह संभव है? कैसे?

जवाबों:


44

मैंने इस उत्तर का उपयोग किया कि मैं iPhone पर एक गोल कोने वाला UILabel कैसे बनाऊं? और कैसे iPhone से किया पारदर्शिता के साथ एक गोल परिभाषा दृश्य है से कोड ?इस कोड को बनाने के लिए।

तब मैंने महसूस किया कि मैंने गलत प्रश्न का उत्तर दिया (UIImage के बजाय एक गोल यूआईबेल दिया) इसलिए मैंने इस कोड का उपयोग परिवर्तन करने के लिए किया है:

http://discussions.apple.com/thread.jspa?threadID=1683876

व्यू टेम्प्लेट के साथ iPhone प्रोजेक्ट बनाएं। दृश्य नियंत्रक में, इसे जोड़ें:

- (void)viewDidLoad
{
    CGRect rect = CGRectMake(10, 10, 200, 100);
    MyView *myView = [[MyView alloc] initWithFrame:rect];
    [self.view addSubview:myView];
    [super viewDidLoad];
}

MyViewसिर्फ एक UIImageViewउपवर्ग है:

@interface MyView : UIImageView
{
}

मैंने पहले कभी ग्राफिक्स संदर्भों का उपयोग नहीं किया था, लेकिन मैं इस कोड को एक साथ रखने में कामयाब रहा। यह कोनों में से दो के लिए कोड गायब है। यदि आप कोड पढ़ते हैं, तो आप देख सकते हैं कि मैंने इसे कैसे लागू किया (कुछ CGContextAddArcकॉल हटाकर, और कोड में कुछ त्रिज्या मानों को हटाकर। सभी कोनों के लिए कोड है, इसलिए इसे शुरुआती बिंदु के रूप में उपयोग करें और हटाएं। वे हिस्से जो आपके लिए आवश्यक कोनों को बनाते हैं। ध्यान दें कि आप चाहे तो 2 या 3 गोल कोनों से भी आयताकार बना सकते हैं।

कोड सही नहीं है, लेकिन मुझे यकीन है कि आप इसे थोड़ा छोटा कर सकते हैं।

static void addRoundedRectToPath(CGContextRef context, CGRect rect, float radius, int roundedCornerPosition)
{

    // all corners rounded
    //  CGContextMoveToPoint(context, rect.origin.x, rect.origin.y + radius);
    //  CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height - radius);
    //  CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, 
    //                  radius, M_PI / 4, M_PI / 2, 1);
    //  CGContextAddLineToPoint(context, rect.origin.x + rect.size.width - radius, 
    //                          rect.origin.y + rect.size.height);
    //  CGContextAddArc(context, rect.origin.x + rect.size.width - radius, 
    //                  rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);
    //  CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + radius);
    //  CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + radius, 
    //                  radius, 0.0f, -M_PI / 2, 1);
    //  CGContextAddLineToPoint(context, rect.origin.x + radius, rect.origin.y);
    //  CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + radius, radius, 
    //                  -M_PI / 2, M_PI, 1);

    // top left
    if (roundedCornerPosition == 1) {
        CGContextMoveToPoint(context, rect.origin.x, rect.origin.y + radius);
        CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height - radius);
        CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, 
                        radius, M_PI / 4, M_PI / 2, 1);
        CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, 
                                rect.origin.y + rect.size.height);
        CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y);
        CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y);
    }   

    // bottom left
    if (roundedCornerPosition == 2) {
        CGContextMoveToPoint(context, rect.origin.x, rect.origin.y);
        CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height);
        CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, 
                                rect.origin.y + rect.size.height);
        CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y);
        CGContextAddLineToPoint(context, rect.origin.x + radius, rect.origin.y);
        CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + radius, radius, 
                        -M_PI / 2, M_PI, 1);
    }

    // add the other corners here


    CGContextClosePath(context);
    CGContextRestoreGState(context);
}


-(UIImage *)setImage
{
    UIImage *img = [UIImage imageNamed:@"my_image.png"];
    int w = img.size.width;
    int h = img.size.height;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

    CGContextBeginPath(context);
    CGRect rect = CGRectMake(0, 0, w, h);


    addRoundedRectToPath(context, rect, 50, 1);
    CGContextClosePath(context);
    CGContextClip(context);

    CGContextDrawImage(context, rect, img.CGImage);

    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    [img release];

    return [UIImage imageWithCGImage:imageMasked];
}

alt text http://nevan.net/skitch/skitched-20100224-092237.png

यह मत भूलो कि काम करने के लिए आपको क्वार्ट्जकोर रूपरेखा प्राप्त करने की आवश्यकता होगी।


उन विचारों के साथ अपेक्षा के अनुरूप काम नहीं करता जिनका आकार बदल सकता है। उदाहरण: एक उइलाबेल। मास्क का रास्ता तय करना और फिर उसमें टेक्स्ट सेट करके आकार बदलना पुराने मास्क को बनाए रखेगा। ड्रॉअर को सबक्लासिंग और ओवरलोडिंग की आवश्यकता होगी।
14:30 बजे user3099609

358

IOS 3.2 में शुरू, आप UIBezierPathआउट-ऑफ-द-बॉक्स गोल आयत बनाने के लिए s की कार्यक्षमता का उपयोग कर सकते हैं (जहाँ आपके द्वारा निर्दिष्ट केवल कोने गोल हैं)। तब आप इसका उपयोग पथ के रूप में कर सकते हैं CAShapeLayer, और इसे अपने दृश्य की परत के लिए मास्क के रूप में उपयोग कर सकते हैं:

// Create the path (with only the top-left corner rounded)
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds 
                                               byRoundingCorners:UIRectCornerTopLeft
                                                     cornerRadii:CGSizeMake(10.0, 10.0)];

// Create the shape layer and set its path
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = imageView.bounds;
maskLayer.path = maskPath.CGPath;

// Set the newly created shape layer as the mask for the image view's layer
imageView.layer.mask = maskLayer;

और वह यह है - कोर ग्राफिक्स में मैन्युअल रूप से परिभाषित आकार के आसपास कोई गड़बड़ नहीं, फ़ोटोशॉप में कोई मास्किंग छवियां नहीं बना रहा है। परत को भी अमान्य करने की आवश्यकता नहीं है। गोल कोने को लागू करना या नए कोने में बदलना उतना ही सरल है जितना कि किसी नए को परिभाषित करना UIBezierPathऔर CGPathमास्क की परत के मार्ग के रूप में इसका उपयोग करना । cornersके पैरामीटर bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:विधि बिटमास्क है, और इसलिए कई कोनों उन्हें एक साथ Oring से गोल किया जा सकता है।


संपादित करें: एक छाया जोड़ना

यदि आप इसमें एक छाया जोड़ना चाह रहे हैं, तो थोड़ा और काम करना होगा।

क्योंकि " imageView.layer.mask = maskLayer" एक मुखौटा लागू करता है, एक छाया आमतौर पर इसके बाहर नहीं दिखाएगा। ट्रिक पारदर्शी दृश्य का उपयोग करना है, और फिर CALayerदृश्य की परत में दो सबलेयर ( एस) जोड़ें : shadowLayerऔर roundedLayer। दोनों का उपयोग करने की आवश्यकता है UIBezierPath। की सामग्री के रूप में छवि को जोड़ा जाता है roundedLayer

// Create a transparent view
UIView *theView = [[UIView alloc] initWithFrame:theFrame];
[theView setBackgroundColor:[UIColor clearColor]];

// Create the path (with only the top-left corner rounded)
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:theView.bounds 
                                               byRoundingCorners:UIRectCornerTopLeft
                                                     cornerRadii:CGSizeMake(10.0f, 10.0f)];

// Create the shadow layer
CAShapeLayer *shadowLayer = [CAShapeLayer layer];
[shadowLayer setFrame:theView.bounds];
[shadowLayer setMasksToBounds:NO];
[shadowLayer setShadowPath:maskPath.CGPath];
// ...
// Set the shadowColor, shadowOffset, shadowOpacity & shadowRadius as required
// ...

// Create the rounded layer, and mask it using the rounded mask layer
CALayer *roundedLayer = [CALayer layer];
[roundedLayer setFrame:theView.bounds];
[roundedLayer setContents:(id)theImage.CGImage];

CAShapeLayer *maskLayer = [CAShapeLayer layer];
[maskLayer setFrame:theView.bounds];
[maskLayer setPath:maskPath.CGPath];

roundedLayer.mask = maskLayer;

// Add these two layers as sublayers to the view
[theView.layer addSublayer:shadowLayer];
[theView.layer addSublayer:roundedLayer];

1
एक अच्छा है, इससे मुझे समूहीकृत UITableView सेल के
चयनितबेकग्राउंड व्यूज

वैसे भी कोनों को गोल करने के बाद इस दृश्य में एक बूंद छाया जोड़ने के लिए? सफलता के बिना निम्नलिखित प्रयास किया। `स्व.लेयर.मास्कसटॉब = सं; self.layer.shadowColor = [UIColor blackColor] .CGColor; self.layer.shadowRadius = 3; self.layer.shadowOffset = CGSizeMake (-3, 3); self.layer.shadowOpacity = 0.8; self.layer.shouldRasterize = YES; `
क्रिस वैगनर

1
@ क्रिस वाग्नेर: गोल दृश्य पर छाया लागू करने के संबंध में मेरा संपादन देखें।
स्टुअर्ट

@StuDev उत्कृष्ट! मैं छाया दृश्य के लिए एक सबव्यू के साथ गया था लेकिन यह बहुत अच्छा है! इस तरह उप परतों को जोड़ने के लिए नहीं सोचा था। धन्यवाद!
क्रिस वैगनर

मेरी छवि सफेद क्यों हो जाती है, जब मैं UITableView स्क्रॉल करता हूं और सेल को फिर से बनाया जाता है - यह काम करता है! क्यों?
फर्नांडो रेडोंडो

18

मैंने अपने कोड में कई स्थानों पर इस कोड का उपयोग किया है और यह 100% सही ढंग से काम करता है। आप किसी भी संपत्ति को बदलकर एक संपत्ति को बदल सकते हैं "byRoundingCorners: UIRectCornerBottomeeft"

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(10.0, 10.0)];

                CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
                maskLayer.frame = view.bounds;
                maskLayer.path = maskPath.CGPath;
                view.layer.mask = maskLayer;
                [maskLayer release];

इस समाधान का उपयोग करते समय UITableView-सुबह (जैसे UITableViewCells या UITableViewHeaderFooterViews) यह स्क्रॉल करते समय खराब चिकनाई का परिणाम देता है । उस उपयोग के लिए एक और दृष्टिकोण एक बेहतर प्रदर्शन के साथ यह समाधान है (यह सभी कोनों के लिए एक कोनेरेडियस जोड़ता है)। केवल विशिष्ट कोनों को 'शो' करने के लिए (ऊपर और नीचे दाएं कोनों की तरह) मैंने इसके ऊपर एक नकारात्मक मान के साथ एक frame.origin.xसबव्यू जोड़ा और कोनेरेडियस को इसकी परत को सौंपा। अगर किसी को एक बेहतर समाधान मिला, मुझे दिलचस्पी है।
anneblue

11

IOS 11 में, हम अब केवल कुछ कोनों को गोल कर सकते हैं

let view = UIView()

view.clipsToBounds = true
view.layer.cornerRadius = 8
view.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner]

7

स्विफ्ट 3+ सिंटैक्स के साथ कायर एक्सटेंशन

extension CALayer {

    func round(roundedRect rect: CGRect, byRoundingCorners corners: UIRectCorner, cornerRadii: CGSize) -> Void {
        let bp = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: cornerRadii)
        let sl = CAShapeLayer()
        sl.frame = self.bounds
        sl.path = bp.cgPath
        self.mask = sl
    }
}

इसका उपयोग इस तरह किया जा सकता है:

let layer: CALayer = yourView.layer
layer.round(roundedRect: yourView.bounds, byRoundingCorners: [.bottomLeft, .topLeft], cornerRadii: CGSize(width: 5, height: 5))

5

विशिष्ट कोनों को गोल करने के लिए स्टुअर्ट उदाहरण महान काम करता है। यदि आप कई कोनों को गोल करना चाहते हैं जैसे शीर्ष बाएँ और दाएँ यह कैसे करना है

// Create the path (with only the top-left corner rounded)
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageview
                                               byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight
                                                     cornerRadii:CGSizeMake(10.0, 10.0)];

// Create the shape layer and set its path
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = imageview.bounds;
maskLayer.path = maskPath.CGPath;

// Set the newly created shape layer as the mask for the image view's layer
imageview.layer.mask = maskLayer; 

इस समाधान का उपयोग करते समय UITableView-सुबह (जैसे UITableViewCells या UITableViewHeaderFooterViews) यह स्क्रॉल करते समय खराब चिकनाई का परिणाम देता है । उस उपयोग के लिए एक और दृष्टिकोण एक बेहतर प्रदर्शन के साथ यह समाधान है (यह सभी कोनों के लिए एक कोनेरेडियस जोड़ता है)। केवल विशिष्ट कोनों को 'शो' करने के लिए (ऊपर और नीचे दाएं कोनों की तरह) मैंने इसके ऊपर एक नकारात्मक मान के साथ एक frame.origin.xसबव्यू जोड़ा और कोनेरेडियस को इसकी परत को सौंपा। अगर किसी को एक बेहतर समाधान मिला, मुझे दिलचस्पी है।
anneblue

5

साझा करने के लिए धन्यवाद। यहां मैं इस मुद्दे पर आगे के संदर्भ के लिए तेजी से 2.0 पर समाधान साझा करना चाहता हूं। (UIRectCorner के प्रोटोकॉल के अनुरूप)

let mp = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.bottomLeft, .TopLeft], cornerRadii: CGSize(width: 10, height: 10))
let ml = CAShapeLayer()
ml.frame = self.bounds
ml.path = mp.CGPath
self.layer.mask = ml

4

एक आसान और तेज़ उत्तर है जो आपकी आवश्यकताओं के आधार पर काम कर सकता है और छाया के साथ भी काम करता है। आप superlayer पर maskToBounds को सच करने के लिए सेट कर सकते हैं, और बच्चे की परतों को ऑफसेट कर सकते हैं ताकि उनके 2 कोने सुपरलेयर सीमा के बाहर हों, प्रभावी रूप से 2 तरफ दूर गोल कोनों को काटकर।

बेशक यह केवल तब काम करता है जब आप एक ही तरफ केवल 2 गोल कोनों को रखना चाहते हैं और परत की सामग्री एक समान दिखती है जब आप एक तरफ से कुछ पिक्सेल काटते हैं। शीर्ष चार्ट पर केवल बार चार्ट गोल होने के लिए बहुत अच्छा काम करता है।


3

इससे संबंधित प्रश्न देखें । आपको कुछ गोल कोनों के CGPathसाथ अपनी आयत खींचनी होगी , अपने को जोड़ना होगा और फिर इसका उपयोग करके क्लिप करना होगाCGPathCGContextCGContextClip

आप एक छवि के लिए अल्फा मान के साथ गोल आयत भी खींच सकते हैं और फिर एक नई परत बनाने के लिए उस छवि का उपयोग कर सकते हैं जिसे आप अपनी परत की maskसंपत्ति के रूप में सेट करते हैं ( Apple के दस्तावेज़ देखें )।


2

डेढ़ दशक देर से, लेकिन मुझे लगता है कि लोगों का वर्तमान तरीका 100% सही नहीं है। कई लोगों का यह मुद्दा रहा है कि UIBezierPath + CAShapeLayer पद्धति का उपयोग करना ऑटो-लेआउट के साथ हस्तक्षेप करता है, खासकर जब यह स्टोरीबोर्ड पर सेट होता है। इस पर कोई जवाब नहीं मिलता, इसलिए मैंने खुद को जोड़ने का फैसला किया।

इसे दरकिनार करने का एक बहुत आसान तरीका है: गोल कोनों को अंदर खींचें drawRect(rect: CGRect) फ़ंक्शन ।

उदाहरण के लिए, अगर मुझे UIView के लिए शीर्ष गोल कोनों की आवश्यकता थी, तो मैं UIView को उपवर्ग करूँगा और फिर जहाँ भी उपयुक्त हो, उस उपवर्ग का उपयोग करूँगा।

import UIKit

class TopRoundedView: UIView {

    override func drawRect(rect: CGRect) {
        super.drawRect(rect)

        var maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: UIRectCorner.TopLeft | UIRectCorner.TopRight, cornerRadii: CGSizeMake(5.0, 5.0))

        var maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.CGPath

        self.layer.mask = maskLayer
    }
}

यह मुद्दे को जीतने का सबसे अच्छा तरीका है और अनुकूलन के लिए बिल्कुल भी समय नहीं लगता है।


1
यह समस्या को हल करने का सही तरीका नहीं है। drawRect(_:)यदि आप अपने दृश्य में कस्टम ड्राइंग कर रहे हैं (जैसे कोर ग्राफिक्स के साथ), तो आपको केवल ओवरराइड करना चाहिए , अन्यथा एक खाली कार्यान्वयन भी प्रदर्शन को प्रभावित कर सकता है। हर बार एक नई मुखौटा परत बनाना अनावश्यक भी है। pathजब आपको दृश्य की सीमा बदल जाती है, और layoutSubviews()विधि के ओवरराइड के भीतर होने वाली जगह के लिए आपको वास्तव में मुखौटा परत की संपत्ति को अपडेट करने की आवश्यकता होती है ।
स्टुअर्ट

2

केवल कुछ कोनों को गोल करना ऑटो आकार बदलने या ऑटो लेआउट के साथ अच्छा नहीं खेलेगा।

तो एक अन्य विकल्प नियमित उपयोग करना है cornerRadiusऔर उन कोनों को छिपाना है जो आप किसी अन्य दृश्य के नीचे या उसके पर्यवेक्षण सीमा के बाहर नहीं चाहते हैं, जिससे यह सुनिश्चित होता है कि यह उनकी सामग्री को क्लिप करने के लिए सेट है।


1

करने के लिए जोड़ने के लिए इस सवाल का जवाब और इसके अलावा , मैं एक सरल, पुन: प्रयोज्य बनाया UIViewस्विफ्ट में। आपके उपयोग के मामले के आधार पर, आप संशोधन करना चाहते हैं (हर लेआउट आदि पर वस्तुओं को बनाने से बचें), लेकिन मैं इसे यथासंभव सरल रखना चाहता था। UIImageViewयदि आप उपवर्ग पसंद नहीं करते हैं, तो एक्सटेंशन आपको इसे अन्य दृश्य (पूर्व ) में लागू करने की अनुमति देता है ।

extension UIView {

    func roundCorners(_ roundedCorners: UIRectCorner, toRadius radius: CGFloat) {
        roundCorners(roundedCorners, toRadii: CGSize(width: radius, height: radius))
    }

    func roundCorners(_ roundedCorners: UIRectCorner, toRadii cornerRadii: CGSize) {
        let maskBezierPath = UIBezierPath(
            roundedRect: bounds,
            byRoundingCorners: roundedCorners,
            cornerRadii: cornerRadii)
        let maskShapeLayer = CAShapeLayer()
        maskShapeLayer.frame = bounds
        maskShapeLayer.path = maskBezierPath.cgPath
        layer.mask = maskShapeLayer
    }
}

class RoundedCornerView: UIView {

    var roundedCorners: UIRectCorner = UIRectCorner.allCorners
    var roundedCornerRadii: CGSize = CGSize(width: 10.0, height: 10.0)

    override func layoutSubviews() {
        super.layoutSubviews()
        roundCorners(roundedCorners, toRadii: roundedCornerRadii)
    }
}

यहां बताया गया है कि आप इसे कैसे लागू करेंगे UIViewController:

class MyViewController: UIViewController {

    private var _view: RoundedCornerView {
        return view as! RoundedCornerView
    }

    override func loadView() {
        view = RoundedCornerView()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        _view.roundedCorners = [.topLeft, .topRight]
        _view.roundedCornerRadii = CGSize(width: 10.0, height: 10.0)
    }
}

0

स्टुअर्ट के जवाब को लपेटते हुए, आपके पास निम्नलिखित के रूप में कोने की विधि हो सकती है:

@implementation UIView (RoundCorners)

- (void)applyRoundCorners:(UIRectCorner)corners radius:(CGFloat)radius {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;

    self.layer.mask = maskLayer;
}

@end

तो गोल कोने को लागू करने के लिए, आप बस करें:

[self.imageView applyRoundCorners:UIRectCornerTopRight|UIRectCornerTopLeft radius:10];

0

मैं एक परत के मुखौटे को परिभाषित करने का सुझाव दूंगा। मुखौटा अपने आप में CAShapeLayerएक समर्पित पथ वाला होना चाहिए । आप अगले UIView एक्सटेंशन (स्विफ्ट 4.2) का उपयोग कर सकते हैं:

extension UIView {
    func round(corners: UIRectCorner, with radius: CGFloat) {
        let maskLayer = CAShapeLayer()
        maskLayer.frame = bounds
        maskLayer.path = UIBezierPath(
            roundedRect: bounds,
            byRoundingCorners: corners,
            cornerRadii: CGSize(width: radius, height: radius)
        ).cgPath
        layer.mask = maskLayer
   }
}
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.