自定义手势识别器无法正常工作

时间:2015-07-24 15:35:12

标签: ios objective-c uigesturerecognizer

" touchBegan"并且" touchEnded"方法被调用,但我的视图只在touchesBegan方法上改变了它的背景而在touchEnded上没有改变。这是我的代码:

@implementation ClickableViewGestureRecognizer

NSString* const COLOR_UNTOUCHED = @"#00000000";
NSString* const COLOR_TOUCHED = @"#33000000";

- (instancetype)init {
    self = [super init];
    return self;
}

- (instancetype)initWithTarget:(id)target
                    action:(SEL)action {
    self = [super initWithTarget:target action:action];
    return self;
}

- (void)touchesBegan:(NSSet *)touches
       withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    super.view.layer.backgroundColor = [self getTouchedColor];
}

- (void)touchesEnded:(NSSet *)touches
       withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    super.view.layer.backgroundColor = [self getUntouchedColor];
}

- (CGColorRef) getUntouchedColor {
    return [JABColor colorFromHexString:COLOR_UNTOUCHED].CGColor;
}

- (CGColorRef) getTouchedColor {
    return [JABColor colorFromHexString:COLOR_TOUCHED].CGColor;
}

@end

这是colorFromHexString:对于任何想要看到它的人来说。

+ (UIColor *)colorFromHexString:(NSString *)hexString {
    unsigned rgbValue = 0;
    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    [scanner setScanLocation:1]; // bypass '#' character
    [scanner scanHexInt:&rgbValue];
    return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
}

2 个答案:

答案 0 :(得分:0)

你有touchesCancelled方法吗?也许你的Touch事件被取消而不是“结束”,尝试覆盖touchesCancelled

- (void)touchesCancelled:(NSSet *)touches
               withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    super.view.layer.backgroundColor = [self getUntouchedColor];
}

您可以尝试将[UIColor blackColor]设置为未触摸的颜色吗?直接,不转换为十六进制

答案 1 :(得分:0)

答案是" colorFromHexString"给了我一些问题。我把它扔掉了,只使用了UIColor方法。

相关问题