Closed. This question is off-topic。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
4年前关闭。
我想开发一个应用程序,其中项目应显示为
添加
在
现在,您可以在
基于这一点,您将能够获取相应单元格的相应
现在,给已使用
使用
根据所选项目,删除适当的项目。
重新加载收集视图后,
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
4年前关闭。
我想开发一个应用程序,其中项目应显示为
UICollectionView
。当用户点击并按住项目时,所有项目都应开始摇动,就像Apple主屏幕上一样,当我们要删除应用程序时,图标开始摇动。因此,请告诉我如何实现此功能。有图书馆可以使用吗? 最佳答案
首先声明变量:
UIButton* _deleteButton;
CGPoint p; // It is a point which will give you which cell has been selected.
添加
UIGestureRecognizerDelegate
。在
.m
文件中的viewDidLoad
中,将UILongPressGestureRecognizer
和UITapGestureRecognizer
添加到collectionView
中,因为要长按摇动单元格:- (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;
}
关于ios - 苹果喜欢在iOS的UICollectionView中摇动图标吗? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33491376/
10-08 21:06