storyboard上にボタンを複数配置してロングタップしたらアラート表示、シングルタップでログを表示させたいのですが、ボタンとシングルタップメソッド、ロングタップメソッドのヒモ付方がわかりません。
アドバイス頂けると幸いです。
– (IBAction)buttonTapped:(id)sender { }
このようなIBActionに紐付けてその中でシングルタップやロングタップさせるしかないのでしょうか。
初心者ゆえ、無知で申し訳ありませんがよろしくお願いいたします。
下記のコードのシングルとロングタップのメソッド部分です。
[ViewController.m]
#import "ViewController.h" // Delegate画面遷移用 #import "AppDelegate.h" //ロングタップ生成 UILongPressGestureRecognizer *LongGesture; //シングルタップ生成 UITapGestureRecognizer *SingletapGesture; @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // delegateデータを送る準備 ViewAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; //ロングタップ開始 //ロングタップ LongGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(long_tapped:)]; //デリゲートをセット LongGesture.delegate = self; //ロングタップ有効化 //btn に追加 [_btn addGestureRecognizer:LongGesture]; _btn.userInteractionEnabled = YES; //シングルタップ開始 //シングルタップ SingletapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(single_tapped:)]; //シングルタップ有効化 //btn に追加 [_btn addGestureRecognizer:SingletapGesture]; _btn.userInteractionEnabled = YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //ロングタップ - (void)long_tapped:(UILongPressGestureRecognizer *)sender{ UIButton *btn = (UIButton *)sender.view; //長押ししたボタンを取得 switch (btn.tag) { case 1: //ボタンに応じてアラート表示 //btn [self voice_Alert]; break; case 2: //ボタンに応じてアラート表示 //btn [self voice_Alert]; break; } } //シングルタップ - (void)single_tapped:(UITapGestureRecognizer *)sender{ UIButton *btn = (UIButton *)sender.view; //1回だけ押したボタンを取得 switch (btn.tag) { case 1: NSlog(@"OK1"); break; case 2: NSlog(@"OK1"); break; } } //音声アラート - (void)voice_Alert { //音声名代入 switch (_btn.tag) { case 1: ac = [UIAlertController alertControllerWithTitle:@"音声名" message:@"btn2" preferredStyle:UIAlertControllerStyleAlert]; break; case 2: ac = [UIAlertController alertControllerWithTitle:@"音声名" message:@"btn2" preferredStyle:UIAlertControllerStyleAlert]; break; } @end
[ViewController.h]
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIGestureRecognizerDelegate,UIAlertViewDelegate> { // アラートコントローラを生成 UIAlertController * ac; } //ロングタップ - (void)long_tapped:(UILongPressGestureRecognizer *)sender; //シングルタップ - (void)single_tapped:(UILongPressGestureRecognizer *)sender; //ボタン生成 @property (weak, nonatomic) UIButton *btn; //音声アラート - (void)voice_Alert; @end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/12/25 05:31