gpt4 book ai didi

ios - 当用户使用长按手势时如何在 Collection View 单元格中显示按钮。

转载 作者:行者123 更新时间:2023-11-28 22:04:20 25 4
gpt4 key购买 nike

好吧,过去两天我一直很震惊,但还没有找到任何解决方案。我正在处理集合,我在每个集合单元格上动态创建一个按钮并将其隐藏,当用户使用长按手势时,按钮应该显示在每个单元格中。我应该如何实现这一点。任何想法都非常感谢。

-  (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];
}
}

这有帮助吗?

关于ios - 当用户使用长按手势时如何在 Collection View 单元格中显示按钮。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24389214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com