简体   繁体   中英

adding extra image when the UIButton remain pressed

I want to add an extra image when my button remains pressed. Is it possible in iphone?

I guess you mean when the user makes a long press on the cell (tap down and keep finger down)? You can add a UILongPressGestureRecognizer element to your button and specify its target and action like this:

UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.button addGestureRecognizer:longPressRecognizer];
[longPressRecognizer release];

This will call the -longPress: method when a user long presses on the button. The callback function could look like this:

- (void)longPress:(UILongPressGestureRecognizer *)recognizer{
    if (recognizer.state == UIGestureRecognizerStateBegan){
        // do something like add an image
    }
}

Hope this helps!

Of course Yes,

First take a custom button and set any image to it.

Then bind the following method wih Touch Down event of the button.

-(IBAction)buttonTouchedImage:(id)sender
{
    [yourButton setImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateHighlighted];
}

Set button highlighted image and make it highlighted = YES when button pressed. This should work I guess.

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