Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 7th, 2012  |  syntax: None  |  size: 5.59 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Rapid user input messing up UIViewAnimation
  2. - (void)assetWasTapped:(CustomAsset*)tappedAsset
  3. {
  4.     // The asset thumbnail was tapped.  Check if the count is < 3 and add to the tray
  5.     if([selectedAssets count] < 3)
  6.     {
  7.         NSMutableDictionary *dataToAdd = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  8.                                           tappedAsset, @"selectedAsset",
  9.                                           [[tappedAsset.asset defaultRepresentation] url], @"selectedAssetURL",
  10.                                           tappedAsset.indexPath, @"selectedAssetIndexPath",
  11.                                           [NSNumber numberWithInt:[tappedAsset tag]], @"selectedAssetTag", nil];
  12.         [selectedAssets addObject:dataToAdd];
  13.         [self addAssetToSelectedTray:tappedAsset];
  14.     }
  15. }
  16.  
  17. - (void)addAssetToSelectedTray:(CustomAsset*)tappedAsset
  18. {
  19.     UIView *existingButton;
  20.     int xPos = 30;
  21.  
  22.     CustomAsset *tempCustomAsset = [[[CustomAsset alloc] initWithAsset:tappedAsset.asset] autorelease];
  23.  
  24.     // Switch to deal with 1~3 selected assets
  25.     switch ([selectedAssets count])
  26.     {  
  27.         case 1:
  28.             tempCustomAsset.frame = CGRectMake(125, 8, 73, 73);
  29.             [selectedPhotosTray addSubview:tempCustomAsset];
  30.             break;
  31.         case 2:
  32.             for(existingButton in [selectedPhotosTray subviews])
  33.             {
  34.  
  35.                 [UIView animateWithDuration:0.1
  36.                                       delay:0.0
  37.                                     options:UIViewAnimationOptionCurveEaseOut
  38.                                  animations:^{
  39.                                      existingButton.frame = CGRectMake(70, 8, 73, 73);
  40.                                  }
  41.  
  42.                                  completion:^(BOOL  completed){
  43.  
  44.                                  }
  45.                  ];
  46.             }
  47.             tempCustomAsset.frame = CGRectMake(180, 8, 73, 73);
  48.             [selectedPhotosTray addSubview:tempCustomAsset];
  49.             break;
  50.         case 3:
  51.             for(existingButton in [selectedPhotosTray subviews])
  52.             {
  53.  
  54.                 [UIView animateWithDuration:0.1
  55.                                       delay:0.0
  56.                                     options:UIViewAnimationOptionCurveEaseOut
  57.                                  animations:^{
  58.                                      existingButton.frame = CGRectMake(xPos, 8, 73, 73);
  59.                                  }
  60.  
  61.                                  completion:^(BOOL  completed){
  62.  
  63.                                  }
  64.                  ];
  65.                 xPos += 95;
  66.             }
  67.  
  68.             tempCustomAsset.frame = CGRectMake(220, 8, 73, 73);
  69.             [selectedPhotosTray addSubview:tempCustomAsset];
  70.             break;
  71.         default:
  72.             break;
  73.     }
  74. }
  75.  
  76. - (void)removeAssetFromSelectedTray:(CustomAsset*)tappedAsset
  77. {
  78.     // If the asset was removed, remove from the tray
  79.     [UIView animateWithDuration:0.2
  80.                           delay:0.0
  81.                         options:UIViewAnimationOptionCurveEaseInOut
  82.                      animations:^{
  83.                          tappedAsset.transform = CGAffineTransformMakeScale(0.3, 0.3);
  84.                          tappedAsset.alpha = 0.0;
  85.                      }
  86.  
  87.                      completion:^(BOOL  completed){
  88.                          [tappedAsset removeFromSuperview];
  89.  
  90.                          if([selectedAssets count] == 0)
  91.                          {
  92.                              [self closeSelectedPhotosTrayWithAnimation:YES];
  93.                          }
  94.  
  95.                          int xPos = 70;
  96.                          UIButton *existingButton;
  97.                          switch ([selectedAssets count])
  98.                          {
  99.                              case 1:
  100.                                  for(existingButton in [selectedPhotosTray subviews])
  101.                                  {
  102.                                      [UIView animateWithDuration:0.1
  103.                                                            delay:0.0
  104.                                                          options:UIViewAnimationOptionCurveEaseOut
  105.                                                       animations:^{
  106.                                                           existingButton.frame = CGRectMake(125, 8, 73, 73);
  107.                                                       }
  108.  
  109.                                                       completion:^(BOOL  completed){
  110.  
  111.                                                       }
  112.                                       ];
  113.                                  }
  114.                                  break;
  115.                              case 2:
  116.  
  117.                                  for(existingButton in [selectedPhotosTray subviews])
  118.                                  {
  119.                                      [UIView animateWithDuration:0.1
  120.                                                            delay:0.0
  121.                                                          options:UIViewAnimationOptionCurveEaseOut
  122.                                                       animations:^{
  123.                                                           existingButton.frame = CGRectMake(xPos, 8, 73, 73);
  124.                                                       }
  125.                                                       completion:^(BOOL  completed){
  126.                                                       }
  127.                                       ];
  128.  
  129.                                      xPos += 110;
  130.                                  }            
  131.                                  break;            
  132.                              default:
  133.  
  134.                                  break;
  135.                          }
  136.                      }
  137.      ];
  138. }