简体   繁体   中英

Animate all cells of UICollectionView like springboard in iOS?

I want to add iOS spring board animation to all of my cells.I have add the below code by which i am able to animate only a single cell but i am not able to animate all of the cells.Please how will it work.

I have tried this.

Define Variables

 UIButton* _deleteButton;
 CGPoint p;
 UILongPressGestureRecognizer *lpgr;

Add gestures

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getUserAlbums];
    arr_userAlbums=[[NSMutableArray alloc]init];

    //afdd gesture code
    lpgr
    = [[UILongPressGestureRecognizer alloc]
       initWithTarget:self action:@selector(handleLongPress:)];
    lpgr.minimumPressDuration = .3; // To detect after how many seconds you want shake the cells
    lpgr.delegate = self;
    [self.collection_view addGestureRecognizer:lpgr];

    lpgr.delaysTouchesBegan = YES;
    // Do any additional setup after loading the view.
}

Add gestures:

- (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
        return;
    }
    p = [gestureRecognizer locationInView:self.collection_view]; // Store (x,y) co-ordinate where the user has tapped the cell in point p.

    NSIndexPath *indexPath = [self.collection_view indexPathForItemAtPoint:p];
    if (indexPath == nil)
    {
        NSLog(@"couldn't find index path");
    }
    else
    {
        // get the cell at indexPath (the one you long pressed)
        UICollectionViewCell* cell =
        [self.collection_view cellForItemAtIndexPath:indexPath];
        // do stuff with the cell

        CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        [anim setToValue:[NSNumber numberWithFloat:0.0f]];
        [anim setFromValue:[NSNumber numberWithDouble:M_PI/43]];
        [anim setDuration:0.1];
        [anim setRepeatCount:NSUIntegerMax];
        [anim setAutoreverses:YES];
        cell.layer.shouldRasterize = YES;
        [cell.layer addAnimation:anim forKey:@"SpringboardShake"];
        //_mainImage.userInteractionEnabled = NO;
        //[cell bringSubviewToFront:_deleteButton];

        CGFloat delButtonSize = 20;

        _deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 15, delButtonSize, delButtonSize)];
        _deleteButton.center = CGPointMake(0, 0);
        _deleteButton.backgroundColor = [UIColor clearColor];

        [_deleteButton setImage: [UIImage imageNamed:@"cross_30.png"] forState:UIControlStateNormal];
        [cell addSubview:_deleteButton];


        [_deleteButton addTarget:self action:@selector(deleteyourItem) forControlEvents:UIControlEventTouchUpInside];

        [[NSUserDefaults standardUserDefaults]setValue:@"yes" forKey:@"longPressed"];
    }
}

Method to delete items

-(void)deleteyourItem
{
    NSIndexPath *indexPath = [self.collection_view indexPathForItemAtPoint:p];
    //delete your item based on the `indexpath` from your collectionViewArray here.
    //OR If you are accessing the database to display the collectionView, you can compare the value fetched based on the `indexPath`, with your database value and then delete it.

    // Reload your collectionView after deletion
}

Easiest way:

Put all this animation code into a method in your UICollectionViewCell subclass. Let's say the method is named - (void)shakeItem:(BOOL)shake .

On handleLongPress you should iterate on collectionView.indexPathsForVisibleItems , on these cells you should call method shakeItem: .

Keep in mind, you need to override prepareForReuse method and cancel the shaking effect there. You also need to save that the items are shaking and begin shaking new dequeued cells.

Another way is to follow any tutorial (there are quite a lot of them): http://code.tutsplus.com/tutorials/a-springboard-like-layout-with-the-uicollectionview-class--mobile-13426 but it won't use your code ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM