一般情況下點選效果都是正常的!要不然你對它做了什麼?一般細心的小夥伴都沒有遇到這種情況,但是呢!
當然我是二班的!在這裡我主要講兩個問題,解決問題和普及魔法。
一.普及問題(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
---------------
#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;
拖進工程直接用就行了!!!!多多指教
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];