I have 2 viewControllers and in first one I'm using tapRecognizer to press and hold in order to show a UImenucontroller to copy a string. The tap is used for selecting the title on navigation bar, and it shows a UImenucontroller with copy item on it.
It works for the first time, but when user switch to another view controller and come back to the first view controller again, the menu does not show any more.
-(void)viewDidLoad{
[super viewDidLoad];
UIView *viewWithTitleLabel = self.navigationController.navigationBar.subviews[1];
viewWithTitleLabel.userInteractionEnabled = YES;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(topBarTitleTap:)];
[viewWithTitleLabel addGestureRecognizer:longPress];
}
-(void)topBarTitleTap:(UILongPressGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
UIMenuController *menuController = [UIMenuController sharedMenuController];
[menuController setTargetRect:CGRectMake(CGRectGetMidX([self.view bounds]), -12.0, 0.0f, 0.0f) inView:self.view];
[menuController setMenuVisible:YES animated:YES];
}
}
- (void) copy:(id) sender {
// called when copy clicked in tab bar title
NSString *copyStringverse = self.navigationItem.title;
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:copyStringverse];
}
- (BOOL) canBecomeFirstResponder {
return YES;
}
Add [self becomeFirstResponder]; before pop UIMenuController
For example you can change your code as follow
-(void)topBarTitleTap:(UILongPressGestureRecognizer *)gestureRecognizer
{
[self becomeFirstResponder];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
UIMenuController *menuController = [UIMenuController sharedMenuController];
[menuController setTargetRect:CGRectMake(CGRectGetMidX([self.view bounds]), -12.0, 0.0f, 0.0f) inView:self.view];
[menuController setMenuVisible:YES animated:YES];
}
}
And don't forget to implement
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
//Customize your action if statement here
return YES;
}
For your viewcontroller
Check if LongPressGestureRecognizer is working, every time.
I would place the gesturerecognizer code in viewDidAppear instead of ViewDidLoad, just to be safe
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
In my app users can send messages to each other. I use UITextView inside of a bubble image to display the chat history.
[messageTextView setFrame:CGRectMake(padding, padding+5, size.width, size.height+padding)];
[messageTextView sizeToFit];
messageTextView.backgroundColor=[UIColor clearColor];
UIImage *img = [UIImage imageNamed:#"whiteBubble"];
UIImageView *bubbleImage=[[UIImageView alloc] initWithImage:[img stretchableImageWithLeftCapWidth:24 topCapHeight:15]];
messageTextView.editable=NO;
[bubbleImage setFrame:CGRectMake(padding/2, padding+5,
messageTextView.frame.size.width+padding/2, messageTextView.frame.size.height+5)];
[cell.contentView addSubview:bubbleImage];
[cell.contentView addSubview:messageTextView];
Currently, when a user holds down on the message text, they see the 'Copy' and 'Define' options with cursors to select text.
However, I would rather have the basic iOS messaging option of holding down on a chat bubble to copy the entire message. How can this be achieved?
I would subclass UITextView to implement your own version of the copy menu. You can do it a number of ways, but one possible way is like below.
The basic idea is that the text view sets up a UILongPressGestureRecognizer that will create the popup menu when a long press is detected.
UILongPressGestureRecognizer has several default system menus that will show up unless you tell them not to. The way to do that is to return NO for any selectors that you don't want to handle in canPerformAction:withSender:. In this case, we're returning NO for any selector except for our custom copyText: selector.
Then that selector just gets a reference to the general UIPasteboard and sets it's text to the text of the TextView.
In your subclass's implementation:
#implementation CopyTextView
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
- (instancetype)init
{
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (void)setup {
self.editable = NO;
self.selectable = NO;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressDetected:)];
longPress.minimumPressDuration = 0.3f; // however long, in seconds, you want the user to have to press before the menu shows up
[self addGestureRecognizer:longPress];
}
- (void)longPressDetected:(id)sender {
[self becomeFirstResponder];
UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
if (longPress.state == UIGestureRecognizerStateEnded) {
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:#"Copy" action:#selector(copyText:)];
UIMenuController *menuCont = [UIMenuController sharedMenuController];
[menuCont setTargetRect:self.frame inView:self.superview];
menuCont.arrowDirection = UIMenuControllerArrowDown;
menuCont.menuItems = [NSArray arrayWithObject:menuItem];
[menuCont setMenuVisible:YES animated:YES];
}
}
- (BOOL)canBecomeFirstResponder { return YES; }
- (void)copyText:(id)sender {
UIPasteboard * pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:self.text];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == #selector(copyText:)) return YES;
return NO;
}
#end
Useful documentation:
UILongPressGestureRecognizer Documentation
UIMenuController Documentation
I referred various post on SO for the same issue. But still not able to get the solution.
I have sub-Class the UIButton where I am having UILongGestureRecognizer. My Implementation goes as below:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[self addGestureRecognizer:longGesture];
}
return self;
}
- (BOOL)becomeFirstResponder
{
return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
return YES;
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (void)longPress:(UILongPressGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan)
{
NSLog(#"ButtonView: longPress: event called");
UIMenuController *menu = [UIMenuController sharedMenuController];
if (![menu isMenuVisible])
{
ButtonView *btn = (ButtonView *)gesture.view;
if (![btn becomeFirstResponder])
{
NSLog(#"couldn't become first responder");
return;
}
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:#"Change Color" action:#selector(changeColor:)];
UIMenuController *menuCont = [UIMenuController sharedMenuController];
menuCont.arrowDirection = UIMenuControllerArrowDown;
menuCont.menuItems = [NSArray arrayWithObject:menuItem];
if([btn canBecomeFirstResponder])
{
[menuCont setTargetRect:btn.frame inView:btn.superview];
[menuCont setMenuVisible:YES animated:YES];
NSLog(#"menu visible....");
}
}
}
if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled || gesture.state == UIGestureRecognizerStateFailed)
{
[self.layer setBorderColor:[UIColor clearColor].CGColor];
[self.layer setBorderWidth:0.0];
}
}
I have override becomeFirstResponder and canBecomeFirstResponder.
Important to Note: My Log message "menu visible...." is getting logged when I long pressed the Button, But I am not able to see the UIMenuController visible.
Is there anything that I am still missing in above code?? Thanks in advance.
Add this line before showing the menu:
[self becomeFirstResponder];
And remove this code:
- (BOOL)becomeFirstResponder
{
return YES;
}
I am trying to use UIMenuCnotroller to show a list of dynamically generated items, they share the same action method, and so I need to know which item is selected in the single action method.
However, in the action method - (void)menuItemAction:(id)sender;the sender is actually the UIMenuController object, and I didn't find any method of UIMenuController can tell me which menuitem is selected.
One solution I can think of is to dynamically generate different action selectors for different items, and do some tricks in forwardInvocation
But is there any easier way?
You can use UIMenuCnotroller like:
1) creation:
UIMenuController *menuController = [UIMenuController sharedMenuController];
UIMenuItem *open = [[UIMenuItem alloc] initWithTitle:#"Open" action:#selector(open:)];
UIMenuItem *reDownload = [[UIMenuItem alloc] initWithTitle:#"Re-Download" action:#selector(reDownload:)];
[menuController setMenuItems:[NSArray arrayWithObjects:open, reDownload, nil]];
[menuController setTargetRect:cell.frame inView:self.view];
[menuController setMenuVisible:YES animated:YES];
[open release];
[reDownload release];
2) To catch actions should implement next methods:
- (BOOL) canPerformAction:(SEL)selector withSender:(id) sender
{
if (selector == #selector(open:))
{
return YES;
}
if (selector == #selector(reDownload:))
{
return YES;
}
return NO;
}
- (BOOL) canBecomeFirstResponder
{
return YES;
}
3) And realization of yours methods:
- (void) open:(id) sender
{
[self doSomething];
}
- (void) reDownload:(id) sender
{
[self doSomething];
}
Hope, this helps.
Okay, I've solved this one. It involves messing with [NSObject forwardInvocation:] and is a bit dirty, but the resulting code is quite minimal. Answered over here: https://stackoverflow.com/a/9874092/790036
One easiest way would be to use different #selector method for each menu item
Examples:
UIMenuItem *oneObj = [[UIMenuItem alloc] initWithTitle:#"One" action:#selector(One:)];
UIMenuItem *twoObj = [[UIMenuItem alloc] initWithTitle:#"Two" action:#selector(Two:)];
I have a UITableView with a gesture recognizer added:
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
[myTableView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.cancelsTouchesInView = NO;
... everything works fine when tapping on the tableview to dismiss the keyboard. My problem is, my hideKeyboard method also calls when tapping on the "clear" button on my UITextField. Very strange.
commentTextField = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 310, 35)];
commentTextField.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
commentTextField.borderStyle = UITextBorderStyleRoundedRect;
commentTextField.textColor = [UIColor blackColor]; //text color
commentTextField.font = [UIFont fontWithName:#"Helvetica" size:14.0]; //font size
commentTextField.placeholder = #"Enter a comment..."; //place holder
commentTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
commentTextField.keyboardType = UIKeyboardTypeDefault; // type of the keyboard
commentTextField.returnKeyType = UIReturnKeySend; // type of the return key
commentTextField.clearButtonMode = UITextFieldViewModeAlways; // has a clear 'x' button to the right
commentTextField.delegate = self;
[commentTextField setHidden:NO];
[commentTextField setEnabled:YES];
[commentTextField setDelegate: self];
hide keyboard method:
- (void) hideKeyboard{
if(keyboard){
[commentTextField resignFirstResponder];
[UIView animateWithDuration:.3
delay:.0
options:UIViewAnimationCurveEaseInOut
animations:^{ // start animation block
[myTableView setFrame:CGRectMake(0, myTableView.frame.origin.y + 216, myTableView.frame.size.width, myTableView.frame.size.height)];
}
completion:^(BOOL finished){
}];
keyboard = 0;
}
}
Any help would be appreciated, thanks!
The following is a little more general - it's not coupled to your specific views:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UITextField class]] ||
[touch.view isKindOfClass:[UIButton class]])
{
return NO;
}
return YES;
}
Also, don't forget to set the delegate for the gesture recognizer, and mark the class as implementing the UIGestureRecognizerDelegate protocol.
I had the same issue. I also implemented the following method in my view:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ((touch.view == self.textField) && (gestureRecognizer == self.tapGestureRecognizer))
{
return NO;
}
return YES;
}
But it still didn't work. So I set a break in the method and saw that when I click the field, touch.view would be set to it but when I clicked on the clear button it was coming in as a UIButton*. At that point it was obvious what was happening and what to do to fix it. The below resolves the issue.
if((touch.view == self.textField || [self.textField.subviews containsObject:touch.view]) && (gestureRecognizer == self.tapGestureRecognizer))
{
return NO;
}
My issue with Ezmodius' approach is that he depends on a property called 'textField' and my UIViewController is a controller from which all my other UIViewControllers inherit, so i needed to implement a more generic approach: whenever there was a textfield in any UIViewController that needed to be cleared, i implemented the following (inside the same gestureRecognizer method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UITextField class]] ||
([touch.view isKindOfClass:[UIButton class]] && [touch.view.superview isKindOfClass:[UITextField class]]))
{
return NO;
}
return YES;
}
So basically im checking whether it is a textfield or a button whose superview is a textfield (in this case, the clear button inside the textfield) and it works like a charm for me. I implemented this in my base UIViewController class and it works for every page where this happens.