[英]How to add long press gesture recognizer to a button inside of tableview cell?
[英]How to Display button in Collection view cell when user uses a Long press gesture.
好吧,這兩天我一直感到震驚,還沒有找到任何地方的解決方案。 我正在使用集合,我在每個集合單元上動態創建一個按鈕並將其隱藏,當用戶使用長按手勢時,該按鈕應顯示在每個單元中。 我應該如何實施呢? 任何想法都非常感謝。
- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
{
userButtonOutlet.hidden=YES;
saveButtonOutlet.hidden=NO;
secondsLeft = minutes = seconds = 0;
if (timerr) {
[timerr invalidate];
}
timerr = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
secondsLeft=1800;
cancelSessionButtonOutlet.hidden=NO;
// [collectionData reloadData];
//Do Whatever You want on End of Gesture
}
else if(gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
NSLog(@"UIGestureRecognizerStateStarted");
}
}
- (IBAction)cancelSessionButton:(id)sender
{
NSLog(@"TEST");
cancelSessionButtonOutlet.hidden=YES;
userButtonOutlet.hidden=NO;
saveButtonOutlet.hidden=YES;
myCounterLabel.text=nil;
secondsLeft=0;
NSLog(@"The cancel session seconds left %d", secondsLeft);
[self stopTimer:nil];
}
-(NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
return 25;
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
return 1;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"item clicked");
CGRect rect = [collectionView layoutAttributesForItemAtIndexPath:indexPath].frame;
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController* vc = [storyboard instantiateViewControllerWithIdentifier:@"note"];
pc = [[UIPopoverController alloc] initWithContentViewController:vc];
[pc presentPopoverFromRect:rect inView:collectionView
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
NSLog(@" The index path is %@", indexPath);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// we're going to use a custom UICollectionViewCell, which will hold an image and its label
Cell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TableCell"
forIndexPath:indexPath];
if ((long)indexPath.row < 20 || (long)indexPath.row == 22 )
{
NSLog(@"case");
myCell.roomLabel.text = [NSString stringWithFormat:@"{%ld,%ld}", (long)indexPath.row, (long)indexPath.section];
NSLog(@"INDEX PATH %lu" , (long)indexPath.row );
myCell.subjectLabel.text =@"English";
myCell.cellView.backgroundColor= [UIColor grayColor];
button=[[UIButton alloc]init];
[button setFrame:CGRectMake(5,5,10,23)];
[button setTitle:@"X" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setHidden:NO];
[myCell addSubview:button];
}
else
{
myCell.userInteractionEnabled = NO;
}
return myCell;
}
您如何創建BOOL標志說:
@interface MyViewController
{
BOOL shouldDisplayButton;
}
@end
然后將長按手勢添加到整個UICollectionView中:
-(void)viewDidLoad
{
[super viewDidLoad];
...
UILongPressGestureRecognizer *longPress = [[UIGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
...
[self.myCollectionView addGestureRecognizer:longPress];
}
-(void)handleLongPress:(UILongPressGestureRecognizer *)longPressGesture
{
// -------------------------------------------------------------------
// we only want to trigger this if the user let go of the long press
// otherwise, the long press will continuously execute this block
// -------------------------------------------------------------------
if(longPressGesture.state == UIGestureRecognizerStateEnded)
{
// ----------------------------------------------------------------
// This basically toggles the boolean flag on and off then reload
// your collection view. See your collectionView cellForRowAtIndex
// where you enable the button based on the value of this boolean.
// ----------------------------------------------------------------
if(shouldDisplayButton)
{
shouldDisplayButton = NO;
}
else
{
shouldDisplayButton = YES;
}
[self.myCollectionView reloadData];
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
....
button=[[UIButton alloc]init];
[button setFrame:CGRectMake(5,5,10,23)];
[button setTitle:@"X" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[myCell addSubview:button];
// ---------------------------------------------------------------------
// Here you only hide your button if the BOOL flag is NO, otherwise,
// you show your button.
// ---------------------------------------------------------------------
if(shouldDisplayButton)
{
[button setHidden:NO];
}
else
{
[button setHidden:YES];
}
}
有幫助嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.