问题如题,自己繁琐的解决方法如下:
-(void)buttonAction:(id)sender{
UIButton *button = (UIButton *)sender;
UIImage *image = [buttonbackgroundImageForState:UIControlStateNormal];
imageView = [[UIImageViewalloc]initWithFrame:self.view.bounds];
imageView.image = image;
imageView.tag =23419;
[button removeTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(tapImage:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:imageView];
[imageView release];
}
-(void)tapImage:(UIGestureRecognizer *)recognizer{
UIView *view = [self.viewviewWithTag:23419];
[view removeFromSuperview];
[myButton removeTarget:self action:@selector(tapImage:) forControlEvents:UIControlEventTouchUpInside];
[myButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside ];
}
我是用红色部分代码实现了上述问题。逻辑如下:当按钮事件点击之后,移除之前的事件并添加一个事件,这个事件就是手势单击事件,然后手势单击事件完成之后移除按钮中事件,添加原来的事件。感觉好麻烦。其实有个简单的方法:设置imageview的
userInteractionEnabled
-(void)buttonAction:(id)sender{
UIButton *button = (UIButton *)sender;
UIImage *image = [buttonbackgroundImageForState:UIControlStateNormal];
imageView = [[UIImageViewalloc]initWithFrame:self.view.bounds];
imageView.image = image;
imageView.tag =23419;
imageView.userInteractionEnabled =YES;
[self.viewaddSubview:imageView];
[imageView release];
}
-(void)tapImage:(UIGestureRecognizer *)recognizer{
UIView *view = [self.viewviewWithTag:23419];
[view removeFromSuperview];
}
现在发现,要是只想背景响应事件,而不想它上面的view响应,可以把上面的view的userInteractionEnabled设置为NO。