简体   繁体   English

保持按下UIButton时添加额外的图像

[英]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? 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元素,并指定其目标和操作,如下所示:

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. 当用户长按按钮时,这将调用-longPress:方法。 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. 然后通过按钮的Touch Down事件绑定以下方法。

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

Set button highlighted image and make it highlighted = YES when button pressed. 设置按钮突出显示的图像,并在按下按钮时使其突出显示= YES。 This should work I guess. 我猜这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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