UISlider's editingDidEnd event is not working in iPad - iphone

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

Related

UIButton receiving touch events outside bounds when set as leftView of UITextField

This has been a bit of a head scratcher for me. In an app I'm building I am using a UITextField, and adding a button as the leftView property. However, it seems that on an iPad (both sim and on device), the button is receiving touches that are well outside its bounds. This is interfering with the ability of the UITextField to become the first responder when the user touches the placeholder text. It seems that when touching the placeholder text, the events are being handled by the button instead of the text field itself. Strangely, this only appears to happen on iPad; it works as expected on the iPhone.
Here is some simple code demonstrating the problem:
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10.0f,
10.0f,
(self.view.frame.size.width - 20.0f),
35.0f)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.placeholder = #"Test text";
[self.view addSubview:textField];
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
[addButton addTarget:self action:#selector(touchDown:) forControlEvents:UIControlEventTouchDown];
[addButton addTarget:self action:#selector(touchUpInside) forControlEvents:UIControlEventTouchUpInside];
[addButton addTarget:self action:#selector(touchUpOutside) forControlEvents:UIControlEventTouchUpOutside];
textField.leftView = addButton;
textField.leftViewMode = UITextFieldViewModeAlways;
}
- (void)touchDown:(id)sender {
NSLog(#"touchDown");
}
- (void)touchUpInside {
NSLog(#"touchUpInside");
}
- (void)touchUpOutside {
NSLog(#"touchUpOutside");
}
It seems that sometimes touches are still perceived as being inside even though they appear to be outside the button's bounds. Then getting even further right, the button only receives UIControlEventTouchDown then UIControlEventTouchUpOutside.
2013-07-25 11:51:44.217 TestApp[22722:c07] touchDown
2013-07-25 11:51:44.306 TestApp[22722:c07] touchUpInside
2013-07-25 11:51:44.689 TestApp[22722:c07] touchDown
2013-07-25 11:51:44.801 TestApp[22722:c07] touchUpOutside
EDIT
Here is an example with the background color of the button changed as well as the approximate zones that trigger the events above. Also, I checked the frame of the button and it is less than 30px wide.
I sat down and spent some more time on this last night. I was already subclassing UITextField in my real application, so I ended up overriding -(id)hitTest:withEvent: as seen below. This has been working fine so far.
- (id)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if ([[super hitTest:point withEvent:event] isEqual:self.leftView]) {
if (CGRectContainsPoint(self.leftView.frame, point)) {
return self.leftView;
} else {
return self;
}
}
return [super hitTest:point withEvent:event];
}
I verified your results. I think it's most likely a bug in UITextView. Unfortunately, for some reason if you set leftView, events in that area are being sent to the wrong view (on the iPad).
I don't have a simple workaround for you since adjusting the frame of the leftView has no effect. I think it needs to be fixed at a lower level than we have access to. Maybe report the bug to Apple and live with it for now?
You could track the location of the touch and ignore it if it's out of bounds, but it seems like a lot of work for a minor bug?
Just met this question too, for some other solution I found is adding a check whether the event point located inside the button.
- (void)touchUpInside:(UIButton *)sender event:(UIEvent *)event
{
CGPoint location = [[[event allTouches] anyObject] locationInView:sender];
if (!CGRectContainsPoint(sender.bounds, location)) {
// Outside of bounds, so ignore:
return;
}
// Inside our bounds, so continue as normal:
}

Is there a way to get the location of a users touch while pressing UIButton?

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]);
}

How to get the id of button & implementing touchesMoved Simultaneously?

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.

How to drag and drop a UIButton

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 !!!

How do I handle event when user touch up outside the UIView?

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.