ios 添加到cell 上的button点击无效!扩大button的点击区域(黑魔法)

一般情况下点击效果都是正常的!要不然你对它做了什么?一般细心的小伙伴都没有遇到这种情况,但是呢!

当然我是二班的!在这里我主要讲两个问题,解决问题和普及魔法。

一.普及问题(button在cell上点击无效)

自定义一个cell,cell里边creat了一个button!然后调试了半天,什么反应都没有!

 

1.button的enable 设置为yes可点击的。

 

1.我以为我设置了交互禁用!

    self.userInteractionEnabled = YES;

 

2.button的frame越界了!

首先你要明白action的传递的载体,父试图的载体。

 

二.普及魔法(扩大button的点击区域)

 

啥都不说了,直接上代码!!!

//

//  UIButton+EnlargeEdge.h

//  peter.zhang

//

//  Created by peter.zhang on 8/28/14.

//  Copyright (c) 2014 peter. All rights reserved.

//

 

#import <objc/runtime.h>

 

@interface UIButton (EnlargeEdge)

- (void)setEnlargeEdge:(CGFloat) size;

- (void)setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left;

@end

 

 

---------------

//

//  UIButton+EnlargeEdge.h

//  peter.zhang

//

//  Created by peter.zhang on 8/28/14.

//  Copyright (c) 2014 peter. All rights reserved.

//

 

#import "UIButton+EnlargeEdge.h"

 

@implementation UIButton (EnlargeEdge)

static char topNameKey;

static char rightNameKey;

static char bottomNameKey;

static char leftNameKey;

 

- (void)setEnlargeEdge:(CGFloat) size

{

    objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);

    objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);

    objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);

    objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);

}

 

- (void)setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left

{

    objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);

    objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);

    objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:bottom], OBJC_ASSOCIATION_COPY_NONATOMIC);

    objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:left], OBJC_ASSOCIATION_COPY_NONATOMIC);

}

 

- (CGRect)enlargedRect

{

    NSNumber* topEdge = objc_getAssociatedObject(self, &topNameKey);

    NSNumber* rightEdge = objc_getAssociatedObject(self, &rightNameKey);

    NSNumber* bottomEdge = objc_getAssociatedObject(self, &bottomNameKey);

    NSNumber* leftEdge = objc_getAssociatedObject(self, &leftNameKey);

    if (topEdge && rightEdge && bottomEdge && leftEdge)

    {

        return CGRectMake(self.bounds.origin.x - leftEdge.floatValue,

                          self.bounds.origin.y - topEdge.floatValue,

                          self.bounds.size.width + leftEdge.floatValue + rightEdge.floatValue,

                          self.bounds.size.height + topEdge.floatValue + bottomEdge.floatValue);

    }

    else

    {

        return self.bounds;

    }

}

 

- (UIView*)hitTest:(CGPoint) point withEvent:(UIEvent*) event

{

    CGRect rect = [self enlargedRect];

    if (CGRectEqualToRect(rect, self.bounds))

    {

        return [super hitTest:point withEvent:event];

    }

    return CGRectContainsPoint(rect, point) ? self : nil;

}

@end

 

拖进工程直接用就行了!!!!多多指教

    UIButton *btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];

    btnCancel.frame = CGRectMake(100, 100, 10, 10);

    btnCancel.backgroundColor = [UIColor redColor];

    [btnCancel setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];

    [btnCancel addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    [topView addSubview:btnCancel];

 

上一篇:支付宝付款页面调整屏幕亮度


下一篇:超全超实用的Javascript类库和jQuery插件大全之一:Web印刷排版