I created many uibuttons dynamically via a for loop and added them to the scene. Now I want to add drag and drop capability to each uibutton. How can I accomplish this?
Say a game like breaker, you have a paddle. It is the only object which will be dragged and dropped by player so all we need to do is add a uibutton object for the paddle and update its center in touchesBegan and touchesMoved.
In my problem, I don't know which button will be clicked. If I attached an action to all the buttons to track which button being click then I won't be able to perfor开发者_C百科m DRAG the first time player click on a button. Maybe there is a better way.
Correct me if I don't understand, but each button should "know" if it is the one being clicked. In the touchesMoved:withEvent:
method, couldn't you just set the center to the location of the touch, or is there something I'm missing?
Try this.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.highlighted) {
UITouch *touch = [touches anyObject];
self.center = CGPointMake(touch.x, touch.y);
}
}
Of course, this code has a couple issues such as not dealing well with multiple touches, but I hope it gets the point across.
If you just want to be able to tell which button is currently tapped, you can use the highlighted
property of UIControl. Hope that helps!
精彩评论