【问题标题】:Grid of Buttons on iPhone that Can be Dragged OveriPhone上可以拖动的按钮网格
【发布时间】:2010-08-27 03:17:14
【问题描述】:

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

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

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

“Touch Drag Enter”和“Touch Drag Exit”事件似乎仅在用户的初始选择位于发送方时才会触发(不允许在两个按钮之间拖动)。谢谢!

【问题讨论】:

    标签: iphone objective-c cocoa-touch


    【解决方案1】:

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

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

    它只是一个容器视图 (PaletteView),其中包含一堆具有特定颜色的子视图。当PaletteView 收到触摸时,它会找到触摸下的视图,然后告诉它的委托 (PaletteViewDelegate) 颜色发生了变化。

    在带有 4.0.2 的 iPhone 4 上测试。

    【讨论】:

    • 谢谢!这正是我想要的!非常感谢!
    【解决方案2】:

    您可以使用 touchesBegan + touchesMoved + touchesEnded 方法来创建用户从头到尾触摸的按钮列表。

    我是即时写的,没有测试它,但它可能看起来像这样:

    - (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】:

      我认为UIGestureRecognizer 可能会给你带来一些运气

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-21
        • 2018-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多