我正在做一个自定义ImagePicker,类似于平常的UIImagePickerController,但您可以点击选择最多3张照片。当您点击选择第一张照片时,一个小UIView托盘将从底部滑入,并且所选缩略图将出现在那里。当您点击选择第二张和第三张照片时,它们将被添加到选定的照片纸盒中。如果您在选取器中点击相同的照片,它将从托盘中移除。 (道歉在代码中的幻数;我还在测试的东西出来现在)快速用户输入搞乱UIViewAnimation
我们已经附上以下裁剪照片给的托盘的外观与正确选择3张照片的想法。如果这些照片是在一个正常的速度挖掘
http://dl.dropbox.com/u/762437/screen1.jpg
一切工作正常。但是,如果用户以某种方式决定以随机方式快速拍摄照片,它偶尔会混淆动画并将照片放置在托盘上。
我附在下面的例子。在下面的例子中,实际上只选择了2张照片,但不知何故,第3张照片仍然部分可见。
http://dl.dropbox.com/u/762437/screen2.jpg
我不知道如何来缓解这种行为,当用户决定只是“混搭按钮”,可以这么说。我认为也许它可能是好的使用@synchronized(self)并包装assetsWasTapped函数的内部,但它似乎没有做任何事情(assetsWasTapped从主线程调用)。
有时,我可能会看到两张照片被添加到托盘中的相同位置,这使我认为我的NSMutableArray计数(selectedPhotos)存在一些计时问题,我用它来确定每个缩略图应该放在哪里。
我的具体问题道歉,但也许它的更一般的版本将如何处理与动画和快速的用户输入。
任何帮助将是非常非常赞赏。谢谢!
- (void)assetWasTapped:(CustomAsset*)tappedAsset
{
// The asset thumbnail was tapped. Check if the count is < 3 and add to the tray
if([selectedAssets count] < 3)
{
NSMutableDictionary *dataToAdd = [NSMutableDictionary dictionaryWithObjectsAndKeys:
tappedAsset, @"selectedAsset",
[[tappedAsset.asset defaultRepresentation] url], @"selectedAssetURL",
tappedAsset.indexPath, @"selectedAssetIndexPath",
[NSNumber numberWithInt:[tappedAsset tag]], @"selectedAssetTag", nil];
[selectedAssets addObject:dataToAdd];
[self addAssetToSelectedTray:tappedAsset];
}
}
- (void)addAssetToSelectedTray:(CustomAsset*)tappedAsset
{
UIView *existingButton;
int xPos = 30;
CustomAsset *tempCustomAsset = [[[CustomAsset alloc] initWithAsset:tappedAsset.asset] autorelease];
// Switch to deal with 1~3 selected assets
switch ([selectedAssets count])
{
case 1:
tempCustomAsset.frame = CGRectMake(125, 8, 73, 73);
[selectedPhotosTray addSubview:tempCustomAsset];
break;
case 2:
for(existingButton in [selectedPhotosTray subviews])
{
[UIView animateWithDuration:0.1
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
existingButton.frame = CGRectMake(70, 8, 73, 73);
}
completion:^(BOOL completed){
}
];
}
tempCustomAsset.frame = CGRectMake(180, 8, 73, 73);
[selectedPhotosTray addSubview:tempCustomAsset];
break;
case 3:
for(existingButton in [selectedPhotosTray subviews])
{
[UIView animateWithDuration:0.1
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
existingButton.frame = CGRectMake(xPos, 8, 73, 73);
}
completion:^(BOOL completed){
}
];
xPos += 95;
}
tempCustomAsset.frame = CGRectMake(220, 8, 73, 73);
[selectedPhotosTray addSubview:tempCustomAsset];
break;
default:
break;
}
}
- (void)removeAssetFromSelectedTray:(CustomAsset*)tappedAsset
{
// If the asset was removed, remove from the tray
[UIView animateWithDuration:0.2
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
tappedAsset.transform = CGAffineTransformMakeScale(0.3, 0.3);
tappedAsset.alpha = 0.0;
}
completion:^(BOOL completed){
[tappedAsset removeFromSuperview];
if([selectedAssets count] == 0)
{
[self closeSelectedPhotosTrayWithAnimation:YES];
}
int xPos = 70;
UIButton *existingButton;
switch ([selectedAssets count])
{
case 1:
for(existingButton in [selectedPhotosTray subviews])
{
[UIView animateWithDuration:0.1
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
existingButton.frame = CGRectMake(125, 8, 73, 73);
}
completion:^(BOOL completed){
}
];
}
break;
case 2:
for(existingButton in [selectedPhotosTray subviews])
{
[UIView animateWithDuration:0.1
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
existingButton.frame = CGRectMake(xPos, 8, 73, 73);
}
completion:^(BOOL completed){
}
];
xPos += 110;
}
break;
default:
break;
}
}
];
}