2014-09-02 11 views
29

C'è un modo per impostare cornerRadius solo per l'angolo in basso a sinistra, in basso a destra e in alto a sinistra di un UIView?come impostare cornerRadius solo per l'angolo in basso a sinistra, in basso a destra e in alto a sinistra di un UIView?

Ho provato quanto segue, ma ho finito per far sparire la vista. C'è qualcosa di sbagliato con il codice qui sotto?

UIBezierPath *maskPath; 
    maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(20.0, 20.0)]; 
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
    maskLayer.path = maskPath.CGPath; 
    view.layer.mask = maskLayer; 
+0

Tenta di usare 'UIBezierPath * maskPath = [UIBezierPath nuovo],' e quindi utilizzare 'addLineToPoint:', 'addArcWithCenter: Raggio: startAngle: endAngle: in senso orario:' metodi. –

+0

Il tuo codice sembra a posto. Basta controllare di aver aggiunto il framework QuartzCore e importato nella classe in cui si sta scrivendo questo codice. Non so che sia esattamente il caso ma è successo con me prima. –

+0

possibile duplicato di [UIView arrotondato utilizzando CALayers - solo alcuni angoli - Come?] (Http://stackoverflow.com/questions/2264083/rounded-uiview-using-calayers-only-some-corners-how) – Stuart

risposta

113

si può fare in questo modo:

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.viewOutlet.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)]; 

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

enter image description here

Aggiornamento:
If You bisogno di bordo basta creare un altro CAShapeLayer e aggiungerlo al livello di visualizzazione come sottolivello. Ti piace questa (posto questo codice sotto il codice in alto):

CAShapeLayer *borderLayer = [[CAShapeLayer alloc] init]; 
borderLayer.frame = self.view.bounds; 
borderLayer.path = maskPath.CGPath; 
borderLayer.lineWidth = 4.0f; 
borderLayer.strokeColor = [UIColor blackColor].CGColor; 
borderLayer.fillColor = [UIColor clearColor].CGColor; 

[self.viewOutlet.layer addSublayer:borderLayer]; 

enter image description here

a Swift 3.0 in questo modo:

let maskPath = UIBezierPath.init(roundedRect: self.viewOutlet.bounds, byRoundingCorners:[.topLeft, .bottomLeft], cornerRadii: CGSize.init(width: 10.0, height: 10.0)) 
let maskLayer = CAShapeLayer() 
maskLayer.frame = self.viewOutlet.bounds 
maskLayer.path = maskPath.cgPath 
self.viewOutlet.layer.mask = maskLayer 
+0

qui ho bisogno di impostare confine per questa vista ... È possibile mostrare il confine? – Siva

+0

@Ssn Dai un'occhiata alla risposta aggiornata –

+0

Grazie. funziona molto bene. – fozoglu

0

Ti sei perso per aggiungere UIRectCornerTopLeft. Si prega di provare questo codice:

UIRectCorner rectCorner = UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight; 

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

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
maskLayer.path = maskPath.CGPath; 
view.layer.mask = maskLayer; 
view.layer.masksToBounds = YES; 

può essere utile ....

+3

Copiato da http : //stackoverflow.com/questions/10167266/how-to-set-cornerradius-for-only-top-left-and-top-right-corner-of-a-uiview? rq = 1 – boeqqsh

8

Utilizzare un costume UIView e attuare il codice qui sotto

#import "CustomUIView.h" 

@implementation CustomView 

- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
    CAShapeLayer * maskLayer = [CAShapeLayer layer]; 
    maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: self.bounds byRoundingCorners: UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii: (CGSize){20.0, 20.0}].CGPath; 

    self.layer.mask = maskLayer; 
} 

@end 

Uscita:

enter image description here

Guarda le UIBezierPath.h opzioni:

typedef NS_OPTIONS(NSUInteger, UIRectCorner) { 
    UIRectCornerTopLeft  = 1 << 0, 
    UIRectCornerTopRight = 1 << 1, 
    UIRectCornerBottomLeft = 1 << 2, 
    UIRectCornerBottomRight = 1 << 3, 
    UIRectCornerAllCorners = ~0UL 
}; 
3

speranza che questo vi aiuterà

