How to use a UIButton as a toggle switch

前端 未结 11 764
我在风中等你
我在风中等你 2020-12-02 18:44

I am using a UIButton of custom type and what I want is use it like a toggle switch with the change of image. Like when it is clicked if previously it was not in selected mo

相关标签:
11条回答
  • 2020-12-02 19:12

    In the interface:

    @interface TransportViewController : UIViewController {
    
        UIButton *button;
    }
    @property(nonatomic, retain) UIButton *button;
    

    In the implementation:

    - (void)loadView {
    
    [super loadView];
    
        ...
    
        [self setButton:[UIButton buttonWithType:UIButtonTypeCustom]];
        [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:[UIImage imageNamed:@"image1"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"image2"] forState:UIControlStateSelected];
    }
    
    - (void) onClick:(UIButton *)sender {
    
        [sender setSelected:!sender.selected];
    }
    
    0 讨论(0)
  • 2020-12-02 19:14

    For Swift 3

    @IBAction func playPause(sender : UIButton){
    
    
       if sender.isSelected {
          player.pause()
       }else{
          player.play()
      }
      sender.isSelected = !sender.isSelected
    }
    
    0 讨论(0)
  • 2020-12-02 19:17

    In your header file add:

    IBOutlet UIButton *toggleButton;
    BOOL toggleIsOn;
    
    @property (nonatomic, retain) IBOutlet UIButton *toggleButton;
    

    In the implementation:

    - (IBACtion)toggle:(id)sender
    {
      if(toggleIsOn){
        //do anything else you want to do.
      }
      else {
        //do anything you want to do.
      }
      toggleIsOn = !toggleIsOn;
      [self.toggleButton setImage:[UIImage imageNamed:toggleIsOn ? @"on.png" :@"off.png"] forState:UIControlStateNormal];
    }
    

    then link your button with the IBActions and the IBOutlet and initialize toggleIsOn to NO.

    0 讨论(0)
  • 2020-12-02 19:20

    The only problem here is that you have to use 2 images to achieve toggling. Also you can't use highlighted property because of UIButton (UIControl) automatically sets this property under the hood, to be exact in touchBegan:, touchMoved:, etc. methods. The best way I offer is to simple use subclassing:

    @interface ToggleButton : UIButton
    
    @end
    
    @implementation ToggleButton
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        [super touchesBegan:touches withEvent:event];
        self.highlighted = self.selected = !self.selected;
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        [super touchesMoved:touches withEvent:event];
        self.highlighted = self.selected;
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
        [super touchesEnded:touches withEvent:event];
        self.highlighted = self.selected;
    }
    
    - (void)setSelected:(BOOL)selected{
        [super setSelected:selected];
        self.highlighted = selected;
    }
    
    @end
    

    This is quite enough to make your toggle working

    0 讨论(0)
  • 2020-12-02 19:21
    - (IBAction)buttonTapped:(UIButton *)sender {
    
    
       //first time sender.selected is No
        if (sender.selected) {
            //code here
            sender.selected=NO;
        }
        else{
        //code here
            sender.selected=YES;
        }
    }
    
    0 讨论(0)
提交回复
热议问题