簡體   English   中英

自定義手勢識別器無法正常運行

[英]Custom Gesture Recognizer doesn't work properly

“ touchedBegan”和“ touchedEnded”方法都被調用,但是我的視圖僅在touchesBegan方法上改變了背景,而在touchedEnded上卻沒有改變。 這是我的代碼:

@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];
}

你有touchesCancelled方法嗎? 也許您的Touch事件已取消,而不是“結束”,請嘗試覆蓋touchesCancelled

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

並且可以僅出於測試原因嘗試將[UIColor blackColor]設置為不變色嗎? 直接,無需從十六進制轉換

答案是“ colorFromHexString”給了我問題。 我把它扔掉了,只使用了UIColor方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM