正在回答 回答被采纳积分+1
3回答
Tender10
2017-08-18 11:05:50
您好,您的问题我已经知道了,但是您的代码写得有些命名不太规范,这里我在我上一份的案例中给您解决了这个问题,你可以自己去练习一下,感受一下。如果还有什么不明白的地方,可以继续提问。
#import "ViewController.h" @interface ViewController () @property (nonatomic,strong)NSArray *labelArray; @property (nonatomic,strong)UIImageView *imageView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self imageViewAddGesture]; [self setFrame]; } - (void)imageViewAddGesture { // 设置一个图片的大小、位置、颜色 self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 200, 200)]; self.imageView.userInteractionEnabled = YES; self.imageView.backgroundColor = [UIColor redColor]; [self.view addSubview: self.imageView]; } // 点击、长按、滑动手势的响应事件 -(void)addTapGesture:(UITapGestureRecognizer *)tap { NSLog(@"点击"); tap.enabled = NO; } -(void)addLongGesture:(UILongPressGestureRecognizer *)longGesture { if (longGesture.state == UIGestureRecognizerStateBegan) { NSLog(@"长按"); } longGesture.enabled = NO; } -(void)addPanGesture:(UIPanGestureRecognizer *)panGesture { if (panGesture.state == UIGestureRecognizerStateBegan) { NSLog(@"滑动"); } panGesture.enabled = NO; } // 按钮位置 - (void)setFrame { for (int i = 0; i < 3; i++) { UIButton *gestureButton = [[UIButton alloc] initWithFrame:CGRectMake(100+i*60, 100, 40, 30)]; gestureButton.backgroundColor = [UIColor yellowColor]; [gestureButton addTarget:self action:@selector(gestureButtonClick:) forControlEvents:UIControlEventTouchUpInside]; [gestureButton setTitle:self.labelArray[i] forState:UIControlStateNormal]; [gestureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; gestureButton.tag = i; [self.view addSubview:gestureButton]; } } // 存储按钮的Title - (NSArray *)labelArray { if (_labelArray == nil) { _labelArray = [NSArray arrayWithObjects:@"点击",@"长按",@"滑动",nil]; } return _labelArray; } // 按钮的点击响应事件 - (void)gestureButtonClick:(UIButton *)sender { NSInteger tag = sender.tag; if (tag == 0) { UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addTapGesture:)]; [self.imageView addGestureRecognizer:singleTap]; } else if(tag == 1){ UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(addLongGesture:)]; [self.imageView addGestureRecognizer:longGesture]; } else{ UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(addPanGesture:)]; [self.imageView addGestureRecognizer:panGesture]; } } @end
慕粉1330426222
2017-08-17 20:17:49
这个是测试的 第一张图是测试结果 测试过程是点击单击按钮后点击imageview触发点击手势打印 点击 俩字 第二个是我点击完单击后 点击长按按钮 在imageview中做出单击的手势 结果也打印出来 了 单击 二字 在我点击长按的时候 imageview上并没有禁用 单击手势
麻烦老师帮忙指出错误!
Tender10
2017-08-17 17:37:39
下面给你写了一个图片添加多个手势的案例,并且使用一个手势禁用其他全部手势
- (void)viewDidLoad { [super viewDidLoad]; // 设置一个图片的大小、位置、颜色 UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)]; imageView.userInteractionEnabled = YES; imageView.backgroundColor = [UIColor redColor]; // 创建点击、长按、滑动三个手势 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addTapGesture:)]; UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(addLongGesture:)]; UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(addPanGesture:)]; // 给同一个图片添加三个手势 [imageView addGestureRecognizer:singleTap]; [imageView addGestureRecognizer:longGesture]; [imageView addGestureRecognizer:panGesture]; [self.view addSubview: imageView]; } // 点击、长按、滑动手势的响应事件 -(void)addTapGesture:(UITapGestureRecognizer *)tap { NSLog(@"点击"); } -(void)addLongGesture:(UILongPressGestureRecognizer *)longGesture { if (longGesture.state == UIGestureRecognizerStateBegan) { NSLog(@"长按"); } } -(void)addPanGesture:(UIPanGestureRecognizer *)panGesture { if (panGesture.state == UIGestureRecognizerStateBegan) { NSLog(@"滑动"); } }
如果看不懂,或者还是有疑问可以继续提问。
iOS入门:基础语法与常用控件
- 参与学习 1337 人
- 提交作业 712 份
- 解答问题 1878 个
很多人都在疑问没有基础该怎么学习iOS开发?不用担心,本路径采用游戏关卡式的教学模式,并且以经典OC与最新Swift双重结合的教学内容,帮助大家快速掌握两种iOS开发语言基础,去掉“零”标签。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星