https://github.com/matsu-chara/SimpleSwitchButton
iOSでsmart newsとかmixi newsみたいなジャンル選択をポチポチするやつとか画像ベースのラジオボタンが欲しかったんですが、 ボタンを統一的に扱うのが面倒だったのでSimpleSwitchButtonCollectionという統一的に管理してくれるクラスを作成しました。
SimpleSwitchButtonCollection* simpleSwitchButtonCollection = [[SimpleSwitchButtonCollection alloc] initWithSimpleSwitchButtonMode:asCheckBox];
asCheckBox
、asRadioButton
のどちらかを選択することで複数選択の可否を設定します。
タップしたときにイベントを発行して今ONになってる物の一覧を取得したり、 全部ON、全部OFFなどを簡単に行なうことが出来ます。
int counter = [simpleSwitchButtonCollection countOnItems]; NSArray* onKeys = [simpleSwitchButtonCollection getKeysOfOnItem]); [simpleSwitchButtonCollection setAllItemsToOff]; [simpleSwitchButtonCollection setToOnForKey[@"news"];
ボタンの追加は
SimpleSwitchButton *button = [simpleSwitchButtonCollection createSimpleSwitchButtonForKey:@"news" ButtonFrame:CGRectMake(0, 50, 100, 100) OnImageName:@"news_on.png" OffImageName:@"news_off.png"]; [self.view addSubview:button];
みたいな感じで簡単にできます。 タップ中にハイライトしている画像を指定することも出来ます。
SimpleSwitchButton *button = createSimpleSwitchButtonForKey::@"news" ButtonFrame:CGRectMake(0, 50, 100, 100) OnImageName:@"news_on.png" OnHighlitedImageName:@"news_on_highlited.png" OffImageName:@"news_off.png" OffHighlitedImageName:(@"news_off_highlited.png";
詳しくはReadmeで。 探せば他にいいの絶対あるやろ感ありますが、気づいたら出来てたので('_`) 今はStoryboardに置いたボタンをCollectionに登録できないので気が向いたらやります。