我有一个子类 UIButton,我将其制成不规则形状(平行四边形),在其中我覆盖了触摸事件,因此它只接受形状内的触摸事件
如何实现像普通 UIButton 这样的触摸事件,在其中我可以在点击并拖动 UIButton 外的手指以取消触摸时取消触摸事件。对于我当前的代码,如果我在按钮内拖动手指,它会调用 touchesCancelled 事件。我正在使用 TouchUpInside 事件来执行 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)setText:(NSString *)text withAlignment:(NSTextAlignment)alignment
{
_buttonLabel = [[UILabel alloc] init];
_buttonLabel.font = FONT_Helvetica_Neue(14);
_buttonLabel.textColor = [UIColor whiteColor];
_buttonLabel.text = text;
_buttonLabel.textAlignment = alignment;
if (alignment == NSTextAlignmentLeft) {
_buttonLabel.frame = CGRectMake(15 + 10, 0, elementWidth(self), elementHeight(self));
} else if (alignment == NSTextAlignmentRight) {
_buttonLabel.frame = CGRectMake(0, 0, elementWidth(self) - 15 - 10, elementHeight(self));
} else if (alignment == NSTextAlignmentCenter) {
_buttonLabel.frame = CGRectMake(0, 0, elementWidth(self), elementHeight(self));
}
[self addSubview:_buttonLabel];
}
- (void)setBackgroundColor:(UIColor *)backgroundColor
{
[self setImage:[Utils imageWithColor:backgroundColor andSize:self.frame.size] forState:UIControlStateNormal];
CAShapeLayer *mask = [CAShapeLayer new];
mask.frame = self.bounds;
mask.path = _path.CGPath;
self.layer.mask = mask;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self];
if ([_path containsPoint:touchLocation]) {
NSLog(@"Inside!");
self.highlighted = YES;
//[self sendActionsForControlEvents:UIControlEventTouchUpInside];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self];
self.highlighted = NO;
if ([_path containsPoint:touchLocation]) {
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self];
if ([_path containsPoint:touchLocation]) {
NSLog(@"...");
self.highlighted = YES;
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self];
self.highlighted = NO;
if ([_path containsPoint:touchLocation]) {
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
}
}