效果图:
代码:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.title=@"单击手势的添加"; UIView *parentView=[[UIView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)]; parentView.backgroundColor=[UIColor redColor]; [self.view addSubview:parentView]; //单击的手势 UITapGestureRecognizer *tapRecognize = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)]; tapRecognize.numberOfTapsRequired = 1; tapRecognize.delegate = self; [tapRecognize setEnabled :YES]; [tapRecognize delaysTouchesBegan]; [tapRecognize cancelsTouchesInView]; [self.view addGestureRecognizer:tapRecognize]; } #pragma UIGestureRecognizer Handles -(void) handleTap:(UITapGestureRecognizer *)recognizer { NSLog(@"---单击手势-------"); }
时间: 2024-10-29 03:20:07