动画UICollectionView的所有单元格如iOS中的跳板?

时间:2015-11-06 13:01:49

标签: ios objective-c iphone ipad animation

我想将iOS弹簧板动画添加到我的所有单元格中。我添加了下面的代码,通过它我只能动画一个单元格,但我无法为所有单元格设置动画。请问它将如何工作

我试过这个。

定义变量

 UIButton* _deleteButton;
 CGPoint p;
 UILongPressGestureRecognizer *lpgr;

添加手势

- (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.
}

添加手势

- (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"];
    }
}

删除项目的方法

-(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
}

1 个答案:

答案 0 :(得分:0)

最简单的方法:

将所有动画代码放入UICollectionViewCell子类中的方法中。假设该方法名为- (void)shakeItem:(BOOL)shake

handleLongPress上,您应该在collectionView.indexPathsForVisibleItems上进行迭代,在这些单元格上,您应该调用方法shakeItem:

请注意,您需要覆盖prepareForReuse方法并取消其中的摇晃效果。您还需要保存物品抖动并开始摇动新出列的细胞。

另一种方法是遵循任何教程(有很多教程):http://code.tutsplus.com/tutorials/a-springboard-like-layout-with-the-uicollectionview-class--mobile-13426但它不会使用你的代码;)