我希望创建一个简单的调色板,并在界面构建器中设置按钮网格。
我希望用户能够通过选择任何按钮与调色板(按钮)进行交互,然后在按钮之间拖动手指(在进入和退出按钮时触发事件并在每个事件中更改所选颜色) 。这可能吗?
例如,用户触摸蓝色按钮(颜色更新),然后将手指拖动到绿色按钮(颜色更新)。
“触摸拖动输入”和“触摸拖动退出”事件似乎仅在用户的初始选择位于发件人上时触发(不允许在两个按钮之间拖动)。谢谢!
答案 0 :(得分:7)
我创建了一个简单的应用程序,可以满足您的需求。您可以在以下网址找到来源:
http://github.com/st3fan/iphone-experiments/tree/master/Miscellaneous/ColorPalette/
它只是一个容器视图(PaletteView
),其中包含一组具有特定颜色的子视图。当PaletteView
收到触摸后,它会找到触摸下的视图,然后告诉其委托(PaletteViewDelegate
)颜色发生变化。
使用4.0.2在iPhone 4上测试。
答案 1 :(得分:1)
您可以使用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]];
}
}
}
我希望这会有所帮助:)
答案 2 :(得分:1)
我想你可能会对UIGestureRecognizer
感到幸运