首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UICollectionView的所有细胞都像iOS中的跳板一样充满活力?

UICollectionView的所有细胞都像iOS中的跳板一样充满活力?
EN

Stack Overflow用户
提问于 2015-11-06 13:01:48
回答 1查看 851关注 0票数 1

我想把iOS弹簧板动画添加到我的所有单元中。我已经添加了下面的代码,通过它我只能动画一个单元,但我不能动画所有的cells.Please将如何工作。

我试过这个。

定义变量

代码语言:javascript
复制
 UIButton* _deleteButton;
 CGPoint p;
 UILongPressGestureRecognizer *lpgr;

添加手势

代码语言:javascript
复制
- (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.
}

添加手势:

代码语言:javascript
复制
- (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"];
    }
}

删除项的方法

代码语言:javascript
复制
-(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
}
EN

回答 1

Stack Overflow用户

发布于 2015-11-06 13:32:34

最简单的方法:

将所有这些动画代码放入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,但是它不会使用您的代码;

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33567426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档