1

我必须制作一个包含大约 20 个按钮的应用程序,如果我从一个按钮切换到另一个按钮而不移除触摸(即通过拖动触摸)我想在我进入该按钮的框架区域时调用该函数。
例如:
在此处输入图像描述
当我将触摸从按钮的标签 10 拖动到按钮的标签 11 时,它应该调用按钮的标签 11 选择器方法。

4

2 回答 2

1

您应该使用以下方式注册事件。

 [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDragEnter];
于 2012-07-13T06:46:03.643 回答
1

我认为你不能用UIButtons. 但我对此有一个建议可能对你有帮助。添加Images而不是Buttons设置那些userIntractionEnable:NO然后在touchesMoved方法中,您可以通过与坐标@selector进行比较来调用您的方法。X,Y

我为此写了一个小代码-

//I added your example image on my self.view with a view
//of 320x140 and set it userIntractionEnabled:NO
//Now in touchMoved: method did this..

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint pt = [touch locationInView:self.view];

    //checking the touch is in self.view or not
    if([touch view] == self.view)
    {
        //This is like a 2D array, So you have to follow row and column pattern.
        if((pt.y>=0.0 && pt.y<=70.0))//For First Row
        {
            //These 7 are FirstRow Columns
            if(pt.x>=0.0 && pt.x<=45.0){
             NSLog(@"Method - 10");
            }
               if(pt.x>=46.0 && pt.x<=90.0){
                NSLog(@"Method - 11");
            }
            if(pt.x>=91.0 && pt.x<=135.0){
                NSLog(@"Method - 12");
            }

            if(pt.x>=136.0 && pt.x<=180.0){
                NSLog(@"Method - 13");
            }

            if(pt.x>=181.0 && pt.x<=225.0){
                NSLog(@"Method - 14");
            }

            if(pt.x>=226.0 && pt.x<=270.0){
                NSLog(@"Method - 15");
            }

            if(pt.x>=271.0 && pt.x<=315.0){
                NSLog(@"Method - 16");
            }

        }
        //Row change this is for Second Row
        if((pt.y>=71.0 && pt.y<=140.0))
        {
            //These 7 are SecondRow Columns
            if(pt.x>=0.0 && pt.x<=45.0){
            NSLog(@"Method - 17");
            }
            if(pt.x>=46.0 && pt.x<=90.0){
                NSLog(@"Method - 18");
            }
            if(pt.x>=91.0 && pt.x<=135.0){
            NSLog(@"Method - 19");
            }

            if(pt.x>=136.0 && pt.x<=180.0){
                NSLog(@"Method - 20");
            }

            if(pt.x>=181.0 && pt.x<=225.0){
            NSLog(@"Method - 21");
            }

            if(pt.x>=226.0 && pt.x<=270.0){
            NSLog(@"Method - 22");
            }

            if(pt.x>=271.0 && pt.x<=315.0){
                NSLog(@"Method - 23");
            }

        }

    }
}

希望对你有帮助 !!!!:-)

于 2012-07-13T07:50:01.653 回答