【问题标题】:How to Disable click event out side the button ?如何禁用按钮外的点击事件?
【发布时间】:2017-02-08 13:03:54
【问题描述】:

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

代码

.h

进口

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

图片

我的问题是,

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

谢谢

【问题讨论】:

  • 它给你,因为你的按钮形状是方形的。明白了。
  • 是的,我明白了,但我只想要这种类型的按钮形状。我该怎么做?

标签: ios objective-c iphone uibutton


【解决方案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];

在你的控制器中添加这些方法

-(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 属性来区分您的按钮。

【讨论】:

  • 嘿,Sunny 确保我的红色部分是按钮,所以当我实现这对按钮有好处。我的意思是按钮是否像点击事件一样工作?
  • 兄弟假设您在单视图控制器中有超过 2 个按钮,那么您如何管理这个东西?
  • 然后我没有使用像 [UIColor redColor] 这样的颜色?
  • 还有一件事是我手中的按钮计数,例如男士:-当我给 3 的高度和宽度时,它应该给我三个按钮,每个 120 度。(如三菱符号)。所以可能我理解不了。
  • 为 UIButton 更新。检查一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
  • 1970-01-01
  • 1970-01-01
  • 2014-02-01
相关资源
最近更新 更多