简体   繁体   中英

Show an image in a UIButton on long press gesture

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 ?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM