iOS触摸事件问题
在线博彩导航
在线博彩导航
当前位置 : 在线博彩导航 > 博彩导航大全

iOS触摸事件问题

问题描述:
我创建了一个UIView的子类,主要用于画一些直线,在初始化方法中,写了相应的UIGestureRecognizer的类去对应触摸事件。

-instancetypeinitWithFrame:CGRectframe{
    self=[super initWithFrame:frame];
    ifself{
        self.linesInProgress=[[NSMutableDictionary alloc]init];
        self.finishedLines=[[NSMutableArray alloc]init];
        self.backgroundColor=[UIColor grayColor];
        self.multipleTouchEnabled=YES;
        //双击
        UITapGestureRecognizer *doubleTapRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selectordoubleTap:];
        doubleTapRecognizer.numberOfTapsRequired=2;
        doubleTapRecognizer.delaysTouchesBegan=YES;
        [self addGestureRecognizer:doubleTapRecognizer];
        //单击
        UITapGestureRecognizer *tapRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selectortap:];
        tapRecognizer.delaysTouchesBegan=YES;
        //区分双击和单击
        [tapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
        [self addGestureRecognizer:tapRecognizer];
        //长按
        UILongPressGestureRecognizer *pressRecognizer=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selectorlongPress:];
        [self addGestureRecognizer:pressRecognizer];
        //拖移
        self.moveRecognizer=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selectormoveLine:];
        self.moveRecognizer.delegate=self;
        //当cancelsTouchesInView的值为NO时,意味着这个对象所依附的UIVIEW对象仍然会收到相应的UIResponder消息,从而有机会处理相关的UITouch对象。
        self.moveRecognizer.cancelsTouchesInView=NO;
        [self addGestureRecognizer:_moveRecognizer];
    }
    return self;
}

在单击事件中,我创建了UIMenuController对象,并为其创建了一个标题为delete的UIMenuItem对象:

-voidtap:UIGestureRecognizer*gr{
    NSLog@"Recognized tap";
    CGPoint point=[gr locationInView:self];
    self.selectedLine=[self lineAtPoint:point];
    if self.selectedLine {
        //使视图成为UIMenuItem动作消息的目标
        [self becomeFirstResponder];
        //获取UIMenuController对象
        UIMenuController *menu=[UIMenuController sharedMenuController];
        //创建一个新的标题为“Delete”的UIMenuIetm对象
        UIMenuItem *deleteItem=[[UIMenuItem alloc]initWithTitle:@"Delete" action:@selectordeleteLine:];
        menu.menuItems=@[deleteItem];
        //先为UIMenuController对象设置显示区域,然后将其设置为可见
        [menu setTargetRect:CGRectMakepoint.x, point.y, 2, 2 inView:self];
        [menu setMenuVisible:YES animated:YES];
    }
    else{
        //如果没有选中的线条,就隐藏UIMenuController对象
        [[UIMenuController sharedMenuController]setMenuVisible:NO animated:YES];
    }
    [self setNeedsDisplay];
}

从初始化方法中可以看到,我还实现了拖放手势的事件,问题来了:
在用户按下并选中某根线条后,UIView会显示相应的菜单例如这里的delete,如果在没有关闭该菜单的时候就开始画新的线条,应用会在画新线条的同时拖移之前选中的线条。

请问这个该如何解决?

用BOOL或枚举写一个表示当前编辑状态的属性。

栏目列表

广告位

在线博彩导航