代码之家  ›  专栏  ›  技术社区  ›  Kevin Sylvestre

iPhone上可以拖动的按钮网格

  •  3
  • Kevin Sylvestre  · 技术社区  · 15 年前

    我想创建一个简单的调色板,并已设置在界面生成器中的按钮网格。

    我希望用户能够与调色板(按钮)进行交互,通过选择任何按钮,然后在按钮之间拖动手指(在进入和退出按钮时触发事件,并在每个事件中更改选定的颜色)。这可能吗?

    例如,用户触摸蓝色按钮(颜色更新),然后将手指拖到绿色按钮(颜色更新)。

    3 回复  |  直到 15 年前
        1
  •  7
  •   Stefan Arentz    15 年前

    我创建了一个简单的应用程序,可以满足你的需要。您可以在以下位置找到来源:

    http://github.com/st3fan/iphone-experiments/tree/master/Miscellaneous/ColorPalette/

    它只是一个容器视图( PaletteView 收到一个触摸,它找到触摸下的视图,然后告诉它的代理( PaletteViewDelegate

    在4.0.2版本的iphone4上测试。

        2
  •  1
  •   Andre    15 年前

    您可以使用touchesbeated+touchemoved+touchesend方法创建用户从头到尾触摸的按钮列表。

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    {
        // interactions is the NSMutableArray where you store the pressed/dragged buttons
        // we need to clear it when we start a new checkForInteractions
        [interactions removeAllObjects];
    
        UITouch *touch = [touches anyObject];
        [self checkForInteractions:touch];
    }
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        [self checkForInteractions:touch];
    }
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        [self checkForInteractions:touch];
        // here you'd call a function that'd use the buttons on the array,
        // to do whatever you wanted with them
        // it can also be copied to touchesMoved, to trigger the interactions
        // as the user's finger is moving, rather than only when it stops
        [self useInteractions];
    }
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
    {
        // if the touch was cancelled, clear the interactions
        [interactions removeAllObjects];
    }
    - (void) checkForInteractions : (UITouch *) touch
    {
        if ([touch view] == myButton1 || [touch view] == myButton2)
        {
            // check if the object had already been added to the array
            if ( ![interactions containsObject:[touch view]] )
            {
                [interactions addObject:[touch view]];
            }
        }
    }
    

    我希望这有帮助:)

        3
  •  1
  •   Aaron Saunders    15 年前

    我想你可能会有点运气 UIGestureRecognizer