Ciao! Voglio disegnare un angolo arrotondato del mio UIView
. Solo uno, gli altri non possono essere modificati.Come disegnare un singolo angolo arrotondato del mio UIView.
risposta
A partire da iOS 3.2, è possibile utilizzare la funzionalità di UIBezierPath
s per creare un rettangolo arrotondato pronto all'uso (in cui solo gli angoli specificati sono arrotondati). È quindi possibile utilizzare questo come il percorso di un CAShapeLayer
, e utilizzare questo come una maschera per il livello della vostra vista:
// 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;
E questo è tutto - senza fare in giro manualmente definire forme Core Graphics, senza creazione mascherare le immagini in Photoshop . Il livello non ha nemmeno bisogno di invalidare. Applicare l'angolo arrotondato o passare a un nuovo angolo è semplice come definire un nuovo UIBezierPath
e utilizzare il suo CGPath
come percorso del livello maschera. Il parametro corners
del metodo bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:
è una maschera di bit, pertanto è possibile arrotondare più angoli eseguendo il raggruppamento.
NOTA - Le maschere di livello non vengono visualizzate se utilizzate in combinazione con il metodo renderInContext di CALayer. Se è necessario utilizzare questo, provare a arrotondare gli angoli con il seguente: Just two rounded corners?.
ho fatto un metodo di risposta di StuDev:
+ (CAShapeLayer *) roundedCornerOnImage: (UIImageView *)imageView onCorner: (UIRectCorner)rectCorner
{
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds
byRoundingCorners:rectCorner
cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = imageView.bounds;
maskLayer.path = maskPath.CGPath;
return maskLayer;
}
Esempio di utilizzo su un ImageView in un UITableViewCell:
if (indexPath.row == 0)
cell.imageView.layer.mask = [Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerTopLeft];
else if (indexPath.row == self.arrayPeople.count - 1)
cell.imageView.layer.mask = [Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerBottomLeft];
Grazie a StuDev per una grande soluzione!
Potrei anche aggiungere un bordo attorno a questo percorso per evidenziare .. ?? –
+ (CAShapeLayer *) roundedCornerOnImage: (UIImageView *)imageView onCorner: (UIRectCorner)rectCorner
{
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds
byRoundingCorners:rectCorner
cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = imageView.bounds;
maskLayer.path = maskPath.CGPath;
imageView.layer.mask=maskLayer
return maskLayer;
}
if (indexPath.row == 0)
[Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerTopLeft];
else if (indexPath.row == self.arrayPeople.count - 1)
[Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerBottomLeft];
Una risposta aggiornata a quanto sopra, non è necessario restituirlo e gestirlo. Può essere fatto in quella funzione.
Ho fatto una funzione per rendere il raggio d'angolo personalizzato dopo aver fatto qualche piccolo R & D come segue:
+(void)setConerRadiusForTopLeft:(BOOL)isForTopLeft ForTopRight:(BOOL)isForTopRight ForBottomLeft:(BOOL)isForBottomLeft ForBottomRight:(BOOL)isForBottomRight withCornerRadius:(float)cornerRadius forView:(UIView *)view
{
UIRectCorner corners = (isForTopLeft ? UIRectCornerTopLeft : 0) |
(isForTopRight ? UIRectCornerTopRight : 0) |
(isForBottomLeft ? UIRectCornerBottomLeft : 0) |
(isForBottomRight ? UIRectCornerBottomRight : 0);
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds
byRoundingCorners:corners
cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view.bounds;
maskLayer.path = maskPath.CGPath;
view.layer.mask = maskLayer;
}
- 1. Disegnare un UIView arrotondato con gradiente e ombra
- 2. UITextField - Numero angolo arrotondato
- 3. UISegmentedControl senza angolo arrotondato?
- 4. Errore angolo arrotondato Java
- 5. WPF MediaElement con angolo arrotondato
- 6. CSS3: trasparente arrotondato angolo Problema
- 7. Come disegnare un percorso scorrevole/arrotondato?
- 8. Creazione di una vista angolo arrotondato
- 9. Come disegnare un rettangolo arrotondato in C#
- 10. UIView con angolo arrotondato e bordo ha il colore del bordo sbagliato
- 11. Come creare una forma con angolo superiore arrotondato a sinistra arrotondato e angolo arrotondato a sinistra in basso?
- 12. Come creare un NSImageView con un angolo arrotondato?
- 13. iOS UITableViewCell cell.imageVisualizzazione impostazione angolo arrotondato
- 14. iOS arrotondato angolo UIImmagine con bordo
- 15. WPF angolo arrotondato casella di testo
- 16. svg css angolo arrotondato non funziona
- 17. Come posso disegnare un rettangolo arrotondato Etichetta o Vista?
- 18. Come utilizzare l'etichetta angolo arrotondato in iphone, UILabel Round Corners
- 19. Android TextView angolo arrotondato con perfetta rotonda nell'angolo
- 20. UIView con angolo rotondo e bordo bianco
- 21. Angolo superiore sinistro e destro arrotondato in Javafx/CSS
- 22. UILabel con angolo arrotondato (Xcode 4.5 e iOS 6)
- 23. Java - Pannello angolo arrotondato con compositing in paintComponent
- 24. jQuery codice angolo arrotondato per IE8 in modalità standard?
- 25. Come contorno di un angolo arrotondato di CALayer come tracciato di ritaglio
- 26. Creare un JFrame/ContentPane arrotondato
- 27. Come posso disegnare la progettazione del mio database?
- 28. Crea un angolo personalizzato con un rettangolo arrotondato in alto a sinistra?
- 29. Come capovolgere un singolo UIView (senza capovolgere la vista genitore)
- 30. Style lista non ordinata per visualizzare gli elementi della lista in scatola angolo arrotondato
Il codice mi ha davvero salvato. Se avessi la possibilità, avrei contrassegnato correttamente questa risposta. – itsaboutcode
Una nota: se si modifica la dimensione della vista, sarà necessario ricreare la maschera. Pertanto, sottoclasse UIView e sovrascrive frame e bound. – Krumelur
Potrei anche aggiungere un bordo attorno a questo percorso per evidenziare .. ?? –