长按手势时在 UIButton 中显示图像

发布于 2024-12-29 01:53:33 字数 1388 浏览 1 评论 0原文

我有以下问题。我有一个 UIScrollView ,上面有几个按钮,这些按钮上的图标设置为图像。我的每个按钮都附有一个长按识别器。如何在长按手势时在发件人按钮上显示较小的删除图标?我的目标是创建当用户想要删除特定应用程序时 iOS 呈现的行为。 这是按钮的代码(带有图像):

//set the button with the image of conference here.
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(3, 3, w-5, h-5);
CALayer * l = [button layer];
[l setMasksToBounds:YES];
[l setCornerRadius:8.0];
[button setImage:thumb forState:UIControlStateNormal];

button.property = confInfo;
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i; 
bView.tag = i;

//Add gesture recognizer to be used for deletion of conference.
UILongPressGestureRecognizer *pahGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognizerStateChanged:)];

pahGestureRecognizer.minimumPressDuration = 1.0;
[button addGestureRecognizer:pahGestureRecognizer];

此代码位于循环中(请参阅代码中的 i)。我的长按操作是这样的:

- (void)longPressGestureRecognizerStateChanged:(UIGestureRecognizer *)gestureRecognizer {

      switch (gestureRecognizer.state) {

          case UIGestureRecognizerStateEnded:
               NSLog(@"Tapped!!!");                    
               break;

          default:
               break;
      }
 }

如何将我单击的按钮传递给此操作以在按钮的右上角显示较小的 X 图像?

I have the following problem. I have a UIScrollView on which I have a couple of buttons with icons set as images on these buttons. I have a long press recognizer attached to each button. How can I show a smaller delete icon on the sender button on long press gesture? My goal is to create the behaviour that is presented by iOS when the user wants to delete a specific application.
This is the code for a buttons (with images):

//set the button with the image of conference here.
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(3, 3, w-5, h-5);
CALayer * l = [button layer];
[l setMasksToBounds:YES];
[l setCornerRadius:8.0];
[button setImage:thumb forState:UIControlStateNormal];

button.property = confInfo;
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i; 
bView.tag = i;

//Add gesture recognizer to be used for deletion of conference.
UILongPressGestureRecognizer *pahGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognizerStateChanged:)];

pahGestureRecognizer.minimumPressDuration = 1.0;
[button addGestureRecognizer:pahGestureRecognizer];

This code is in a cycle (see i in the code). My long tap action is like this:

- (void)longPressGestureRecognizerStateChanged:(UIGestureRecognizer *)gestureRecognizer {

      switch (gestureRecognizer.state) {

          case UIGestureRecognizerStateEnded:
               NSLog(@"Tapped!!!");                    
               break;

          default:
               break;
      }
 }

How I can pass a button on which I clicked to this action to show the smaller X image on the right upper corner of the button ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万水千山粽是情ミ 2025-01-05 01:53:33

您的手势识别器应通过 UIButton 的 view 属性附加到它。

case UIGestureRecognizerStateEnded:
    NSLog(@"Tapped!!!");
    [((UIButton*)gestureRecognizer.view) setImage:thumbWithX forState:UIControlStateNormal];
    break;

Your gesture recognizer should be attached to the UIButton via its view property.

case UIGestureRecognizerStateEnded:
    NSLog(@"Tapped!!!");
    [((UIButton*)gestureRecognizer.view) setImage:thumbWithX forState:UIControlStateNormal];
    break;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文