user7018875
user7018875

Reputation: 160

How to Disable click event out side the button ?

I am creating one custom button class inherit from the UIControl. I am creating Button with Kite Shape as shown in image using Bizer Path.

Code

.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");
}

Image

enter image description here

My Question Is,

When I clicked outside the button(Red Portion) It gives me a click event. I do not want to do this. Please help me,How can i solve it?

Thank You

Upvotes: 0

Views: 86

Answers (1)

Sunny
Sunny

Reputation: 831

I am updating my answer for 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;
}

In your controller 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];

Add these methods in your 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;
}

If you have multiple UIButton then use the tag property to distinguish your button.

Upvotes: 1

Related Questions