Ios 触摸开始删除UIButton的按下状态

Ios 触摸开始删除UIButton的按下状态,ios,uibutton,uibezierpath,touchesbegan,Ios,Uibutton,Uibezierpath,Touchesbegan,我正在子类化一个UIButton,这样我就可以有一个自定义形状的按钮。形状由UIBezierPath上的点指定。禁止点击用户不能点击的UIButton区域。(UIButton是矩形的,但UIBezierPath是三角形的)我添加了touchesBegined方法来确定触摸位置是否在UIBezierPath的内部/外部。我成功地做到了这一点,但我注意到我失去了UIButton的按下状态。在实现触摸开始后,按下/突出显示时,UIButton不再改变颜色 绘制形状: - (id)initWithFra

我正在子类化一个UIButton,这样我就可以有一个自定义形状的按钮。形状由UIBezierPath上的点指定。禁止点击用户不能点击的UIButton区域。(UIButton是矩形的,但UIBezierPath是三角形的)我添加了touchesBegined方法来确定触摸位置是否在UIBezierPath的内部/外部。我成功地做到了这一点,但我注意到我失去了UIButton的按下状态。在实现触摸开始后,按下/突出显示时,UIButton不再改变颜色

绘制形状:

- (id)initWithFrame:(CGRect)frame withAngle:(AngleType)angle andColor:(UIColor *)color
{
    if ((self = [super initWithFrame:frame])){

        [self setImage:[Utils imageWithColor:color andSize:frame.size] forState:UIControlStateNormal];

        _path = [UIBezierPath new];

        self.angleType = angle;

        switch (angle) {
            case AngleLeft:
            {
                [_path moveToPoint:CGPointMake(0, elementHeight(self))];
                [_path addLineToPoint:CGPointMake(elementWidth(self), elementHeight(self))];
                [_path addLineToPoint:CGPointMake(elementWidth(self), 0)];
                [_path addLineToPoint:CGPointMake(15, 0)];
                [_path addLineToPoint:CGPointMake(0, elementHeight(self))];
            }
                break;
            case AngleRight:
            {
                [_path moveToPoint:CGPointMake(0, 0)];
                [_path addLineToPoint:CGPointMake(0, elementHeight(self))];
                [_path addLineToPoint:CGPointMake(elementWidth(self),elementHeight(self))];
                [_path addLineToPoint:CGPointMake(elementWidth(self) - 15, 0)];
                [_path addLineToPoint:CGPointMake(0, 0)];
            }
                break;
            case AngleBoth:
            {
                [_path moveToPoint:CGPointMake(15, 0)];
                [_path addLineToPoint:CGPointMake(0, elementHeight(self))];
                [_path addLineToPoint:CGPointMake(elementWidth(self) - 15, elementHeight(self))];
                [_path addLineToPoint:CGPointMake(elementWidth(self), 0)];
                [_path addLineToPoint:CGPointMake(15, 0)];
            }
                break;
            default:
                break;
        }

        CAShapeLayer *mask = [CAShapeLayer new];
        mask.frame = self.bounds;
        mask.path = _path.CGPath;

        self.layer.mask = mask;

    }

    return self;
}
仅允许形状内部的触摸事件

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self];

    if ([_path containsPoint:touchLocation]) {
        NSLog(@"Inside!");
        [super sendActionsForControlEvents:UIControlEventTouchUpInside];
    } else {
        NSLog(@"Outside!");
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self];
    if ([_path containsPoint:touchLocation]) {
        NSLog(@"Ended Inside!");
        [self sendActionsForControlEvents:UIControlEventTouchUpInside];
    } else {
        NSLog(@"Ended Outside!");
    }
}

通过查阅文档,我发现有一个属性名为“突出显示” 所以我只添加了
self.highlightthed=YES触控开始,且
self.highlightthed=否触控取消和触控取消

希望这对将来的人有所帮助