How to make UIButton dragable but don't trigger click event - ios

I usually use this method to implement the UIButton movable;
[button addTarget:self action:#selector(dragMoving:withEvent:) forControlEvents:UIControlEventTouchDragInside];
But it will trigger the touchUpInside event at same time, and I need touchUpInside event to do some other things.
So does anyone know how to avoid this?
Thanks a lot;
use these code below to solve it;
[button addTarget:self action:#selector(touchDown:withEvent:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:#selector(touchDragInside:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:self action:#selector(touchUpInside:withEvent:) forControlEvents:UIControlEventTouchUpInside];
- (void) touchUpInside :(UIControl*)c withEvent:ev{
NSLog(#"touch Up inside");
if (count) {
NSLog(#"here I can do something about clicking event");
}
}
- (void) touchDown:(UIControl*)c withEvent:ev{
NSLog(#"touch Down");
count = YES;
}
-(void) touchDragInside:(UIControl*)c withEvent:ev{
NSLog(#"touch drag inside");
c.center = [[[ev allTouches] anyObject] locationInView:self.view];
count = NO;
}

Actually the best way to accomplish this is to use UIPanGestureRecognizer and add it to the button. It handles the dragging very well and you also can tap on it without needing to do anything else.

Related

touchesBegan and touchesEnded not called on a UIButton

When I touch a UIButton, the touchesBegan, touchesMoved and touchesEnded methods are not called. I want to perform an event when a continuous touch happens and cancel the event when the touch ends.
You can track UIButton touch start and touch end by using UIButton control events.
- (void)viewDidLoad {
[super viewDidLoad];
[self.button addTarget:self action:#selector(touchStarted:) forControlEvents:UIControlEventTouchDown];
[self.button addTarget:self action:#selector(touchEnded:) forControlEvents:UIControlEventTouchUpInside];
[self.button addTarget:self action:#selector(touchEnded:) forControlEvents:UIControlEventTouchUpOutside];
}
- (void)touchStarted:(id)sender {
NSLog(#"Touch started");
}
- (void)touchEnded:(id)sender {
NSLog(#"Touch ended");
}

How can I make UIButton disable which is created programmatically

I created some buttons in my code. I want to set it disable but I'm not sure how. here's my code.
UIButton *btn_levels = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn_levels.frame=CGRectMake(x-10, y, 40, 40);
[btn_levels setTitle:[beginer_lvl objectAtIndex:i] forState:UIControlStateNormal];
[btn_levels addTarget:self action:#selector(btn_Method) forControlEvents:UIControlEventTouchUpInside];
btn_levels.tag =i;
btn_levels.backgroundColor=[UIColor blackColor];
btn_levels.tintColor=[UIColor cyanColor];
NSLog(#"btn nm=%#",[beginer_lvl objectAtIndex:i]);
[self.scroll addSubview:btn_levels];
and that is the button method but I don't know what to do...
-(void)btn_Method
{
//to make button disable
}
Modify this line
[btn_levels addTarget:self action:#selector(btn_Method:) forControlEvents:UIControlEventTouchUpInside];
and this method
-(void)btn_Method:(UIButton*)sender
{
sender.enabled = NO;
}
change the statement
[btn_levels addTarget:self action:#selector(btn_Method) forControlEvents:UIControlEventTouchUpInside];
to
[btn_levels addTarget:self action:#selector(btn_Method:) forControlEvents:UIControlEventTouchUpInside];
Also change the below method -
-(void)btn_Method
{
//to make button disable
}
to
-(void)btn_Method:(UIButton*)button
{
//to make button disable
[button setEnabled:NO];
}
disabled but visible
btn_levels.enabled = NO;
also invisible
btn_levels.hidden = YES;
Target actions can send the object for which it'll performed the action. To make your method to know which button was tap, you need to make its definition to get the button inside the method. So just change you button definition to look like this,
- (void) btn_Method:(UIButton *)sender {...}
also, where you're adding target to the button you've to add a colon ( : ) after the method name to tell the compiler that, this action will need an object on which an action was added. So that line should look like this,
[btn_levels addTarget:self action:#selector(btn_Method:) forControlEvents:UIControlEventTouchUpInside];
^
Here, we will get a sender (you can give any name to your argument) of type UIButton with btn_Method action call. That's it, now you can do anything with that button, so in your case you want to make it disable, so the method would look like this,
- (void) btn_Method:(UIButton *)sender {
sender.enabled = NO;
}

UIButton UIControl target action 2 actions for single button

I have a button that has a selected and non-selected state.
My target action code for the button is this:
NSArray *targets = [myButton actionsForTarget:self forControlEvent:UIControlEventTouchUpInside];
if ([targets count] == 0) {
[myButton addTarget:self action:#selector(save:) forControlEvents:UIControlEventTouchUpInside];
When the button is pressed, it goes to selected state in the method.
If the button is selected, a different action should be called. Is there any unintended consequence to doing it this way or is there a way to add actions with UIControlState instead?
if (myButton.selected == NO){
[myButton addTarget:self action:#selector(save:) forControlEvents:UIControlEventTouchUpInside];
}
else{
[myButton addTarget:self action:#selector(delete:) forControlEvent:UIControlEventTouchUpInside];
}
-(IBAction)myButton:(id)sender
{
UIButton *btn = (UIButton*)sender;
btn.selected = !btn.selected;
if(btn.selected)
{
//Do whatever you want when button is selected
[self delete];
}
else
{
//Do whatever you want when button isDeselected
[self save];
}
}
Your approach might be confusing. You can use like this.
[myButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
- (void)buttonPressed:(UIButton *)sender
{
if(sender.selected)
[self save];
else
[self delete];
}
-(void)save
{
// your code
}
-(void)delete
{
// your code
}
i believe that you are wanting something like toggle button. if i am right use yourbutton.currentTitle to follow the functionality. i.e. if [youtbutton.currentTitle isEqualToString: #"save]" then you should perform save and set that to delete and vice verse

Swipe between UIButtons

Say I have 2 UIButtons next to each other and both of them will turn from white to black when I put my finger on them.
That obviously works fine, but if I put my finger on button 1 it turns black and then when I move my finger over the button 2 without lifting my finger, button 2 doesn't turn black, button 1 is still black.
So how can I "swipe" over one button and then over another one and change the highlighted button from the first one to the second one.
In your viewcontroller override the touchesMoved event. This will tell you when touches occur. The problem is that your buttons will have to have userInteractionEnabled set to NO so that they don't gobble the touch event. This will be ok if you're trying to do a custom slider or something where you don't need the actual button events.
Then in the touchesMoved you can loop through your buttons and see which button is pressed. Here is some code stolen from an excellent answer at Highlight the button when drag enter
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *t in touches) {
CGPoint touchPoint = [t locationInView:self.view];
CGPoint testPoint = [self.view convertPoint:touchPoint toView:aButton];
if ([aButton pointInside:testPoint withEvent:event]) {
//Do something
}
//rest of code
[self.button1 addTarget:self
action:#selector(dragEnter:)
forControlEvents:UIControlEventTouchDragEnter];
[self.button2 addTarget:self
action:#selector(dragEnter:)
forControlEvents:UIControlEventTouchDragEnter];
[self.button1 addTarget:self
action:#selector(dragExit:)
forControlEvents:UIControlEventTouchDragExit];
[self.button2 addTarget:self
action:#selector(dragExit:)
forControlEvents:UIControlEventTouchDragExit];
- (void)dragEnter:(id)sender
{
UIButton *button = (UIButton *)sender;
button.highlighted = YES;
}
- (void)dragExit:(id)sender
{
UIButton *button = (UIButton *)sender;
button.highlighted = NO;
}

How to find which button is pressed on the array of buttons in ios

I have an array of buttons,I want the text on each button..to be displayed on the label..so friends,please tell me how can I achieve this
Regards
Ranjit
Simply add button in this way
Button.tag = i
[Button addTarget:self action:#selector(MenuDetail:) forControlEvents:UIControlEventTouchUpInside];
[listOfButtons addObject:Button];
and handle touch up event as following
-(IBAction)MenuDetail:(id)sender
{
UIButton *temp= (UIButton *)sender;
NSInteger parentValue=temp.tag;
clsMenuItem *tempMenu=[appDelegate searchMenu:parentValue];
if(parentValue==1)
{
// do something
}
else if(parentValue==1)
{
// do something
}
}
hope this helps

Resources