2015-02-25 1 views
3

Il y a quelques réponses pour l'objectif c mais il n'y en a pas trouvé concernant swift.Swift: Comment créer un UIView avec un cercle transparent au milieu?

Je voudrais créer une vue sombre avec un cercle transparent au milieu afin que l'utilisateur puisse voir la sous-vue et interagir avec elle. Comment puis-je l'implémenter en utilisant swift.

Plus précisément, je suis à la recherche d'un résultat comme l'implémentation de l'image de profil whatsapp. Il y a ce cercle transparent au milieu et l'utilisateur peut voir l'image et faire défiler par exemple.

Merci pour votre aide les gars!

Répondre

2

Voici une méthode que j'utilise dans mes projets pour créer des masques circulaires (ce n'est pas à Swift mais facilement traduisible):

- (UIImage *)circularOverlayMask 
{ 
    // Constants 
    CGRect bounds = self.navigationController.view.bounds; 
    CGFloat width = bounds.size.width; 
    CGFloat height = bounds.size.height; 
    CGFloat diameter = width - (INNER_EDGE_INSETS * 2); 
    CGFloat radius = diameter/2; 
    CGPoint center = CGPointMake(width/2, height/2); 

    // Create the image context 
    UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 0); 

    // Create the bezier paths 
    UIBezierPath *clipPath = [UIBezierPath bezierPathWithRect:bounds]; 
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(center.x - radius, center.y - radius, diameter, diameter)]; 

    [clipPath appendPath:maskPath]; 
    clipPath.usesEvenOddFillRule = YES; 

    [clipPath addClip]; 
    [[UIColor colorWithWhite:0 alpha:0.5f] setFill]; 
    [clipPath fill]; 

    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return finalImage; 
} 

Je crée essentiellement un sous-vue que j'ajouter au-dessus de l'image ScrollView, comme celui-ci :

UIImageView *maskView = [[UIImageView alloc] initWithImage:[self overlayMask]]; 
maskView.userInteractionEnabled = NO; 
[self.view insertSubview:maskView aboveSubview:_scrollView]; 

Espérons que ça aide.

(à l'origine dans DZNPhotoPickerController)

+0

Il fonctionne très bien! Merci Cyril tu es au top;) – Sam

+0

Merci Cyril, savez-vous comment puis-je créer un objet image avec ce qui est montré dans le cercle (dans le cadre d'une image plus grande qui se trouve derrière le masque)? – Sam

+0

Vous devriez jeter un oeil à l'implémentation de DZNPhotoPickerController dans la méthode '' 'editedImage''': https://github.com/dzenbot/DZNPhotoPickerController/blob/3bba17c70b29ca44313b30cff9b4eefe3bc82389/Source/Classes/Editor/DZNPhotoEditorViewController.m#L481 – Cyril

4

Réponse de Cyril traduit à rapide pour éviter typage supplémentaire:

func circularOverlayMask() -> UIImage { 
     let bounds = self.view.bounds 
     let width = bounds.size.width 
     let height = bounds.size.height 
     let diameter = width 
     let radius = diameter/2 
     let center = CGPointMake(width/2, height/2) 

     // Create the image context 
     UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0) 

     // Create the bezier paths 
     let clipPath = UIBezierPath(rect: bounds) 
     let maskPath = UIBezierPath(ovalInRect: CGRectMake(center.x - radius, center.y - radius, diameter, diameter)) 

     clipPath.appendPath(maskPath) 
     clipPath.usesEvenOddFillRule = true 

     clipPath.addClip() 
     UIColor(white: 0, alpha: 0.5).setFill() 
     clipPath.fill() 

     let finalImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() 

     return finalImage 
    }