UITapGestureRecognizerクラスのインスタンスをViewに貼り付け、ターゲットアクションデザインパターンで実現させる。
MyViewController.h
#import <UIKit/UIKit.h> #import "MyView.h" @interface MyViewController : UIViewController @property MyView* myView; @end
MyViewController.m
#import "MyViewController.h" @interface MyViewController () @end @implementation MyViewController @synthesize myView; - (void)viewDidLoad { [super viewDidLoad]; myView = [[MyView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)]; myView.backgroundColor = [UIColor yellowColor]; [self.view addSubview:myView]; UITapGestureRecognizer* tapGesturRecobnizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touched)]; [myView addGestureRecognizer:tapGesturRecobnizer]; } - (void)touched { NSLog(@"----ログ2----"); } @end
MyView.h
#import <UIKit/UIKit.h> @interface MyView : UIView @end
MyView.m
#import "MyView.h" @implementation MyView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { } return self; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"----ログ1----"); } @end
コメント