我正在使用UIButton的自定義類型,我想要的是使用它就像一個切換開關與圖像的變化。就像之前點擊時它不在選定模式時一樣,它應該進入選定模式或以其他方式進行,反之亦然。它也會有不同的圖像,當它被選中時,它會有不同的圖像。如何使用UIButton作爲切換開關
我不能以編程方式做到這一點,有沒有什麼好的簡單的方法來做到這一點。
我正在使用UIButton的自定義類型,我想要的是使用它就像一個切換開關與圖像的變化。就像之前點擊時它不在選定模式時一樣,它應該進入選定模式或以其他方式進行,反之亦然。它也會有不同的圖像,當它被選中時,它會有不同的圖像。如何使用UIButton作爲切換開關
我不能以編程方式做到這一點,有沒有什麼好的簡單的方法來做到這一點。
在你的頭文件中加入:
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。
我相信這個帖子有一個更好的解決方案:iPhone UIButton with UISwitch functionality
的UIButton已經切換內置有可以設置和更改基於狀態的皮膚選擇的屬性。
在接口:
@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];
}
這裏唯一的問題是,你必須使用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
這是相當足以讓你切換工作
我做了這個類,改變類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
- (IBAction)buttonTapped:(UIButton *)sender {
//first time sender.selected is No
if (sender.selected) {
//code here
sender.selected=NO;
}
else{
//code here
sender.selected=YES;
}
}
UIButton
不默認支持一個 「切換」 功能。要使用此您需要設置不同的圖像,甚至文本顏色在Interface Builder的State Configuration = Selected
,並使用UIButton
選定屬性來切換其狀態。
代碼:
- (IBAction)yourButtonTouch:(UIButton *)sender {
sender.selected = !sender.selected;
if (sender.selected) {
//...
// Action to be performed when button is selected or
// the first touch
// ...
}
}
的SWIFT 3
@IBAction func playPause(sender : UIButton){
sender.isSelected = !sender.isSelected
if sender.isSelected {
player.pause()
}else{
player.play()
}
}
[iPhone的UIButton與UISwitch功能](的
可能重複http://stackoverflow.com/questions/2255166/iphone-uibutton -with-uiswitch-functionality) – QED 2013-02-14 19:19:51