TouchDragEnter into a UIButton - touch

Is it possible to TouchDown outside a uibutton and then dragInside another UIButton with the UIEventTouchDragEnter or something like that?
I need help with the code. I tried it with imageView but i would appreciate a solutions with buttons!
Thank you!
Franhu

The UITouchDownEvent event must happen inside the button for the button to track the touch. The UITouchDragEnter event means that the touch started inside the button (generating UITouchDownEvent), moved outside the button (generating UITouchDragExitEvent)' then moved back inside the button (generating UITouchDragEnterEvent).

Related

UIButton's IBAction method execute after delay in Swift

I have 4 UIButtons in a UIScrollView. The view hierarchy is like:
UIViewController-> UIView-> UIScrollView-> UIView-> UIView-> UIButton
I have connected the IBAction for respected UIButton with TouchupInside from UIStoryboard.
My Issue is when I click on UIButton, it gets highlighted, but the respected IBAction method is not getting called at the time. The action is been called after I select any other control like UITextField or any other editable control. I am unable to understand the issue.
I have tried to add the action to the UIButton programmatically also like:
self.newProductNotifBtn?.addTarget(self, action: #selector(notifCheckMarkBtnClicked), forControlEvents: .TouchUpInside)
But the result is same. No action on the method is executing at the same time it is been selected.
The suggestions are appreciated.
Thank you all for you kind response and time.
After searching while I found my answer. Here I add my answer just to get someone help.
I had a silly mistake. The issue was not with the scrollview.
I took a tap gesture recognizer in both storyboard and programatically. Which was make delay in button execution.
I removed it from both which make my code run perfectly and without any delay.
Thank you once again.

Custom UIBarButtonItem highlight issue

I have a toolbar with 5 buttons.
4 of them are regular bar button items and one of them is a custom one (A 'UIButton' inside a 'UIBarButtonItem').
I noticed that when I click between the regular buttons (not exactly on them), one of them (the closest one) still recieves the click event and is being highlighted (which is what I want).
But the custom bar button item does not show this behaviour.
When I tap between it and one of the regular buttons neither of the 2 receives the touch event. This probably because the UIButton is the one the gets the click event. Is there a way to add a touch event the containing bar button item as well? Or perhaps another way to solve this?
Thanks!
button.userInteractionEnabled = YES; I believe is the answer.
One solution might be that you create image of same as bar button item and assign to UIButton as background image, this will solve your issue.
Hope this helps you....
Solved it!
I added another UIView to the UIBarButtonItem and then I added the UIButton to the UIView.
I added a touch gesture to the UIView (And also kept the original touch event of the UIButton) that expanded the area that you can touch the button which solved the problem.

One tap triggering events on multiple views?

In my program I have placed a UIButton as a subview of a UIView, both of which have userInteractionEnabled set to true. When the button is tapped, an event is called to handle the button tap, which works as expected. However, the button's UIView superview also handles an event which should not be triggered in this case. Can anybody explain why the UIButton AND UIView are both triggering an event? Any help is appreciated.
Should you resign first responder after the button has done its duty so that the view that the button is in is no longer in control.
How about adding: bringSubviewtoFront:scrollview
to the end of your UIButton code. The idea here is to make your uiview the foremost view; which is what I think is happening when you touch the uiview and thus reactivating the triggering events
I have found a solution: I simply made the button a superview of the view which was handling unwanted events, rather than a subview.

dragging and dropping a button in iphone

I need to drag and drop UIButtons(image) on bigger UIImageView. I want to implement following functionality -
While dragging if the button is inside UIImageView then only it should drop,else it should not drop
When touching down and dragging it should immediately create a new UIButton at original position.
After the UIButton is dropped it should not move from its new position.
Thanking in advance.
I'm not quite sure if this is what you're looking for but what you can do is start a timer on the touchDown action that will trigger a method that makes the button update to the current CGPoint touch location. Then add if statements to control whether it's inside an image or not. To make the button stop, use the touchUpInside method and invalidate the timer.

UIButton delayed state change

I have a UIButton subview inside of a UITableViewCell.
When this button is touched, the user must hold the button for about a half second for the button's image to change to the UIControlStateHighlighted image.
This means that if the user just taps the button as is usually the case, the highlighted state is never shown.
Why does this occur and how can I fix it?
I just encountered this problem and saw that this issue hadn't been closed. After screwing around for a while I found a fix for it.
Now you can fix this by turning off delaysContentTouches or unchecking the "Delays content touches" box on the tableview.
The only negative side effect is that the user won't be able to tap down on a button and initiate a scrolling gesture. However, if the user tries to scroll starting from anywhere that doesn't itself accept touches, the behavior should be the same as before.
The problem is that your UIButton is inside a UITableView. This means that the table view has to determine whether your tap is going to be a swipe or if it's just a tap intended for the button. The table view has to delay sending a message to the UIButton until it knows that the user doesn't intend to swipe and therefore scroll the view instead of pressing the button.
If you don't need a table view, get rid of the UITableView.
Up for David Hodge's answer.
I just want to add a way to remove that "only negative side effect", already described by David: if you start scrolling inside a UIcontrol in a UIScrollView with delayContentTouches=NO, scrolling doesn't work.
SOLUTION
Subclass UIScrollView (or UITableView as the original question) and override:
-(BOOL) touchesShouldCancelInContentView:(UIView *)view {
return YES;
}
Your UIControls inside UIScrollView/UITableView will change their state immediately on tap and the scrollviews will be able to scroll even if the touch starts on some UIControl. Works like a charm.
I just change the image from within the target action method:
[sender setBackgroundImage:[UIImage imageNamed:#"highlighted-image.png"] forState:UIControlStateNormal];
It changes the background image instantly.
Edit: completely re-written following a misunderstanding of the question
One way of thinking of a UIButton is as a shorthand way of setting up an area of the screen that can respond to various instantaneous touch events the response it makes is defined by UIControl's Target-Action system for delivering messages to other objects.
UIControlEventTouchDown sounds like the one you need to respond to. It will be triggered as soon as someone touches inside your button - this is what the "Contact Info" button in SMS does.
UIButton* myButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
// SEt up title, frame etc
[myButton addTarget:self action:#selector(myButtonWasPressed) forControlEvents: UIControlEventTouchDown];
[myMainView addSubView:myButton];
Will send a -(void)myButtonWasPressed message to the object this code runs from (ideally you view controller). In myButtonWasPressed you can then add a new view or take any action you like. The SMS app pushes a view controller to display the contact info using a navigation controller.
If this still doesn't solve your problem, you're going to have to post some code in order to get more insight into what's going wrong.