Change UIButton border color on highlight

后端 未结 6 795
礼貌的吻别
礼貌的吻别 2021-02-03 23:53

I\'ve got a simple custom UIButton, to which I added:

button.layer.bordercolor = [[UIColor blueColor]CGColor];

However, I want to change the .b

6条回答
  •  难免孤独
    2021-02-04 00:50

    You were on the right track. Check the code below, it elaborates on this, but what you'll want to do is link selectors to different control events on your button. One for touchDown to change the shadow to red, and another for touchUpInside to change the shadow back when you lift your finger.

    Additionally, I see you've asked several questions on Stack Overflow and have yet to mark any as the correct answer. To continue to receive help on this website, you will need to start marking correct answers to your questions.

    [myButton addTarget:self action:@selector(highlightBorder) forControlEvents:UIControlEventTouchDown];
    [myButton addTarget:self action:@selector(unhighlightBorder) forControlEvents:UIControlEventTouchUpInside];
    
    
    - (void)highlightBorder
    {
        myButton.layer.borderColor = [[UIColor redColor]CGColor];
    }
    
    - (void)unhighlightBorder
    {
        myButton.layer.borderColor = [[UIColor blueColor]CGColor];
        //additional code for an action when the button is released can go here.
    }
    

    NOTE: Other options for UIControlEvents include:

    enum {
       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
    };
    

提交回复
热议问题