我需要在点击时将UIButton
图像更改为“选定”或“突出显示”状态的图像(而不是点按结束时)。
此外,我需要使用以下代码将按钮的状态更改为“已选择”:
- (IBAction)convertDown:(id)sender {
[buttonConvert setSelected:TRUE];
}
但是使用这段代码,我会看到“正常”状态的图像,直到敲击结束:(
如何解决这个问题?
答案 0 :(得分:0)
这是Interface Builder问题。在敲击结束前的那段时间内无法设置图像。可以通过以下代码修复:
- (void)viewDidLoad {
[super viewDidLoad];
[buttonConvert setBackgroundImage:[UIImage imageNamed:@"iphone_button_highlighted.png"]
forState:UIControlStateSelected | UIControlStateHighlighted];
}
答案 1 :(得分:0)
为按钮ControlEvents
有许多ControlEvents
可用:
UIControlEventTouchDown = 1 << 0,
UIControlEventTouchDownRepeat = 1 << 1,
UIControlEventTouchDragInside = 1 << 2,
UIControlEventTouchDragOutside = 1 << 3,
UIControlEventTouchDragEnter = 1 << 4,
UIControlEventTouchDragExit = 1 << 5,
UIControlEventTouchUpInside = 1 << 6,
UIControlEventTouchUpOutside = 1 << 7,
UIControlEventTouchCancel = 1 << 8,
UIControlEventValueChanged = 1 << 12,
UIControlEventEditingDidBegin = 1 << 16,
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19,
UIControlEventAllTouchEvents = 0x00000FFF,
UIControlEventAllEditingEvents = 0x000F0000,
UIControlEventApplicationReserved = 0x0F000000,
UIControlEventSystemReserved = 0xF0000000,
UIControlEventAllEvents = 0xFFFFFFFF
示例强>:
[yourButton addTarget:self
action:@selector(methodTouchUpInside:)
forControlEvents: UIControlEventTouchUpInside];
- (void)methodTouchDown:(id)sender{
NSLog(@"TouchDown");
}
或添加gustures而不是为ControlEvents分配目标
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[yourButton addGestureRecognizer: tapGesture];
- (void) handleTap:(UITapGestureRecognizer *)sender
{
if(sender.state == UIGestureRecognizerStateBegan)
{
NSLog(@"UIGestureRecognizerStateBegan");
}
else if(sender.state == UIGestureRecognizerStateEnded)
{
NSLog(@"UIGestureRecognizerStateEnded");
}
}
有许多UIGestureRecognizerState
可用。
UIGestureRecognizerStatePossible,
UIGestureRecognizerStateBegan,
UIGestureRecognizerStateChanged,
UIGestureRecognizerStateEnded,
UIGestureRecognizerStateCancelled,
UIGestureRecognizerStateFailed,
UIGestureRecognizerStateRecognized