我想開發一個應用程序,其中的項目應顯示爲UICollectionView
。當用戶敲擊&時,所有的項目都應該像蘋果主屏幕一樣搖晃,當我們想要刪除一個應用程序時,圖標開始抖動。所以請告訴我如何實現這個功能。有沒有圖書館可以使用?蘋果喜歡在iOS中的UICollectionView中晃動圖標?
0
A
回答
13
首先聲明變量:
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];
}
現在,您可以在您的.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;
}
相關問題
- 1. iOS - 蘋果是否會喜歡Firebase?
- 2. 喜歡在遊戲中的Uilabel效果
- 3. 安卓喜歡iOS中的權限
- 4. iOS喜歡在Android上過度滾動效果
- 5. Wpf動畫 - 就像在iOS上晃動圖標
- 6. 在Facebook上顯示喜歡的喜歡數量喜歡按鈕
- 7. GDI +喜歡在WPF中畫圖
- 8. iBooks庫喜歡的效果
- 9. CodeIgniter不喜歡視圖中的方法?
- 10. 邀請喜歡的人喜歡fanpage效果測量
- 11. AngularJS不喜歡的標籤
- 12. 蘋果的bug?沒有調用UICollectionView targetContentOffsetForProposedContentOffset
- 13. 用戶界面控制器喜歡在蘋果的書本應用程序?
- 14. SQL不喜歡和喜歡
- 15. mySQL喜歡,但不喜歡
- 16. 喜歡但不喜歡
- 17. 喜歡和不喜歡在同一列
- 18. iOS 11喜歡用uibutton作爲標題的大標題
- 19. 似乎無法讓我喜歡/不喜歡在PHP中工作
- 20. 在通用應用程序中使用@ 2x視網膜圖像爲ipad?蘋果是否喜歡原生應用?
- 21. UICollectionView中的動畫滾動 - iOS 10
- 22. 喜歡MySQL中的Concat
- 23. SQL:expading 「在(...)」 「喜歡」
- 24. 蘋果手錶的圖標在iPhone上
- 25. 動畫視圖喜歡這個例子
- 26. jQuery中的喜歡/不喜歡按鈕系統
- 27. 喜歡和喜歡的按鈕爲Drupal
- 28. 喜歡/不喜歡Firebase的交易?
- 29. 更改UICollectionView中標籤的顏色iOS
- 30. Can not Facebook「喜歡」圖形API的/ OBJECT_ID /喜歡
如何做一些編程? – matt
我不知道我如何編碼,所以這就是爲什麼我問它。 – Techiee