touchesBegan删除了UIButton的按下状态

时间:2014-06-12 16:51:23

标签: ios uibutton uibezierpath touchesbegan

我正在为UIButton创建子类,因此我可以使用自定义形状的按钮。形状由UIBezierPath上的点指定。要禁用点击用户无法点按的UIButton区域。 (UIButton是矩形,但是UIBezierPath是Triangular)我添加了touchesBegan方法来确定触摸位置是否在UIBezierPath内部/外部。我在这方面很成功,但我注意到我失去了我的UIButton的紧迫状态。在实现touchesBegan时,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!");
    }
}

1 个答案:

答案 0 :(得分:0)

通过浏览文档,我发现有一个名为突出显示的属性 所以我刚刚在touchesBegan上添加了self.highligthed = YES;,在touchesCancelled和touchesEnded上添加了self.highligthed = NO;

希望这有助于将来。