2013-05-19 4 views
5

sinistra/Ho un scorrimento orizzontale UICollectionView all'interno della visualizzazione principale di un controller della vista in questo modo (Gray è UIView, legno è UICollectionView):Objective C - Faded Pendenza sui lati destro UICollectionView

Desidero aggiungere sfumature sbiadite fisse all'estrema sinistra & all'estrema destra di questo UICollectionView in modo che le pergamene sembrino scomparire mentre l'utente scorre. Come potrei fare questo? Coinvolge qualche uso di CAGradientLayer? Sarei grato per qualsiasi aiuto tu possa darmi!

risposta

1

tenta di aggiungere due strati di CAGradientLayer sul substrato visualizzazione dell'insieme:

#import <QuartzCore/QuartzCore.h> 

CAGradientLayer *leftShadow = [CAGradientLayer layer]; 
leftShadow.frame = CGRectMake(0, 0, 100, self.collectionView.frame.size.height); 
leftShadow.startPoint = CGPointMake(0, 0.5); 
leftShadow.endPoint = CGPointMake(1.0, 0.5); 
leftShadow.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.0 alpha:0.4f] CGColor], (id)[[UIColor clearColor] CGColor], nil]; 

[self.collectionView.layer addSublayer:leftShadow]; 

CAGradientLayer *rightShadow = [CAGradientLayer layer]; 
rightShadow.frame = CGRectMake(CGRectGetWidth(self.collectionView.frame)-100.0, 0, 100, self.collectionView.frame.size.height); 
rightShadow.startPoint = CGPointMake(1.0, 0.5); 
rightShadow.endPoint = CGPointMake(0, 0.5); 
rightShadow.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.0 alpha:0.4f] CGColor], (id)[[UIColor clearColor] CGColor], nil]; 

[self.collectionView.layer addSublayer:rightShadow]; 
8

ho effettivamente riuscito a capire questo utilizza uno strato di maschera grazie a this tutorial at cocoanetics. Ecco quello che ho fatto:

@interface ScalesViewController : UIViewController 
{ 
    CAGradientLayer *maskLayer; 
} 
@end 

Poi nel .m, ho messo il seguente:

- (void)viewDidAppear:(BOOL)animated 
{ 

    [super viewWillAppear: animated]; 

    if (!maskLayer) 
    { 
     maskLayer = [CAGradientLayer layer]; 

     CGColorRef outerColor = [[UIColor colorWithWhite:0.0 alpha:1.0] CGColor]; 
     CGColorRef innerColor = [[UIColor colorWithWhite:0.0 alpha:0.0] CGColor]; 

     maskLayer.colors = [NSArray arrayWithObjects: 
          (__bridge id)outerColor, 
          (__bridge id)innerColor, 
          (__bridge id)innerColor, 
          (__bridge id)outerColor, nil]; 

     maskLayer.locations = [NSArray arrayWithObjects: 
           [NSNumber numberWithFloat:0.0], 
           [NSNumber numberWithFloat:0.125], 
           [NSNumber numberWithFloat:0.875], 
           [NSNumber numberWithFloat:1.0], nil]; 

     [maskLayer setStartPoint:CGPointMake(0, 0.5)]; 
     [maskLayer setEndPoint:CGPointMake(1, 0.5)]; 

     maskLayer.bounds = self.mainCollectionView.bounds; 
     maskLayer.anchorPoint = CGPointZero; 

     [self.mainCollectionView.layer insertSublayer: maskLayer atIndex: 0]; 
    } 
} 

Questo crea una bella "Fade to Black" effetto su entrambi i lati della mia vista collezione. È possibile aggiungere più colori alle proprietà dei colori & per perfezionare la sfumatura. Gli inizi/punti finali determinano la direzione e la posizione del gradiente.