具有 UISwitch 功能的 iPhone UIButton

发布于 2024-08-21 07:16:48 字数 72 浏览 10 评论 0原文

有没有办法用开关状态的自定义图形来实现 UISwitch? 或者作为另一种选择,具有 UISwitch 功能的 UIButton?

Is there either a way to implement UISwitch with custom graphics for the switch-states?
Or as an alternative the other way round, an UIButton with UISwitch functionality?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事和酒 2024-08-28 07:16:49

UIButton 已经支持“切换”功能。

只需在 Interface Builder 中为“选定状态配置”设置不同的图像,并使用 UIButtonselected 属性来切换其状态。

UIButton already supports a "switch" functionality.

Just set a different image in Interface Builder for the "Selected State Configuration", and use the selected property of UIButton to toggle its state.

帅的被狗咬 2024-08-28 07:16:49

将图像设置为在选定状态下显示:

[button setImage:[UIImage imageNamed:@"btn_graphics"] forState:UIControlStateSelected];

然后在选择器内部进行触摸时,设置:

button.selected = YES;

如果您希望取消另一个按钮的选择,请设置:

otherButton.selected = NO;

set the image to show on selected state:

[button setImage:[UIImage imageNamed:@"btn_graphics"] forState:UIControlStateSelected];

and then on touch up inside selector, set:

button.selected = YES;

if you want this to cancel another button's selection, set:

otherButton.selected = NO;
梦回梦里 2024-08-28 07:16:49

为了构建上面 PGB 和 nurne 所说的内容,在设置状态并附加选择器(事件方法)后,您需要将此代码放入该选择器中。

- (IBAction)cost:(id)sender 
{
    //Toggle current state and save
    self.buttonTest.selected = !self.buttonTest.selected;

    /**
     The rest of your method goes here.
     */
}

To build on what PGB and nurne said above, after you set your states and attach a selector (event method) you would want to put this code in that selector.

- (IBAction)cost:(id)sender 
{
    //Toggle current state and save
    self.buttonTest.selected = !self.buttonTest.selected;

    /**
     The rest of your method goes here.
     */
}
酒浓于脸红 2024-08-28 07:16:49

对于有编程倾向的:

-(void) addToggleButton {
    CGRect aframe = CGRectMake(0,0,100,100);

    UIImage *selectedImage = [UIImage imageNamed:@"selected"];
    UIImage *unselectedImage = [UIImage imageNamed:@"unselected"];

    self.toggleUIButton = [[UIButton alloc] initWithFrame:aframe];
    [self.toggleUIButton setImage:unselectedImage forState:UIControlStateNormal];
    [self.toggleUIButton setImage:selectedImage forState:UIControlStateSelected];
    [self.toggleUIButton addTarget:self 
                            action:@selector(clickToggle:) 
                  forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.toggleUIButton];
}

-(void) clickToggle:(id) sender {
    BOOL isSelected = [(UIButton *)sender isSelected];
    [(UIButton *) sender setSelected:!isSelected];
}

For programmatically inclined:

-(void) addToggleButton {
    CGRect aframe = CGRectMake(0,0,100,100);

    UIImage *selectedImage = [UIImage imageNamed:@"selected"];
    UIImage *unselectedImage = [UIImage imageNamed:@"unselected"];

    self.toggleUIButton = [[UIButton alloc] initWithFrame:aframe];
    [self.toggleUIButton setImage:unselectedImage forState:UIControlStateNormal];
    [self.toggleUIButton setImage:selectedImage forState:UIControlStateSelected];
    [self.toggleUIButton addTarget:self 
                            action:@selector(clickToggle:) 
                  forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.toggleUIButton];
}

-(void) clickToggle:(id) sender {
    BOOL isSelected = [(UIButton *)sender isSelected];
    [(UIButton *) sender setSelected:!isSelected];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文