Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to fadeIn the selected cell of uicollection view in objective C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 681
    Comment on it

    If we want to fadeIn the selected cell of collection view then use the following code-

    Sometimes we need to show the selected cell . For example - If we have list of songs and we want to display which song is currrently playing then FadeIn method can be useful.

    In this code first we need to get the indexpath of selected cell and then we can get the cell from indexpath.

    After that we initialize cagradientlayer and set frame and color of gradientlayer then we mask that layer on the content view of cell.

    Use this method in -didSelectItemAtIndexPath.

     

    Here is the code-

     

    -(void)setfadeInCell{
        
    // here selectedIndex is the index path of selected cell.
        NSIndexPath *iPath = [NSIndexPath indexPathForItem:self.selectedIndex inSection:0];
    
    //get the cell from index path
        UICollectionViewCell *cell1=(UICollectionViewCell *)[self.collectionViewVideo cellForItemAtIndexPath:iPath];
    
    //scroll the collection view to the selected row
        [self.collectionViewVideo scrollToItemAtIndexPath:iPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
    
    // initialize the gradient layer
        CAGradientLayer *gradient = [CAGradientLayer layer];
    //set frame of gradient layer
        gradient.frame = cell1.contentView.bounds;
    //chnage the color of mask here
        gradient.colors = @[(id)[UIColor clearColor].CGColor, (id)[UIColor blackColor].CGColor];
        gradient.locations = @[@0.0, @(1.5)];
        cell1.layer.mask = gradient;
    
    //optional-assign tag to the cell and set interaction
        [cell1 setTag:9999];
        [cell1 setUserInteractionEnabled:NO];
    
    
    }

    Now if you want to remove this after selecting the new cell then use the following code in -didSelectItemAtIndexPath -

     

        [collectionView viewWithTag:9999].layer.mask=nil;
        [[collectionView viewWithTag:9999]setUserInteractionEnabled:YES];
        [[collectionView viewWithTag:9999] setTag:0];
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: