1.点击按钮,出现遮盖层,松开按钮,遮盖层消失
2. 长按按钮,出现遮盖层,松开按钮,遮盖层消失
3. 点击按钮,出现遮盖层,拖拽按钮,遮盖层消失
UIButton *btnBottom = [[UIButton alloc]initWithFrame:CGRectMake(1,1,100,100)];
[btnBottom addTarget:self action:@selector(buttonClickTouchDown:) forControlEvents:UIControlEventTouchDown];
[btnBottom addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btnBottom];
#pragma mark-
#pragma mark-手指按下触发的事件(手指在按钮范围内)
-(void)buttonClickTouchDown:(UIButton *)btn
{
NSLog(@"手指按下触发的事件");
//按钮点击效果
UIView *vi = [[UIView alloc]initWithFrame:btn.frame];
vi.tag = 100;
vi.backgroundColor = [UIColor grayColor];
vi.alpha = 0.2;
[self addSubview:vi];
}
#pragma mark-
#pragma mark-点击图片跳转链接(当手指在按钮的范围内抬起的时候触发)
//根据btn.btnClickUrlString的值进行界面的跳转
-(void)buttonClick:(UIButton *)btn
{
NSLog(@"手指insd触发的事件");
//手指抬起的时候,取掉遮盖层
for (UIView *vi in [self subviews]) {//防止多次点击出现多层view
if (vi.tag == 100) {
[vi removeFromSuperview];
}
}
//点击按钮需要做的事情。。。。
}
#pragma mark-
#pragma mark-点击图片跳转链接(当手指在按钮内部拖动的时候触发)
-(void)buttonClickDragInside:(UIButton *)btn
{
NSLog(@"手指DragInside触发的事件");
//手指抬起的时候,取掉遮盖层
for (UIView *vi in [btn subviews]) {
//防止多次点击出现多层view
if (vi.tag == 100) {
[vi removeFromSuperview];
}
}
}
//因为我的btn添加在一个UIScrollerView 上面,所以在sc滚动的时候点击会出现bug,所以在sc滚动的时候将btn的交互性关闭,当sc减速停止的时候将btn的交互性在打开
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//当sc滚动的时候,关闭按钮的交互性(因为滚动会影响事件的判断)
if (scrollView.tag == 2) {
for (UIButton *btn in [scrollView subviews])
{
NSLog(@"scrollViewDidScroll");
btn.userInteractionEnabled = NO;
}
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//当sc停止滚动的时候,打开按钮的交互性
if (scrollView.tag == 2) {
for (UIButton *btn in [scrollView subviews])
{
NSLog(@"scrollViewDidEndScrollingAn");
btn.userInteractionEnabled = YES;
}
}
}