我正在开发一个iPhone应用程序,我有两个UIViews:一个在左边,一个在右边。每个视图上都有一组UIButtons。可以单击这些按钮并在彼此之间拖动。
我的工作非常好。我实际上有三个视图:左,右和整个屏幕。当用户单击UIButton时,它会弹出当前视图并进入整个视图。用户可以自由地拖动它。当用户释放他们的点击时,UIButton将从整个视图中弹出,并进入任何视图。
此过程非常有效。我的问题是当用户点击按钮并且它弹出到整个视图时,UIButton删除“Clicked”值,用户必须再次单击以拖动按钮。谁能告诉我一个方法,只需点击一下即可完成?
我的代码分布在很多类中,但这是我能做的最好的代码:
// If the current block is selected
if( tempBlock.selected )
{
// The current block becomes selected
selectedBlock = tempBlock;
[selectedBlock retain];
// Take out of garageBlocks
[garageBlocks removeObjectAtIndex:i];
// Move the image to the front
[selectedBlock.button removeFromSuperview];
[self.view addSubview:(UIView*)selectedBlock.button];
// Show available slots
[self UpdateAvailableSlots];
[self ShowAvailableSlots];
// end the loop
i = [garageBlocks count];
}
按钮中的代码:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
selected = TRUE;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// Get the touched position
touch = [[event allTouches] anyObject];
location = [touch locationInView:view];
location.x -= 32;
location.y -= 32;
selected = TRUE;
// Position the object if in garage screen
if( garageScreen )
{
[self SetPosition: (location.x) : (location.y)];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
selected = FALSE;
}
即使我在按钮类中设置了selected = TRUE,它也不会在视图更改后拖动。我认为当视图更改时,在用户再次单击之前不会调用touchesMoved。
感谢您的帮助。