Problem:Can't recognize tap and long press?

Hi guys…I just can’t make the long press work and besides my tapRecognizer seems not getting anything…I tried to delete some code and pretty sure there’s some problem within the longPress method(although i misnamed it as longTap)…my codes are here:

[quote]//
// CAJDrawView.m
// Doooodle
//
// Created by apple on 16/3/11.
// Copyright © 2016年 angieShroom. All rights reserved.
//

#import “CAJDrawView.h”
#import “CAJLine.h”

@interface CAJDrawView()
@property (nonatomic,strong)UIPanGestureRecognizer *moveRecognizer;
@property (nonatomic,strong)NSMutableDictionary *linesInProgress;
@property (nonatomic,strong)NSMutableArray *finishedLines;

@property (nonatomic,weak)CAJLine *selectedLine;
@end

@implementation CAJDrawView

-(instancetype)initWithFrame:(CGRect)frame{

self = [super initWithFrame:frame];
if(self){
    self.linesInProgress = [[NSMutableDictionary alloc] init];
    self.finishedLines = [[NSMutableArray  alloc] init];
    self.backgroundColor = [UIColor grayColor];
    self.multipleTouchEnabled = YES;
    
    UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
    doubleTapRecognizer.numberOfTapsRequired = 2;
    doubleTapRecognizer.delaysTouchesBegan = YES;
    [self addGestureRecognizer:doubleTapRecognizer];
    
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    tapRecognizer.numberOfTapsRequired = 1;
    [tapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
    [self addGestureRecognizer:tapRecognizer];
    
    
    UITapGestureRecognizer *longTapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)];
    [self addGestureRecognizer:longTapRecognizer];
   
    // pan
    /*self.moveRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveLine:)];
    self.moveRecognizer.delegate = self;
    self.moveRecognizer.cancelsTouchesInView = NO;
    [self addGestureRecognizer:self.moveRecognizer];*/
}
return self;

}

#pragma mark - double tap

  • (void)doubleTap:(UIGestureRecognizer *)gr{

    NSLog(@“Recognized Double Tap”);
    [self.linesInProgress removeAllObjects];
    [self.finishedLines removeAllObjects];
    [self setNeedsDisplay];

}
#pragma mark - select a line

  • (void)tap:(UIGestureRecognizer *)gr{

    NSLog(@“Recognized tap”);

    CGPoint point = [gr locationInView:self];
    self.selectedLine = [self lineAtPoint:point];

    if (self.selectedLine){

      [self becomeFirstResponder];
      UIMenuController *menu = [UIMenuController sharedMenuController];
      UIMenuItem *deleteItem = [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(deleteLine:)];
      menu.menuItems = @[deleteItem];
      
      [menu setTargetRect:CGRectMake(point.x, point.y, 2, 2) inView:self];
      [menu setMenuVisible:YES];
    

    }else{
    [[UIMenuController sharedMenuController] setMenuVisible:NO animated:YES];
    }

    [self setNeedsDisplay];
    }

  • (BOOL)canBecomeFirstResponder{
    return YES;
    }

//to select a line from the position that the touch gives

  • (CAJLine *)lineAtPoint:(CGPoint)p{

    for (CAJLine *l in self.finishedLines){
    CGPoint start = l.begin;
    CGPoint end = l.end;

      for (float t = 0.0; t <= 1.0; t += 0.05){
          float x = start.x + t * (end.x - start.x);
          float y = start.y + t * (end.y - start.y);
          
          if (hypot(x - p.x, y - p.y) < 20.0){
              return l;
          }
      }
    

    }
    return nil;
    }

#pragma mark - long tap and pan

  • (void)longTap:(UIGestureRecognizer *)lt{
    if (lt.state == UIGestureRecognizerStateBegan){

      CGPoint point = [lt locationInView:self];
      self.selectedLine = [self lineAtPoint:point];
      
      if (self.selectedLine){
          [self.linesInProgress removeAllObjects];
      }
    

    }else if (lt.state == UIGestureRecognizerStateEnded){
    self.selectedLine = nil;
    }
    [self setNeedsDisplay];

}

/*- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

if (gestureRecognizer == self.moveRecognizer){
    return YES;
}else{
    return NO;
}

}

  • (void)moveLine:(UIPanGestureRecognizer *)pgr{
    if (!self.selectedLine){
    return;
    }

    if (pgr.state == UIGestureRecognizerStateChanged){

      CGPoint translation = [pgr translationInView:self];
      CGPoint begin = self.selectedLine.begin;
      CGPoint end = self.selectedLine.end;
      begin.x += translation.x;
      begin.y += translation.y;
      end.x += translation.x;
      end.y += translation.y;
      
      self.selectedLine.begin = begin;
      self.selectedLine.end = end;
    

    }
    [self setNeedsDisplay];
    [pgr setTranslation:CGPointZero inView:self];
    }*/

#pragma mark - delete a selectedLine

  • (void)deleteLine:(id )sender{
    [self.finishedLines removeObject:self.selectedLine];
    [self setNeedsDisplay];

}

#pragma mark -

  • (void)strokeLine:(CAJLine *)line{

    UIBezierPath *bp = [UIBezierPath bezierPath];
    bp.lineWidth = 10;
    bp.lineCapStyle = kCGLineCapRound;

    [bp moveToPoint:line.begin];
    [bp addLineToPoint:line.end];
    [bp stroke];
    }

  • (void)drawRect:(CGRect)rect{

    for (CAJLine *line in self.finishedLines){

      [[self colorBasedOnAngleWithLine:line]set];
      [self strokeLine:line];
    

    }
    [[UIColor redColor] set];
    for (NSValue *key in self.linesInProgress){
    [self strokeLine:self.linesInProgress[key]];
    }

    if(self.selectedLine){
    [[UIColor greenColor]set];
    [self strokeLine:self.selectedLine];

    }

}

  • (UIColor *)colorBasedOnAngleWithLine:(CAJLine *)line{

    float angleVal = atan2f(line.end.y - line.begin.y, line.end.x - line.begin.x);
    if (angleVal > 3){
    return [UIColor cyanColor];
    }else if(angleVal >2){
    return [UIColor orangeColor];
    }else{
    return [UIColor purpleColor];
    }
    }

#pragma mark - Turning Touches into lines
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

NSLog(@"%@", NSStringFromSelector(_cmd));
for (UITouch *t in touches){
    CGPoint location = [t locationInView:self];
    CAJLine *line = [[CAJLine  alloc] init];
    line.begin = location;
    line.end = location;
    
    NSValue *key = [NSValue valueWithNonretainedObject:t];
    self.linesInProgress[key] = line;
}
[self setNeedsDisplay];

}

  • (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    NSLog(@"%@", NSStringFromSelector(_cmd));
    for (UITouch *t in touches){

      NSValue *key = [NSValue valueWithNonretainedObject:t];
      CAJLine *line = self.linesInProgress[key];
      line.end = [t locationInView:self];
    

    }

    [self setNeedsDisplay];

}

  • (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"%@",NSStringFromSelector(_cmd));
    for (UITouch *t in touches){
    NSValue *key = [NSValue valueWithNonretainedObject:t];
    CAJLine *line = self.linesInProgress[key];

      [self.finishedLines addObject:line];
      [self.linesInProgress removeObjectForKey:key];
    

    }

    [self setNeedsDisplay];

}

  • (void) touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"%@", NSStringFromSelector(_cmd));

    for (UITouch *t in touches){
    NSValue *key = [NSValue valueWithNonretainedObject:t];
    [self.linesInProgress removeObjectForKey:key];
    }
    [self setNeedsDisplay];
    }

@end[/quote]