如何使用UIButton作为切换开关

时间:2009-11-09 16:37:40

标签: iphone

我正在使用自定义类型的UIButton,我想要的是像切换开关一样使用它来改变图像。就像点击它之前一样,如果以前没有处于选定模式,它应该进入选定模式,否则反之亦然。此外,它将具有不同的图像,当它被选中时,它将具有不同的图像。

我无法以编程方式执行此操作,是否有任何简单的方法可以执行此操作。

11 个答案:

答案 0 :(得分:39)

我相信这篇文章有更好的解决方案:iPhone UIButton with UISwitch functionality

UIButton已内置切换。有一个选定的属性,您可以根据状态设置和更改外观。

答案 1 :(得分:15)

在您的标题文件中添加:

IBOutlet UIButton *toggleButton;
BOOL toggleIsOn;

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

在实施中:

- (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];
}

然后将您的按钮与IBActions和IBOutlet相关联,并将toggleIsOn初始化为NO。

答案 2 :(得分:8)

在界面中:

@interface TransportViewController : UIViewController {

    UIButton *button;
}
@property(nonatomic, retain) UIButton *button;

在实施中:

- (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];
}

答案 3 :(得分:5)

UIButton支持"切换"功能默认。要使用此功能,您需要在Interface Builder中为State Configuration = Selected设置不同的图像甚至文本颜色,并使用UIButton的selected属性切换其状态。

代码:

- (IBAction)yourButtonTouch:(UIButton *)sender {

    sender.selected = !sender.selected;
    if (sender.selected) {
        //...
        // Action to be performed when button is selected or 
        // the first touch
        // ... 

    }
}

答案 4 :(得分:4)

适用于Swift 3

@IBAction func playPause(sender : UIButton){


   if sender.isSelected {
      player.pause()
   }else{
      player.play()
  }
  sender.isSelected = !sender.isSelected
}

答案 5 :(得分:3)

- (IBAction)buttonTapped:(UIButton *)sender {


   //first time sender.selected is No
    if (sender.selected) {
        //code here
        sender.selected=NO;
    }
    else{
    //code here
        sender.selected=YES;
    }
}

答案 6 :(得分:1)

这里唯一的问题是你必须使用2个图像才能实现切换。此外,您不能使用突出显示的属性,因为UIButton(UIControl)会自动将此属性设置在引擎盖下,确切地说是 touchBegan: touchMoved:等方法。我提供的最好的方法是简单地使用子类:

@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

这足以使你的切换工作

答案 7 :(得分:0)

更改按钮图像不是一项艰巨的任务。并且您可以使用一些BOOL值来检测您的开关按钮是否处于“ON”状态。在onBtnClk方法中,您只需更改BOOL值的状态并将图像设置为当前状态。

答案 8 :(得分:0)

为此,我们可以使用UIButton子类:

class UIToggleButton: UIButton {
    fileprivate let onImage: UIImage
    fileprivate let offImage: UIImage
    fileprivate let target: AnyObject
    fileprivate let onAction: Selector
    fileprivate let offAction: Selector
    var isOn: Bool {
        didSet {
            let image = isOn ? onImage : offImage
            setImage(image, for: UIControlState())
        }
    }

    init(onImage: UIImage,
        offImage: UIImage,
        target: AnyObject,
        onAction: Selector,
        offAction: Selector)
    {
        isOn = false
        self.onImage = onImage
        self.offImage = offImage
        self.target = target
        self.onAction = onAction
        self.offAction = offAction
        super.init(frame: CGRect.zero)
        setImage(offImage, for: UIControlState())
        addTarget(self, action: #selector(UIToggleButton.tapAction), for: .touchUpInside)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc func tapAction() {
        let sel = isOn ? onAction : offAction
        isOn = !isOn
        _ = target.perform(sel)
    }
}

答案 9 :(得分:0)

我将UIButton用作开关。


class ButtonSwitch: UIButton {
  override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) {
    if allControlEvents == .touchUpInside {
      isSelected.toggle()
    }
    super.sendAction(action, to: target, for: event)
  }
}

答案 10 :(得分:-1)

我创建了这个类,在“接口”构建器中将类更改为PGToggleButton就可以了。它使用默认和突出显示状态的图像,并具有公共属性来获取/设置实际状态。

PGToggleButton.h

@interface PGToggleButton : UIButton

@property (nonatomic, getter=isOn) BOOL on;
-(void)toggle;

@end

PGToggleButton.m

#import "PGToggleButton.h"


@interface PGToggleButton ()
@property (nonatomic, strong) UIImage *offStateImage;
@property (nonatomic, strong) UIImage *onStateImage;
-(void)touchedUpInside:(UIButton*) sender;
@end


@implementation PGToggleButton
@synthesize on = _on;
@synthesize offStateImage = _offStateImage;
@synthesize onStateImage = _onStateImage;

-(void)awakeFromNib
{
    [super awakeFromNib];

    self.offStateImage = [self imageForState:UIControlStateNormal];
    self.onStateImage = [self imageForState:UIControlStateHighlighted];

    [self addTarget:self
             action:@selector(touchedUpInside:)
          forControlEvents:UIControlEventTouchUpInside];
}

-(void)touchedUpInside:(UIButton*) sender
{ [self toggle]; }

-(void)toggle
{ self.on = toggle(_on); }

-(void)setOn:(BOOL) on
{
    _on = on;

    if (on)
        [self setImage:self.onStateImage forState:(UIControlStateNormal)];
    else
        [self setImage:self.offStateImage forState:(UIControlStateNormal)];
}

@end