il tuo codice di cui sopra funziona perfettamente sulla mia macchina, possono essere impostate CAShapeLayer cornice uguale alla vista frame a causa del quale la tua vista scompare ma non vedo questa riga nel tuo codice, quindi controlla la proprietà della tua vista e applica il codice sottostante

UIBezierPath *maskPath; 
    maskPath = [UIBezierPath bezierPathWithRoundedRect:viewName.bounds 
            byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight |UIRectCornerTopLeft) 
              cornerRadii:CGSizeMake(20.0, 20.0)]; 
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
    maskLayer.frame =viewName.bounds; 
    maskLayer.path = maskPath.CGPath; 
    viewName.layer.mask = maskLayer; 
3

So che è molto tardi, ma ho appena creato un metodo in cui è possibile passare solo uno o più UIRectCorners.

[self setMaskTo:myView byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)]; 

- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners 
     { 
      UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds 
                  byRoundingCorners:corners 
                   cornerRadii:CGSizeMake(8.0, 8.0)]; 
      CAShapeLayer *shape = [[CAShapeLayer alloc] init]; 
      [shape setPath:rounded.CGPath]; 
      view.layer.mask = shape; 
    } 
0
UIBezierPath *maskPath; 
maskPath = [UIBezierPath bezierPathWithRoundedRect:someView.bounds 
          byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight) 
            cornerRadii:CGSizeMake(10.0,10.0,5.0, 10.0)]; 


CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
maskLayer.frame = self.bounds; 
maskLayer.path = maskPath.CGPath; 
someView.layer.mask = maskLayer; 
0

Prova questo codice

-(void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners { 
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(10.0, 10.0)]; 
    CAShapeLayer* shape = [[CAShapeLayer alloc] init]; 
    [shape setPath:rounded.CGPath]; 
    view.layer.mask = shape; 
} 

utilizzando chiamate

[self setMaskTo:myView byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight]; 

Opzioni disponibili:

UIRectCornerTopLeft, UIRectCornerTopRight,UIRectCornerBottomLeft,UIRectCornerBottomRight,UIRectCornerAllCorners 
6

Impostazione angolo-raggio per una vista in Swift

La creazione e mascheratura di strati devono correre all'interno del drawRect() blocco opzionale ereditato da UIView

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

     let maskPath = UIBezierPath(roundedRect: self.view.bounds, byRoundingCorners: [.TopLeft, .BottomLeft, .BottomRight], cornerRadii: CGSizeMake(10, 10)) 
     let maskLayer = CAShapeLayer() 
     maskLayer.frame = self.view.bounds 
     maskLayer.path = maskPath.CGPath 
     self.view.layer.mask = maskLayer 
    } 
11

Testata su xcode 8 e swift 3

extension UIView { 
    func roundCorners(corners:UIRectCorner, radius: CGFloat) { 
     let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) 
     let mask = CAShapeLayer() 
     mask.path = path.cgPath 
     self.layer.mask = mask 
    } 
} 

E utilizzare come questo

YourView.roundCorners([.topLeft, .bottomLeft], radius: 10) 
0

uso questo raggio d'angolo codice di vista solo due sides.after molto tempo ho avuto ios suggerimento. qui viewFilter è vista outlet che vogliamo arrotondato.

-(void) viewDidAppear:(BOOL)animated{ 
[super viewDidAppear:animated]; 
[self setMaskTo:viewFilter byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight]; 
} 

-(void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners{ 

UIBezierPath *rounded = [UIBezierPathbezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(20.0, 20.0)]; 

CAShapeLayer *shape = [[CAShapeLayer alloc] init]; 
shape.frame = self.view.bounds; 
[shape setPath:rounded.CGPath]; 
view.layer.mask = shape; 
} 
0

Su iOS 11, quello che ti serve sono i MaskedCorners.

let cornerRadius: CGFloat = 6.0 
    if #available(iOS 11, *) { 
     productImageView.layer.cornerRadius = cornerRadius 
     productImageView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner] 

    } else { 
     let path  = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.topLeft , .topRight], cornerRadii:CGSize(width: cornerRadius, height: cornerRadius)) 
     let maskLayer = CAShapeLayer() 
     maskLayer.frame = bounds 
     maskLayer.path = path.cgPath 
     productImageView.mask    = maskLayer 
     productImageView.layer.masksToBounds = true 
    } 
Problemi correlati