ios 添加到cell 上的button點擊無效!擴大button的點擊地區(黑魔法)

來源:互聯網
上載者:User

標籤:ide   沒有   cell   custom   bottom   rect   get   targe   origin   

一般情況下點擊效果都是正常的!要不然你對它做了什嗎?一般細心的小夥伴都沒有遇到這種情況,但是呢!

當然我是二班的!在這裡我主要講兩個問題,解決問題和普及魔法。

一.普及問題(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];

 

ios 添加到cell 上的button點擊無效!擴大button的點擊地區(黑魔法)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.