2015-11-03 51 views
0

我想開發一個應用程序,其中的項目應顯示爲UICollectionView。當用戶敲擊&時,所有的項目都應該像蘋果主屏幕一樣搖晃,當我們想要刪除一個應用程序時,圖標開始抖動。所以請告訴我如何實現這個功能。有沒有圖書館可以使用?蘋果喜歡在iOS中的UICollectionView中晃動圖標?

+5

如何做一些編程? – matt

+0

我不知道我如何編碼,所以這就是爲什麼我問它。 – Techiee

回答

13

首先聲明變量:

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

添加UIGestureRecognizerDelegate

.m文件,在viewDidLoad,加UILongPressGestureRecognizerUITapGestureRecognizercollectionView,因爲你想動搖細胞在長按:

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

} 

現在,您可以在您的.m文件執行handleLongPress:。當你長按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]; 

,給動畫細胞,其中已經使用QuartzCore框架CABasicAnimation挖掘。動畫時,請將您的_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; 

} 
+2

不要只發布一堆代碼。也添加一些解釋。 – rmaddy

+1

等待一個分鐘,編輯它 – Rumin

+0

變量聲明通常應該放在.m文件中,這樣如果將.h文件包含在多個文件中,最終不會重新聲明相同的變量。 – Caleb