Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I want to develop an app in which the items should be displayed as the UICollectionView. When user tap & hold the items all the items should start shaking like the Apple home screen on which icons start to shake when we want to delete an app. So please tell me how can I implement this feature. Is there any library can use?
Declare your variables first:
UIButton* _deleteButton;
CGPoint p; // It is a point which will give you which cell has been selected.
Add UIGestureRecognizerDelegate.
In .m file, in viewDidLoad, add UILongPressGestureRecognizer and UITapGestureRecognizer to your collectionView, since you want to shake the cells at long press:
- (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];
}
You can now implement handleLongPress: in your .m file. When you will long press your collectionViewCell, you will get (x,y) co-ordinates of where the user has pressed the cell, which we will store in point p.
Based on that point, you will be able to fetch the appropriate indexPath of the appropriate cell.
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];
Now, give animation to the cell, which has been tapped using CABasicAnimation of QuartzCore framework. At the time of animation, bring your _deleteButton up so that it is visible.
With handleTap:, you will then be able to restore animation when clicked outside of the 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];
}
}
Based on the selected item, delete the appropriate item.
-(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
}
After you reload the collection view, cellForItemAtIndexPath:, will look like this:
-(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;
}
Related
I have created a UICollectionView view in which i am giving effects like spring board of iOS. When i long press my UICollectionView then they start animating & it works fine. Now when i have long pressed the cell & animation is started then i want to move the cells on diffrent position & keep it there until next time i change it. Please tell me how i can do this?
Here's my code for animation of cell
- (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
return;
}
p = [gestureRecognizer locationInView:self.collection_view];
NSIndexPath *indexPath = [self.collection_view indexPathForItemAtPoint:p];
if (indexPath == nil) {
NSLog(#"couldn't find index path");
}
else {
[[NSUserDefaults standardUserDefaults]setValue:#"yes" forKey:#"longPressed"];
[self.collection_view reloadData];
}
}
CellForItem
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"arr_album index row");
BlogAlbumCell *cell;
static NSString *identifier = #"UserBlogAlbum";
cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UserAlbum *user_allbum=[arr_userAlbums objectAtIndex:indexPath.row];
cell.label_blog_name.text=user_allbum.album_name;
cell.image_blog_image.image = [UIImage imageNamed:#"more.png"];
[cell.image_blog_image setImageWithURL:[NSURL URLWithString:[IMAGE_BASE_URL stringByAppendingString:user_allbum.album_image]]];
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/50]];
[anim setDuration:0.1];
[anim setRepeatCount:NSUIntegerMax];
[anim setAutoreverses:YES];
cell.layer.shouldRasterize = YES;
[cell.layer addAnimation:anim forKey:#"SpringboardShake"];
CGFloat delButtonSize = 20;
UIButton *delButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, delButtonSize, delButtonSize)];
delButton.center = CGPointMake(9, 10);
delButton.backgroundColor = [UIColor clearColor];
[delButton setImage: [UIImage imageNamed:#"cross_30.png"] forState:UIControlStateNormal];
[cell addSubview:delButton];
[delButton 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];
}
return cell;
}
Take a look at this drag and drop collectionview project.
https://github.com/lxcid/LXReorderableCollectionViewFlowLayout
I am using ABMenuTableViewCell tableview controller in my application. I want to call didSelectRowAtIndexPath when i swipe a UITableViewCell.
Right now didSelectRowAtIndexPath only execute when I tap on a cell, I want to call it even when I swipe it. here is my didSelectRowAtIndexPath and cellforRowAtIndexPath methods code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *likes;
UILabel *downloads;
static NSString *CellIdentifier = #"Cell";
ABMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[ABMenuTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"acc_arrow_back.png"]];
arrow.frame = CGRectMake(300, 50, 5, 12);
arrow.image = [arrow.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[arrow setTintColor:[UIColor colorWithRed:(191/255.0) green:(2/255.0) blue:(6/255.0) alpha:1]];
[cell.contentView addSubview:arrow];
UIImageView *likes_img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"social.png"]];
likes_img.frame = CGRectMake(15, 80, 15, 15);
likes_img.image = [likes_img.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[likes_img setTintColor:[UIColor colorWithRed:(191/255.0) green:(2/255.0) blue:(6/255.0) alpha:1]];
[cell.contentView addSubview:likes_img];
likes =[[UILabel alloc]initWithFrame:CGRectMake(33, 78, 80, 20)];
likes.tag = 1001; // set a tag for this View so you can get at it later
likes.textColor=[UIColor darkGrayColor];
likes.font=[UIFont fontWithName:#"Helvetica" size:10.0f];
likes.text=[[rssOutputData objectAtIndex:indexPath.row]xmllikes];
[cell.contentView addSubview:likes];
cell.detailTextLabel.textColor = [UIColor darkGrayColor];
UIImageView *downloads_img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"download.png"]];
downloads_img.frame = CGRectMake(55, 79, 15, 15);
downloads_img.image = [downloads_img.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[downloads_img setTintColor:[UIColor colorWithRed:(191/255.0) green:(2/255.0) blue:(6/255.0) alpha:1]];
[cell.contentView addSubview:downloads_img];
downloads =[[UILabel alloc]initWithFrame:CGRectMake(73, 78, 80, 20)];
downloads.tag = 1002; // set a tag for this View so you can get at it later
downloads.textColor=[UIColor darkGrayColor];
downloads.font=[UIFont fontWithName:#"Helvetica" size:10.0f];
downloads.text=[[rssOutputData objectAtIndex:indexPath.row]xmldownloads];
[cell.contentView addSubview:downloads];
cell.detailTextLabel.textColor = [UIColor darkGrayColor];
}
else
{
// use viewWithTag to find lblNombre in the re-usable cell.contentView
likes = (UILabel *)[cell.contentView viewWithTag:1001];
downloads = (UILabel *)[cell.contentView viewWithTag:1002];
}
cell.textLabel.text = [[rssOutputData objectAtIndex:indexPath.row]xmlsinger];
cell.detailTextLabel.text = [[rssOutputData objectAtIndex:indexPath.row]xmltitle];
// custom menu view
NSString *nibName = #"ABCellMailStyleMenuView";
ABCellMenuView *menuView = [ABCellMenuView initWithNib:nibName bundle:nil];
menuView.delegate = self;
menuView.indexPath = indexPath;
cell.rightMenuView = menuView;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
And these are the methods in cell class
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer and
- (void)swipeGesture:(UIPanGestureRecognizer *)gesture
here is when i swipe
You can modified swipeGesture method will following code. it will show UITableViewCell as selected once you perform swipe operation.
- (void)swipeGesture:(UIPanGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateBegan) {
NSInteger direction;
// find swipe direction
CGPoint velocity = [gesture velocityInView:self];
if (velocity.x > 0) {
// towards right - hide menu view
direction = ABMenuUpdateHideAction;
}
else {
// towards left - show menu view
direction = ABMenuUpdateShowAction;
}
UITableView* tableView = (UITableView*)self.superview.superview;
CGPoint swipeLocation = [gesture locationInView:tableView];
NSIndexPath *swipedIndexPath = [tableView indexPathForRowAtPoint:swipeLocation];
[tableView selectRowAtIndexPath:swipedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[self updateMenuView:direction animated:YES];
}
}
Hope this help you.
As you are trying to achieve behaviour not supported by ABMenuTableViewCell, you will need to edit it's source:
Add _tapGesture and _menuVisible instance variables:
#implementation ABMenuTableViewCell {
CGRect _rightMenuViewInitialFrame;
UIPanGestureRecognizer *_swipeGesture;
UITapGestureRecognizer *_tapGesture;
BOOL _menuVisible;
}
Implement -tapGesture: method:
- (void) tapGesture:(UITapGestureRecognizer*)gesture {
if (_menuVisible)
[self updateMenuView:ABMenuUpdateHideAction animated:YES];
else
[self updateMenuView:ABMenuUpdateShowAction animated:YES];
}
Add UITapGestureRecognizer inside -commonInit method:
- (void) commonInit {
_swipeGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGesture:)];
_swipeGesture.delegate = self;
[self addGestureRecognizer:_swipeGesture];
_tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesture:)];
_tapGesture.delegate = self;
[self addGestureRecognizer:_tapGesture];
}
Update _menuVisible inside -updateMenuView:animated::
- (void)updateMenuView:(ABMenuUpdateAction)action animated:(BOOL)animated {
...
switch (action) {
case ABMenuUpdateShowAction:
menuNewFrame = CGRectMake(CGRectGetWidth(self.contentView.frame) - initialWidth, .0, initialWidth, CGRectGetHeight(self.contentView.frame));
_menuVisible = YES;
break;
case ABMenuUpdateHideAction:
menuNewFrame = CGRectMake(CGRectGetWidth(self.contentView.frame), .0, .0, CGRectGetHeight(self.contentView.frame));
_menuVisible = NO;
break;
default:
break;
}
...
}
You won't be able to select cells, but as I understand it, you don't want to.
If you want the method to be called when swiping the cell, you should be using a UISwipeGestureRecognizer and not a UIPanGestureRecognizer.
In your viewDidLoad:, set up the gesture recognizer.
// Create a left swipe gesture recognizer
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeLeft:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
// Add it to the table view
[self.tableView addGestureRecognizer:recognizer];
Handle the swipe.
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
// Get location of the swipe
CGPoint location = [gestureRecognizer locationInView:self.tableView];
//Get the corresponding index path within the table view
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
// Check if index path is valid
if (indexPath)
{
// Select the cell at the indexPath
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
That should call the tableView: didSelectRowAtIndexPath: method.
Wondering how can I get all items of each cell in UITableView. My problem is that I want to hide 2 buttons in cell when third is selected.
When I press on button 1, button 2 and 3 must be hidden. What I tried to do for that (in cellForRowAtIndexPath) :
AVMMovieButton *settings = (AVMMovieButton *)[cell viewWithTag:228];
[settings addTarget:self action:#selector(selectSettings:) forControlEvents:UIControlEventTouchUpInside];
settings.tag = indexPath.row;
AVMMovieButton *playButton = (AVMMovieButton *)[cell viewWithTag:134];
[playButton setStore:oneItem];
[playButton addTarget:self action:#selector(playMovie:) forControlEvents:UIControlEventTouchUpInside];
playButton.tag = indexPath.row;
AVMMovieButton *down = (AVMMovieButton *)[cell viewWithTag:282];
AVMMovieButton *del = (AVMMovieButton *)[cell viewWithTag:161];
[settings setSelected:!settings.isSelected];
if (settings.isSelected)
{
NSLog(#"SELECTED!");
down.hidden = YES;
del.hidden = YES;
downLabel.hidden = YES;
delLabel.hidden = YES;
// [self performSelector:#selector(playButtonShow) withObject:nil afterDelay:0.3];
playButton.hidden = NO;
}
else
{
NSLog(#"UNSELECTED!");
playButton.hidden = YES;
down.hidden = NO;
del.hidden = NO;
downLabel.hidden = NO;
delLabel.hidden = NO;
NSLog(#"play button %d",playButton.hidden);
}
and then I added method for selecting my "settings" button:
-(void)selectSettings:(AVMMovieButton *)sender
{
[sender setSelected:!sender.isSelected];
NSLog(#"you just select button");
}
but it doesn't work!
Actually NSLog(#"you just select button"); works, but buttons never hides.
What should I do to get my buttons and hide them?
SOLVED:
All I needed to do was to create custom UITableViewCell class and then access my cell as Jay Gajjar and Akhilrajtr said. After I just used my method for select/deselect my button.
What I've got:
-(void)selectSettings:(AVMMovieButton *)sender
{
AVMMovieButton *settings = (AVMMovieButton *)sender;
CGPoint pointInTable = [settings convertPoint:settings.bounds.origin toView:readyTable];
NSIndexPath *indexPath = [readyTable indexPathForRowAtPoint:pointInTable];
AVMMovieCell *cell=(AVMMovieCell *)[readyTable cellForRowAtIndexPath:indexPath];
if (cell.settingsButton.isSelected)
{
NSLog(#"SELECTED!");
cell.downloadButton.hidden = YES;
cell.deleteButton.hidden = YES;
cell.downLabel.hidden = YES;
cell.delLabel.hidden = YES;
cell.playButton.hidden = NO;
}
else
{
NSLog(#"UNSELECTED!");
cell.playButton.hidden = YES;
cell.downloadButton.hidden = NO;
cell.deleteButton.hidden = NO;
cell.downLabel.hidden = NO;
cell.delLabel.hidden = NO;
NSLog(#"play button %d",cell.playButton.hidden);
}
[settings setSelected:!settings.isSelected];
}
Hope this can be helpful for somebody else!
Try this,
-(void)selectSettings:(AVMMovieButton *)sender{
[sender setSelected:!sender.isSelected];
CGPoint pointInTable = [sender convertPoint:sender.bounds.origin toView:_tableView];
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:pointInTable];
[_tableView beginUpdates];
[_tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[_tableView endUpdates];
NSLog(#"you just select button");
}
cellForRowAtIndexPath is called when Table get reloaded. In your case, what you have to do is:
- (void)button3Method:(id)sender
{
// Begin UITableView Update
[self.tableView beginUpdates];
NSIndexPath *indexPath = // Get cell indexpath for the selected row.
// Logic for getting the UITableViewCell on which you want hide two buttons.
// Logic for hiding those two buttons
// Below line performs reloading of particular UITableViewCell.
[self.tableView reloadRowsAtIndexPaths:#[indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
// End UITaleView Update
[self.tableView endUpdates];
}
Let me know, if this works for you. Good luck.
In cellForRowAtIndexPath:
[cell. settings addTarget:self action:#selector(selectSettings:) forControlEvents:UIControlEventTouchUpInside];
cell. settings.tag=600+indexPath.row;
In button IBAction:
-(void) selectSettings:(id) sender{
UIButton *btn=(UIButton *)sender;
cellMoreButtonIndex=btn.tag;
YOURCELL *cell=(YOURCELL *)[self.contentTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:btn.tag-600 inSection:0]];
[settings setSelected:!settings.isSelected];
if (settings.isSelected) {
NSLog(#"SELECTED!");
down.hidden = YES;
del.hidden = YES;
downLabel.hidden = YES;
delLabel.hidden = YES;
// [self performSelector:#selector(playButtonShow) withObject:nil afterDelay:0.3];
playButton.hidden = NO;
}else {
NSLog(#"UNSELECTED!");
playButton.hidden = YES;
down.hidden = NO;
del.hidden = NO;
downLabel.hidden = NO;
delLabel.hidden = NO;
NSLog(#"play button %d",playButton.hidden);
}
}
add a custom cell and Handle button click event and hide/unhide functionality on cell level
I have created the iPhone app which has the custom tableview cell and I also created a view in cellForRowAtIndexPath. Now, when i swiping the the cell in UITable the view that i created should be displayed in the particular cell and all other cell contents should be remained same.
My code as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RKCardCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"RKCardCell"];
// Configure the cell...
if (cell == nil) {
cell = [[RKCardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"RKCardCell"];
}
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeMethod:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[cell addGestureRecognizer:swipeGesture];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(-320.0, 8.0, 300, 180)];
[view setBackgroundColor:[UIColor greenColor]];
cell.profileImage.image = [UIImage imageNamed:[photoArray objectAtIndex:indexPath.row]];
cell.titleLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.nameLabel.text = [nameArray objectAtIndex:indexPath.row];
cell.descriptionLabel.text = [descriptionArray objectAtIndex:indexPath.row];
//%%% I made the cards pseudo dynamic, so I'm asking the cards to change their frames depending on the height of the cell
cell.cardView.frame = CGRectMake(10, 5, 300, [((NSNumber*)[cardSizeArray objectAtIndex:indexPath.row])intValue]-10);
return cell;
}
Following code is for gesture swiping:
- (void)swipeMethod:(UIGestureRecognizer *)gestureRec
{
[UIView animateWithDuration: 0.5 animations:^{
CGRectOffset(self.rkCardCell.cardView.frame, -320.0, 0.0);
}];
}
Thanks in advance.
It is good if you use the gesture recognizer with the table view.
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeTarget:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[cell addGestureRecognizer:_myTableView];
And your swipe target method like as follows.
- (IBAction)swipeTarget:(UISwipeGestureRecognizer *)gestureRecognizer
{
CGPoint swipePoint = [gestureRecognizer locationInView:_myTableView];
NSIndexPath *cellIndexPath = [_myTableView indexPathForRowAtPoint:swipePoint];
RKCardCell *cell = [_myTableView cellForRowAtIndexPath:cellIndexPath];
// Add your code here to show your view
[UIView animateWithDuration: 0.5 animations:^{
CGRectOffset(cell.cardView.frame, -320.0, 0.0);
}];
}
Take a look on https://github.com/CEWendel/SWTableViewCell .
It's a really good and simple library to make custom swipable tableviewcell.
in my app i have a UICollectionView. i want to apply to it's cells a long pressure gesture. i implemented that but when i run the app only the last cell works and the others do not respond. what it wrong? here is my code.
#interface
#property (nonatomic, strong) UILongPressGestureRecognizer *longPressure;
in view did load:
self.longPressure = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handlePressue:)];
self.longPressure.delegate = self;
[self.collectionView addGestureRecognizer:self.longPressure];
gesture handler:
- (void)handlePressue:(UILongPressGestureRecognizer*)gesture{
[[gesture.view viewWithTag:1] setBackgroundColor:[UIColor yellowColor]];
}
in collectionView:cellforitemAtIndexPAth:
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
imageView.tag = 1;
[imageView setBackgroundColor:[UIColor redColor]];
[cell addGestureRecognizer:self.longPressure];
[cell addSubview:imageView];
return cell;
is there anything wrong?
you need to add gesture on the each cell not for the UICollection View. that might be your problem.the last cell works because the gesture is added to the last cell only.
You need to creat long press gesture for each cell. Because gesture like other ui control,only one in the project.If you want add it to other view,you need copy another one.
Have a look at following Code snippet
MyFlowLayout *myLayout = [[MyFlowLayout alloc]init];
[self.collectionView setCollectionViewLayout:myLayout animated:YES];
UIGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc]
}
initWithTarget:self action:#selector(handlePinch:)];
[self.collectionView addGestureRecognizer:pinchRecognizer];
Handling the Pinch:
- (IBAction)handlePinch:(UIPinchGestureRecognizer *)sender {
// Get a reference to the flow layout
MyFlowLayout *layout =
(MyFlowLayout
*)self.collectionView.collectionViewLayout;
// If this is the start of the gesture
if (sender.state == UIGestureRecognizerStateBegan) {
// Get the initial location of the pinch?
CGPoint initialPinchPoint =
[sender locationInView:self.collectionView]; //Convert pinch location into a specific cell
NSIndexPath *pinchedCellPath =
[self.collectionView
indexPathForItemAtPoint:initialPinchPoint];
// Store the indexPath to cell
layout.currentCellPath = pinchedCellPath; }
else if (sender.state == UIGestureRecognizerStateChanged) {
// Store the new center location of the selected cell layout.currentCellCenter =
[sender locationInView:self.collectionView]; // Store the scale value
layout.currentCellScale = sender.scale; }
else {
[self.collectionView performBatchUpdates:^{ layout.currentCellPath = nil;
layout.currentCellScale = 1.0;
} completion:nil];
} }
For performance reasons I suggest you to avoid adding subviews and/or gestures to every single cell. Instead, add a singe longPressureGesture to your collectionView and on the gesture selector handle your per-cell switch business logic. Something as follow:
On your viewDidLoad or where you setup your collection, add the following gesture:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(showDeleteActions:)];
longPress.delegate = self;
[_aCollectionView addGestureRecognizer:longPress];
then on the selector do the single-cell handling:
- (void)showDeleteActions:(UILongPressGestureRecognizer*)gesture {
if (gesture.state == UIGestureRecognizerStateBegan)
{
NSIndexPath *indexPath = [_aCollectionView indexPathForItemAtPoint:[gesture locationInView:_aCollectionView]];
UICollectionViewCell *cell = [_aCollectionView cellForItemAtIndexPath:indexPath];
NSLog(#"cell to delete at IndexPath: %#", indexPath);
}
}
this approach is much more efficient abd far more stable.
Here what it is causing that your UILongPressGestureRecognizer getting overwrite to last cell. So you need to create UILongPressGestureRecognizer for each cell in cellForRowAtIndexPath:
Here is a small code snippet that can help you out
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
imageView.tag = 1;
[imageView setBackgroundColor:[UIColor redColor]];
UILongPressGestureRecognizer *longPressure =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handlePressue:)];
longPressure.delegate = self;
cell.userInteractionEnabled = YES; // Make userInteractionEnabled enable so it can detect touch
[cell addGestureRecognizer:self.longPressure];
[cell addSubview:imageView];
return cell;
There is no need to create any property for the UILongPressGestureRecognizer in .h file. And also remove code to add userInteractionEnabled on whole UICollectionView.