Can anyone give me an example about Touch drag enter to drag from a button to another that triggering both button's event.
And how does it work?
For example, I want to drag from Do to Fa that event of Do, Re, Mi, Fa are triggered.
Here is my code:
- (void) setupVC {
soundBankPlayer = [[SoundBankPlayer alloc] init];
[soundBankPlayer setSoundBank:#"Piano"];
arrMusicalNotes = [NSArray arrayWithObjects:#"60", #"62", #"64", #"65", #"67", #"69", #"71", #"72", nil];
}
#pragma mark - Setup Musical Note
- (IBAction)btnMusicalNoteclick:(id)sender {
int numOfNote = [[arrMusicalNotes objectAtIndex:((UIButton*)sender).tag] intValue];
NSLog(#"%i", numOfNote);
[soundBankPlayer queueNote:numOfNote gain:1.0f];
[soundBankPlayer playQueuedNotes];
}
- (IBAction)btnDragOut:(id)sender {
NSLog(#"Out");
}
Oh I've seen that when i hold click out of Simulator, the method btnDragOut is triggered. And when i drag from out of Simulator to the button, the method of this button is triggered.
Now I want the method btnDragOut is triggered when i drag out of a button (finger is still in Simulator). Anyone know that?
You can add UIPanGestureRecognizer to your view of your UIViewController subclass via Storyboard or via code in viewDidLoad method:
UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handleDrag:)];
[self.view addGestureRecognizer:gestureRecognizer];
Then you can add property in implementation file of your UIViewController subclass of current UIButton being dragged:
#interface YourViewController ()
#property (weak, nonatomic) UIButton *currentButton;
#end
Now in action method you can detect UIControlEventTouchDragEnter and UIControlEventTouchDragExit events as follows:
- (void)handleDrag:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint point = [gestureRecognizer locationInView:self.view];
UIView *draggedView = [self.view hitTest:point withEvent:nil];
if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {
if ([draggedView isKindOfClass:[UIButton class]] && !self.currentButton) {
self.currentButton = (UIButton *)draggedView;
NSLog(#"Enter: %ld", (long)self.currentButton.tag);
// send enter event to your button
[self.currentButton sendActionsForControlEvents:UIControlEventTouchDragEnter];
}
if (self.currentButton && ![self.currentButton isEqual:draggedView]) {
NSLog(#"Out: %ld", (long)self.currentButton.tag);
// send exit event to your button
[self.currentButton sendActionsForControlEvents:UIControlEventTouchDragExit];
self.currentButton = nil;
}
} else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
self.currentButton = nil;
}
}
Related
I'm having a hard time finding the right documentation for how to handle touch events in order to support similar behavior to the keyboard.
What I want is a button that when I long press it, it shows a custom view controller above the button, but I want the user to be able to drag their finger to one of the other buttons (without taking their finger off the screen).
I have the button with a long press and it's custom view controller all setup and working. What I can't figure is how to support dragging from the first button over to the other button in the view controller to be able to select it.
I've tried using a subclassed UIButton where I tried this:
[self addTarget:self action:#selector(onDragOver:) forControlEvents:UIControlEventTouchDragEnter];
But that doesn't work.
I also found this question How to track button selection after long press? which is precisely the functionality I'm trying to duplicate. But there are no answers.
Here's my solution. The trick is you have to use hitTest:.
First you add a gesture recognizer to the button that is a normal button - the button that you want to open a context menu / custom view controller.
Then in your gesture recognizer callback, you use hitTest: to figure out if the user is over a custom button of yours and update it's state manually.
- (id) init {
//add a long press gesture recognizer
UILongPressureGestureRecognizer * gesture = [[UILongPressureGestureRecognizer alloc] initWithTarget:self action:#selector(onLongTap:)];
[self.myButton addGestureRecognizer:gesture];
}
- (void) onLongTap:(UIGestureRecognizer *) gesture {
if(gesture.state == UIGestureRecognizerStateBegan) {
//display your view controller / context menu over the button
}
if(gesture.state == UIGestureRecognizerStateEnded) {
//gesture stopped, use hitTest to find if their finger was over a context button
CGPoint location = [gesture locationInView:self.view];
CGPoint superviewLocation = [self.view.superview convertPoint:location fromView:self.view];
UIView * view = [self.view.superview hitTest:superviewLocation withEvent:nil];
if([view isKindOfClass:[MMContextMenuButton class]]) {
//their finger was over my custom button, tell the button to send actions
MMContextMenuButton * button = (MMContextMenuButton *) view;
[self hideAndSendControlEvents:UIControlEventTouchUpInside];
if(self.draggedContextMenuButton == button) {
self.draggedContextMenuButton = nil;
}
}
if(self.draggedContextMenuButton) {
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
}
self.draggedContextMenuButton = nil;
}
if(gesture.state == UIGestureRecognizerStateChanged) {
//gesture changed, use hitTest to see if their finger
//is over a button. Manually have to tell the button
//that it should update it's state.
CGPoint location = [gesture locationInView:self.view];
CGPoint superviewLocation = [self.view.superview convertPoint:location fromView:self.view];
UIView * view = [self.view.superview hitTest:superviewLocation withEvent:nil];
if([view isKindOfClass[MMContextMenuButton class]]) {
MMContextMenuButton * button = (MMContextMenuButton *) view;
if(self.draggedContextMenuButton != button) {
[self.draggedContextMenuButton dragOut];
}
self.draggedContextMenuButton = button;
[button dragOver];
}
}
}
//////////////
#import "MMContextMenuButton.h"
#import "MMContextMenus.h"
#implementation MMContextMenuButton
- (id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
self.layer.cornerRadius = 4;
self.adjustsImageWhenHighlighted = FALSE;
self.adjustsImageWhenDisabled = FALSE;
self.backgroundColor = [UIColor clearColor];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor colorWithRed:0.435 green:0.745 blue:0.867 alpha:1] forState:UIControlStateNormal];
[self addTarget:self action:#selector(onHighlight:) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:#selector(onRelease:) forControlEvents:UIControlEventTouchUpOutside&UIControlEventTouchUpOutside];
return self;
}
- (void) onHighlight:(id) sender {
self.backgroundColor = [UIColor colorWithRed:0.435 green:0.745 blue:0.867 alpha:1];
}
- (void) onRelease:(id) sender {
self.backgroundColor = [UIColor clearColor];
}
- (void) hideAndSendControlEvents:(UIControlEvents) events {
[self dragOut];
[self sendActionsForControlEvents:events];
[[MMContextMenus instance] hideContextMenus];
}
- (void) dragOver {
self.highlighted = TRUE;
self.backgroundColor = [UIColor colorWithRed:0.435 green:0.745 blue:0.867 alpha:1];
}
- (void) dragOut {
self.highlighted = FALSE;
self.backgroundColor = [UIColor clearColor];
}
#end
I have been trying to implement a UITapGestureRecognizer that will dismiss a modal view controller by detecting a tap outside the modal view controller on iOS 8.
The issue I am seeing is that in landscape mode on iPad that the tap gesture is only recognised inside parts of the view controller. My modal view is 380 width x 550 height. When orientation is in landscape (with the home button at the right) the tap gesture is detected inside the bottom half of the modal view. When orientation is in landscape but with the home button at the left hand side the tap gesture is detected inside the top half of the modal view.
Problem 1: The tap gesture is only detected inside the modal view when it should be detected outside.
Problem 2: No detection in some areas of the view in landscape orientation.
Here is the code I used:
UIViewController+DismissOnTapOutside.h
#import <UIKit/UIKit.h>
#interface UIViewController (DismissOnTapOutside)
- (void)registerForDismissOnTapOutside; // Call in viewDidAppear
- (void)unregisterForDismissOnTapOutside; // Call in viewWillDisappear
#end
UIViewController+DismissOnTapOutside.m
#import "UIViewController+DismissOnTapOutside.h"
#import <objc/runtime.h>
static char gestureRecognizerKey;
static char gestureRecognizerDelegateKey;
#interface SimpleGestureRecognizerDelegate : NSObject <UIGestureRecognizerDelegate>
#end
#implementation SimpleGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return [otherGestureRecognizer isMemberOfClass:[UITapGestureRecognizer class]];
}
#end
#interface UIViewController ()
#property (assign, nonatomic) UIGestureRecognizer *gestureRecognizer;
#property (strong, nonatomic) SimpleGestureRecognizerDelegate *gestureRecognizerDelegate;
#end
#implementation UIViewController (DismissOnTapOutside)
- (void)setGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
objc_setAssociatedObject(self, &gestureRecognizerKey, gestureRecognizer, OBJC_ASSOCIATION_ASSIGN);
}
- (UIGestureRecognizer *)gestureRecognizer
{
return objc_getAssociatedObject(self, &gestureRecognizerKey);
}
- (void)setGestureRecognizerDelegate:(SimpleGestureRecognizerDelegate *)delegate
{
objc_setAssociatedObject(self, &gestureRecognizerDelegateKey, delegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIGestureRecognizer *)gestureRecognizerDelegate
{
id delegate = objc_getAssociatedObject(self, &gestureRecognizerDelegateKey);
if (!delegate)
{
delegate = [[SimpleGestureRecognizerDelegate alloc] init];
self.gestureRecognizerDelegate = delegate;
}
return delegate;
}
- (void)handleDismissTap:(UIGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateEnded)
{
UIView *view = self.navigationController.view ?: self.view;
// Passing nil gives us coordinates in the window
CGPoint location = [gesture locationInView:nil];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"8.0")) {
if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
location = CGPointMake(location.y, location.x);
}
}
// Then we convert the tap's location into the local view's coordinate system
location = [view convertPoint:location fromView:self.view.window];
if (![view pointInside:location withEvent:nil])
{
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
}
}
- (void)registerForDismissOnTapOutside
{
// This approach is attributed to Danilo Campos:
// http://stackoverflow.com/a/6180584/456434
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDismissTap:)];
recognizer.delegate = self.gestureRecognizerDelegate;
recognizer.numberOfTapsRequired = 1;
recognizer.cancelsTouchesInView = NO;
self.gestureRecognizer = recognizer;
[self.view.window addGestureRecognizer:recognizer];
}
- (void)unregisterForDismissOnTapOutside
{
[self.view.window removeGestureRecognizer:self.gestureRecognizer];
self.gestureRecognizer = nil;
}
#end
The key method that should set the correct location for tap detection does not seem to be setting the location correctly in landscape orientation in iOS 8:
- (void)handleDismissTap:(UIGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateEnded)
{
UIView *view = self.navigationController.view ?: self.view;
// Passing nil gives us coordinates in the window
CGPoint location = [gesture locationInView:nil];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"8.0")) {
if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
location = CGPointMake(location.y, location.x);
}
}
// Then we convert the tap's location into the local view's coordinate system
location = [view convertPoint:location fromView:self.view.window];
if (![view pointInside:location withEvent:nil])
{
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
}
}
All of the examples I have looked at on StackOverflow in relation to this issue are adamant that the above code works. But in my case it simply does not detect taps outside the modal view controller.
How can a tap outside a modal be detected in iOS 8 on iPad in landscape orientation?
I have done code for Scrollview(8images) + Toolbar as well. When I try to click the toolbar button item the scrollview is not turned back as expected. below is the code.
When I click Toolbar button, no action triggered to "IBAction clickprjinfo", I have confirm done "SentAction" to this IBACTION via Barbuttonitem in connection inspector.
during runtime, when i click hold toolbar button + touch on scroolview screen + then release button, then it trigger the IBACTION.
Anyone help to notify my mistake or better way to understand this.
-(void)viewDidLoad
{
[super viewDidLoad];
[self pgcontrolview];
}
-(void) pgcontrolview {
pageC.numberOfPages=8;
pageC.currentPage=0;
for (int i=1; i<=8; i++)
{
UIImageView *images=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.jpg",i]]];
images.frame=CGRectMake((i-1)*1024, 0, 1024, 760);
[scroller addSubview:images];
}
scroller.delegate=self;
scroller.contentSize=CGSizeMake(1024*8, 760);
scroller.pagingEnabled=YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handle_Tap:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:tap];
}
-(IBAction)clickprjinfo:(id)sender{
pageC.currentPage=1;
CGRect frame=scroller.frame;
frame.origin.x=0;//frame.size.width*page;
frame.origin.y=0;
[scroller scrollRectToVisible:frame animated:YES];
}
Add Gesture delegate methods as below
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// test if our control subview is on-screen
if ([touch.view isKindOfClass:[UIControl class]]) {
// we touched a button, slider, or other UIControl
return NO; // ignore the touch
}
return YES; // handle the touch
}
Because it considering UIButton tapping as UITapGesture, so you don't need to allow UITapGesture on UIButton as the above delegate method ignore touch on UIControl (which in-turn UIButton) as allow to perform
-(IBAction)clickprjinfo:(id)sender action
I have added 20 subviews to scrollview line by line as rows
yPos=0;
for (int i=0; i<24; i++) {
UIView *timeView=[[UIView alloc]initWithFrame:CGRectMake(71, yPos, 909, 60)];
timeView.userInteractionEnabled=TRUE;
timeView.exclusiveTouch=YES;
timeView.tag=i;
NSLog(#"sub vieww tag=:%d",timeView.tag);
timeView.backgroundColor=[UIColor whiteColor];
UILabel *lbltime=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 70, 60)];
lbltime.text=#"VIEW HERE";
lbltime.textColor=[UIColor grayColor];
// [timeView addSubview:lbltime];
[scrlView addSubview:timeView];
yPos=yPos+61;
}
Now when ever I tap on a subview I am not getting the tapped subview properties.
like coordinates. It is giving parent view coordinates
I enabled subview UserInteractionEnabled to Yes.
Can any one tell me how to get tapped subview coordinate and tag value?
DO NOT subclass from UIScrollView, that's exactly why there are gesture recognizers. Also, DO NOT add a separate gesture recognizer to each view.
Add one gesture recognizer to your scroll view, and when it's clicked use the x,y values of the touch to calculate which view was clicked.
You'll need to do a small calculation: (y value of the click + UIScrollView y offset) / 60.
60 is the height of each view. This should return the index of the clicked view.
EDIT:
Code example:
- (void)viewTapped:(UIGestureRecognizer*)recognizer
{
CGPoint coords = [recognizer locationInView:recognizer.view];
int clickedViewIndex = (self.offset.y + coords.y) / 60;
// now clickedViewIndex contains the index of the clicked view
}
UIView *v = recognizer.view;
int tagNum = [v tag];
Using the tagNum you can do your further operatins.
Or v is your object of tapped view.
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
tap.numberOfTapsRequired = 1;
[timeview addGestureRecognizer:tap];
Add this in for loop only.
Make a class extending UIScrollView :
For example :
.h file :
#protocol CustomScrollViewDelegate <NSObject>
#optional
// optional becuase we always don't want to interact with ScrollView
- (void) customScrollViewTouchesEnded :(NSSet *)touches withEvent:(UIEvent *)event;
- (void) customScrollViewDidScroll;
#end
#interface CustomScrollView : UIScrollView
#property (weak, nonatomic) id<CustomScrollViewDelegate> touchDelegate;
// delegate was giving error becuase name is already there in UIScrollView
#end
Code for .m file :
#import "CustomScrollView.h"
#implementation CustomScrollView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesEnded");
if (!self.dragging) {
//NSLog(#"touchesEnded in custom scroll view");
if (_touchDelegate != nil) {
if ([_touchDelegate respondsToSelector:#selector(customScrollViewTouchesEnded:withEvent:)]) {
[_touchDelegate customScrollViewTouchesEnded:touches withEvent:event];
}
}
}
else {
// it wouldn't be called becuase at the time of dragging touches responding stops.
[super touchesEnded:touches withEvent:event];
}
}
#end
Implementing this use subview of scroll view, it will work
for (UILabel *label in [customScrollView subviews]) { // change name of table here
if ([label isKindOfClass:[UILabel class]]) {
if (label.tag == savedtag) { // use your tag
// write the code as desired
}
}
}
I'm trying to figure out why I get an error when I use locationOfTouch:inView. Eventually I created a new view with just the locationOfTouch call and I still get a SIGABRT whenever I touch the view.
Aside from import statements, here's all the code in my view:
#interface Dummy : UIView <UIGestureRecognizerDelegate> {
UIPanGestureRecognizer *repositionRecognizer;
}
#end
Here's the impl:
#implementation Dummy
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
repositionRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:#selector(reposition:)];
[repositionRecognizer setDelegate:self];
[self addGestureRecognizer:repositionRecognizer];
self.backgroundColor = [UIColor grayColor];
}
return self;
}
- (void)reposition:(UIGestureRecognizer *) gestureRecognizer {
[gestureRecognizer locationOfTouch:0 inView:self];
//[gestureRecognizer locationInView:self];
}
#end
If I use locationInView, it works okay. If I use locationOfTouch:inView, the program aborts as soon as the touch ends.
EDIT: On the console, with this class, no error messages are shown. The IDE points to main.m with a SIGABRT. Clicking on 'Continue' results in 'EXC_BAD_INSTRUCTION'. Screenshot available on http://imageshack.us/photo/my-images/849/consolel.png/
This crashes because it assumes that there is a touch zero. You need to confirm there is one first, like so:
- (void)reposition:(UIGestureRecognizer *) gestureRecognizer {
if (gestureRecognizer.numberOfTouches > 0){
CGPoint point = [gestureRecognizer locationOfTouch:0 inView:self];
NSLog(#"%#",NSStringFromCGPoint(point));
}
}
Think of the "locationOfTouch:" part as saying "touchAtIndex:", if the array of touches is empty(When you lift your finger or move it off the screen) then there is no touchAtIndex:0.