I have four UIButtons in my interface file. I have outlets attached to each button. I want to be able to drag and drop them across the screen. What is the best way to do this? How can I do this? please help!!!
try this u can drag and drop your button what ever you want
implement this code in ViewDidLoad
[self.shoeimageOutlet addTarget:self action:#selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[self.shoeimageOutlet addTarget:self action:#selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragOutside];
it will call the method written below
- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event
{
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
}
There are 3 possibilities nowadays on how to enable items – that is UIViews and UIControls – to be draggable.
override touchesMoved
add a target/selector for dragging control events
add a pan gesture recognizer
The complete tutorial found here !!!
Related
While my user is dragging his finger while holding down a UIButton, can I get the touch location for the finger at that point? If I can't do this it seems that I could probably create a UIImageView and give it button qualities. After this I would implement the touches began, moved, ended methods.
These methods; however, do not run while a UIButton has been touched.
The reason I need this is because I wanted to find out the direction of a touch drag outside movement, and for various other reasons too, but if this is impossible I will have to create UIImageViews I think.
Also, I do not subclass UIButton, I am too much beginner to take on apples clusters.
you should following this:
[myButton addTarget:self action:#selector(dragHandler:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[myButton addTarget:self action:#selector(dragHandler:withEvent:) forControlEvents:UIControlEventTouchDragOutside];
-(void)dragHandler:(UIButton *)sender withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
NSLog(#"touchInfo:%#", NSStringFromCGPoint([touch locationInView:self.view]);
}
I have a scrollview in my Main view and I have three subviews on my scrollview. And I have UIButtons in all my subviews.
Now, I want to drag those buttons from one subview to another subview (while dragging the buttons, the scrollview should not scroll).
How do I do this?
I'm not completely sure if this snippet works for this particular case (an UIControl inside a UIScrollView), but my understanding of UIResponder chain suggests me that it should :)
- (void)viewDidLoad { // or wherever you initialize your things
...
// Add swipe event to UIButton so it will capture swipe intents
UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] init];
[panGR addTarget:self action:#selector(panEvent:)];
[button addGestureRecognizer:panGR];
[panGR release];
}
- (void)panEvent:(id)sender {
button.center = [sender locationInView:self.view];
}
If this works (can't test it right now, but it did work for me in a similar situation), then you should add more code to handle the drag & drop related events (maybe disable Clip Subviews option in the UIScrollView, add the button to the new superview if the location intersects with the CGRect of the destination, return the button to the original location if it doesn't, etc).
So, what's happening in those lines? When you begin touching the UIButton, the order doesn't get to the UIScrollView because the event could follow as a touch event (handled by the UIButton), or as a pan event (handled by the UIScrollView). When you move your finger, the event is dismissed by the UIButton's responder because there's no Gesture Recognizer that knows how to proceed if the finger is moved.
But when you add a Gesture Recognizer to the UIButton who actually knows what to do when the finger is moved, everything is different: the UIButton will not dismiss the event, and the UIScrollView will never realize that there was a touch moving over it.
I hope my explanation is accurate and comprensible enough. Let me know if a) it doesn't work or b) there's something unclear.
Good luck :)
Try
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if (allowAppDrag && [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
return NO;
}
return YES;
}
I have a situation i have a UIButton Class in which upon selecting a button i am getting the id of the buttton based upon it i am changing the color of the button by using [self addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
And by using touchesBegan, touchesMoved, touchesEnded method i can drag drop the button object to any part of screen.
Now the problem is if i uses touchesBegan, touchesMoved, touchesEnded method then i am not getting id of button so i am not able to change the color.
So how can i able to get both the problem solved?
one of the parameters for touchesBegan is a set of UITouch objects. UITouch has property "view", which is the view that you tapped on. So something like
-(void)touchesBegan:touches withEvent:e
{
id* myButton = [touches anyObject].view;
}
If you call the following method on your parent view (containing all the buttons) in touchesBegan: it should return you the button you are touching....
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
Hope this helps.
I want to trigger one method when sliding the UISlider is ended.I used "editingDidEnd"event of UISlider in xib and attached it to a IBAction method but the method is not being called.Can any one please help me how to know when sliding is ended on UISlider?
You can attach to the "UIControlEventValueChanged" slider event instead and use the following code in your event handler:
[slider addTarget:self action:#selector(handleValueChanged:event:) forControlEvents:UIControlEventValueChanged];
- (void)handleValueChanged:(id)sender event:(id)event {
UITouch *touchEvent = [[event allTouches] anyObject]; // there's only one touch
if (touchEvent.phase == UITouchPhaseEnded) { /* place your code here */ }}
UISlider inherits from UIControl so you should be able to add and action on "UIControlEventTouchUpInside"
Try something like :
[slider addTarget:self action:#selector(sliderTouchUpInsideAction:) forControlEvents:UIControlEventTouchUpInside];
Hope this helps,
Vincent
I have a custom popup menu in my iOS application. It is UIView with buttons on it. How do I handle event when user touch up outside this view? I want to hide menu at this moment.
You should create a custom UIButton that takes up the whole screen. Then add your subview on top of that button. Then when the user taps outside of the subview they will be tapping the button.
For example, make the button like this:
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(yourhidemethod:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"" forState:UIControlStateNormal];
button.frame = self.view.frame;
[self.view addSubview:button];
(where yourhidemethod: is the name of the method that removes your subview.) Then add your subview on top of it.
Update: It looks like you're wanting to know how to detect where touches are in a view. Here's what you do:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self]; //location is relative to the current view
// do something with the touched point
}
Put it in full-screen transparent view and handle touches for it.
One idea is to have an invisible view(uicontrol) that is as big as the screen which then holds this custom popup.
It sounds like you'd be better served by deriving your menu from UIControl rather than UIView. That would simplify the touch handling, and all you'd have to do in this case would be to set a target and action for UIControlEventTouchUpOutside. The target could be the menu itself, and the action would hide or dismiss the menu.