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];

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM