Apple喜欢在iOS中的UICollectionView中摇动图标吗?

时间:2015-11-03 04:25:17

标签: ios objective-c iphone ipad

我想开发一个应用程序,其中的项目应显示为UICollectionView。当用户点击&当我们要删除应用程序时,保持所有项目应该像Apple主屏幕一样开始摇动,其中图标开始摇动。所以请告诉我如何实现此功能。有没有图书馆可以使用?

1 个答案:

答案 0 :(得分:12)

首先声明你的变量:

UIButton* _deleteButton;
CGPoint p;  // It is a point which will give you which cell has been selected.

添加UIGestureRecognizerDelegate

.m文件的viewDidLoad中,将UILongPressGestureRecognizerUITapGestureRecognizer添加到collectionView,因为您希望长按动作细胞:< / p>

- (void)viewDidLoad {
    [super viewDidLoad];

    // Add gesture recognizer to your collection view cell
    UILongPressGestureRecognizer *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.collectionView addGestureRecognizer:lpgr];

    lpgr.delaysTouchesBegan = YES;

    /// This will be helpful to restore the animation when clicked outside the cell
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
    initWithTarget:self action:@selector(handleTap:)];
    //lpgr.minimumPressDuration = .3; //seconds
    tap.delegate = self;
    [self.collectionView addGestureRecognizer:tap];

}

您现在可以在handleLongPress:文件中实施.m。当您长按collectionViewCell时,您将得到(x,y)用户按下单元格的坐标,我们将在p点存储。

基于这一点,您将能够获取相应单元格的相应indexPath

p = [gestureRecognizer locationInView:self.collectionView]; // Store (x,y) co-ordinate where the user has tapped the cell in point p.

NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];

现在,为使用CABasicAnimation QuartzCore框架点击的单元格提供动画。在制作动画时,将_deleteButton抬起以使其可见。

使用handleTap:,您可以在collectionViewCell之外点击动画时恢复动画。

-(void)handleTap:(UITapGestureRecognizer *)gestureRecognizer
{
    NSLog(@"singleTap");
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
        return;
    }
    p = [gestureRecognizer locationInView:self.collectionView];

    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
    if (indexPath == nil){
        NSLog(@"couldn't find index path");
        [[NSUserDefaults standardUserDefaults]setValue:@"no" forKey:@"longPressed"];
        [[NSUserDefaults standardUserDefaults]setValue:@"yes" forKey:@"singleTap"];
        //_deleteButton = [[UIButton alloc] initWithFrame:CGRectZero];
        //[cell addSubview:_deleteButton];
        //[_deleteButton removeFromSuperview];
        [self.collectionView reloadData];

    } else {

    }

}
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
        return;
    }
    p = [gestureRecognizer locationInView:self.collectionView];

    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
    if (indexPath == nil){
        NSLog(@"couldn't find index path");
    } else {

        [[NSUserDefaults standardUserDefaults]setValue:@"yes" forKey:@"longPressed"];
        [self.collectionView reloadData];

    }
}

根据所选项目,删除相应的项目。

-(void)deleteyourItem
{
    NSIndexPath *indexPath = [self.collectionView 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
}

重新加载集合视图后,cellForItemAtIndexPath:将如下所示:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {


    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"favoritecell" forIndexPath:indexPath];
   // UIImageView *img=[[UIImageView alloc]init];

    cell.backgroundColor = [UIColor colorWithRed:251.0/255.0 green:144.0/255.0 blue:13.0/255.0 alpha:1.0];


    //img.image = [UIImage imageNamed:@""];

    NSLog(@"%d",indexPath.row);


    if([[[NSUserDefaults standardUserDefaults]valueForKey:@"longPressed"] isEqualToString:@"yes"])
    {
        CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        [anim setToValue:[NSNumber numberWithFloat:0.0f]];
        [anim setFromValue:[NSNumber numberWithDouble:M_PI/64]];
        [anim setDuration:0.1];
        [anim setRepeatCount:NSUIntegerMax];
        [anim setAutoreverses:YES];
        cell.layer.shouldRasterize = YES;
        [cell.layer addAnimation:anim forKey:@"SpringboardShake"];
        CGFloat delButtonSize = 75;

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

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

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

    }

    else if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"singleTap"] isEqualToString:@"yes"])
    {

        for(UIView *subview in [cell subviews]) {
            if([subview isKindOfClass:[UIButton class]]) {
                [subview removeFromSuperview];
            } else {
                // Do nothing - not a UIButton or subclass instance
            }
        }
            [cell.layer removeAllAnimations];
//            _deleteButton.hidden = YES;
//            [_deleteButton removeFromSuperview];


    }

    //251, 144 , 13


    return cell;

}