如何在按钮旁边禁用点击事件?

时间:2017-02-08 13:03:55

标签: ios objective-c iphone uibutton

我正在创建一个从UIControl继承的自定义按钮类。 我正在使用Bizer Path创建具有风筝形状的Button,如图所示。

代码

的.h

import

@interface CustomButton : UIControl
-(instancetype)initWithFrame:(CGRect)frame;
@end

的.m

#import "CustomButton.h"

@implementation CustomButton
-(instancetype)initWithFrame:(CGRect)frame
{
   self =  [super initWithFrame:frame];
    if(self)
    {
        UIBezierPath *path = [UIBezierPath new];
        //  [path moveToPoint:(CGPoint){0, 0}];
        [path moveToPoint:(CGPoint){100,50}];
        [path addLineToPoint:(CGPoint){100, 100}];
        [path addLineToPoint:(CGPoint){150, 100}];
        [path addLineToPoint:(CGPoint){200, 0}];
        [path addLineToPoint:(CGPoint){100, 50}];

        // Create a CAShapeLayer with this triangular path
        // Same size as the original imageView
        CAShapeLayer *mask = [CAShapeLayer new];
        mask.frame = self.bounds;
        mask.path = path.CGPath;

        // Mask the imageView's layer with this shape
        self.layer.mask = mask;


    }

    return self;
}
@end

VC .m

CustomButton *button = [[CustomButton alloc]initWithFrame:CGRectMake(20, 20, 200, 200)];
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside] ;
    [self.view addSubview:button];
}

- (void)buttonPressed:(id)sender
{

        NSLog(@"pressing");
}

图像

enter image description here

我的问题是,

当我点击按钮外面(红色部分)它给了我一个点击事件。 我不想这样做。 请帮帮我,我该如何解决?

谢谢

1 个答案:

答案 0 :(得分:1)

我正在更新UIBUtton

的答案

CustomButton.h

#import <UIKit/UIKit.h>

@interface CustomButton : UIControl
-(instancetype)initWithFrame:(CGRect)frame;    
@end

<强> CustomButton.m

#import "CustomButton.h"

@implementation CustomButton



-(instancetype)initWithFrame:(CGRect)frame
{
    self =  [super initWithFrame:frame];
    if(self)
    {
        UIBezierPath *path = [UIBezierPath new];
        [path moveToPoint:(CGPoint){100,50}];
        [path addLineToPoint:(CGPoint){100, 100}];
        [path addLineToPoint:(CGPoint){150, 100}];
        [path addLineToPoint:(CGPoint){200, 0}];
        [path addLineToPoint:(CGPoint){100, 50}];
        CAShapeLayer *mask = [CAShapeLayer new];
        mask.frame = self.bounds;
        mask.path = path.CGPath;

        // Mask the imageView's layer with this shape
        self.layer.mask = mask;

    }


    return self;
}

在您的控制器viewDidLoad

CustomButton *buttonCustom = [[CustomButton alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
[buttonCustom setBackgroundColor:[UIColor redColor]];
[buttonCustom addTarget:self action:@selector(buttonCustom:forEvent:) forControlEvents:UIControlEventTouchUpInside];
[buttonCustom setTag:1000];
[self.view addSubview:buttonCustom];

在Controller中添加这些方法

-(void)buttonCustom:(UIButton *)sender  forEvent:(UIEvent*)event{
    UITouch *touch = [[event touchesForView:sender] anyObject];
    CGPoint location = [touch locationInView:self.view];
    UIColor *color = [self colorOfPoint:location];
    if([color isEqual:[UIColor redColor]]){
        NSLog(@"red ");
    }else{
        NSLog(@"White Color");
    }
}

- (UIColor *) colorOfPoint:(CGPoint)point
{
    unsigned char pixel[4] = {0};

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);

    CGContextTranslateCTM(context, -point.x, -point.y);

    [self.view.layer renderInContext:context];

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];

    return color;
}

如果您有多个UIButton,请使用tag属性区分您的按钮。