I have a tableview cell that has an image view, a UILabel and a TextView. I want the user to be able to select text from the TextView when they longPress on the TextView.
I have created a Gesture recogniser for the cell, and when the user long presses on the cell, the gesture recogniser gets called, but I don't get a menu or a cursor for the user to be able to select text and then copy it. Below is the code to attach long press to the UITableView cell
UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(LongPressgesture:)];
[tableView addGestureRecognizer:longPressRecognizer];
I also have the following code that gets called
- (void)LongPressgesture:(UILongPressGestureRecognizer *)gesture{
if (gesture.state == UIGestureRecognizerStateEnded) {
NSLog(#"Long press Ended .................");
}
else {
NSLog(#"Long press detected .....................");
}
}
The function gets called, so I see the message "Long press Detected" on the output log, but I don't get the menu to copy text. I tried creating a menu on the Long Press section as this:
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:#"Copy" action:#selector(copyText:)];
UIMenuController *menuCont = [UIMenuController sharedMenuController];
[menuCont setTargetRect:CGRectMake(10, 100, 400, 400) inView:tableview];
menuCont.arrowDirection = UIMenuControllerArrowDown;
menuCont.menuItems = [NSArray arrayWithObject:menuItem];
[menuCont setMenuVisible:YES animated:YES];
But the copy text function is not called, and there is no menu.
Thanks!
Related
I need to animate table view cell text label.When user long press the cell the text should start moving from right to left as in music player (the name of song is moving from right to left).
Thanks for help
For what you want to do you need to disable selection for the tableView.
Add this on viewDidLoad: tableView.allowsSelection = NO;
after that add gesture recognizer to the labels:
cell.label = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(gestureAction:)];
[self.longPressGestureRecognizer setDelegate:self];
[cell.label addGestureRecognizer:self.longPressGestureRecognizer];
As for the Label, it should be wider than the width of the screen and only 1 line (that way the text will disappear).
- (void)gestureAction:(UIGestureRecognizer *)recognizer {
//in here you add the code with animation to move the position of the label
}
I have a UITableView which is populated with some cells. I have created a UIButton using the following snippet, it is placed next to one of the section headers.
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton addTarget:self action:#selector(addButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[addButton setBackgroundImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
addButton.backgroundColor = [UIColor clearColor];
addButton.frame = CGRectMake(270, 150, 29, 29);
The button is placed and works correctly. However, after the view is scrolled (even slightly - like 1 pixel), the button works once and then ceases to respond. When it fails to respond the action for when it is clicked is not triggered and the button doesn't give the 'depressed' shadow. The rest of the application runs as normal and it does not crash.
This seems odd because after I scroll the button is clickable once more before it stops working. The button is used to insert rows into the table, so after it is pressed there is an extra row, possibly this is breaking the bounds or something?
Button pressed function:
- (void)addButtonPressed {
self.addClientTable.editing = YES;
// First figure out how many sections there are
NSInteger lastSectionIndex = [self numberOfSectionsInTableView:self.addClientTable] - 1;
// Then grab the number of rows in the last section
NSInteger lastRowIndex = [self.addClientTable numberOfRowsInSection:lastSectionIndex];
[self.addClientTable insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex]] withRowAnimation:UITableViewRowAnimationRight];
self.addClientTable.editing = NO;
}
Where addClientTable is the UITableView.
What could cause a UIButton to stop responding to clicks and where in my scenario would this be caused by?
I am almost sure that your problem is that your button is out of it superview, and you are not using the clip subviews option in your view that contains the button, or in one of it superviews.
Set to true all the views property clip subviews and see if it appears your button. (We expect that the button disappear)
If you provide more code I can try to help you to solve this problem.
-
Reading again your question, another probable problem to it is that you have one view in front of your button. You can test it changing the background of your view, or something like that.
I'm working on a very small editor in iOS. It only has one view, one button and a textView. When the button gets pressed, a custom UIMenuController pops up with 3 options: toggle bold, toggle italics and toggle cursive.
This is working very well, however, if I press the button when the UITextView is the first responder, it also shows the two default menu items, named 'select' and 'select all'.
I want to get rid of them but I'm not sure how to do this. This is the code that gets called when the button is pressed:
- (IBAction)settingsPressed:(id)sender
{
UIMenuController *sharedController = [UIMenuController sharedMenuController];
UIMenuItem *menuItem1 = [[UIMenuItem alloc] initWithTitle:bold ? #"Bold off" : #"Bold on" action:#selector(toggleBold:)];
UIMenuItem *menuItem2 = [[UIMenuItem alloc] initWithTitle:italics ? #"Italics off" : #"Italics on" action:#selector(toggleCursive:)];
UIMenuItem *menuItem3 = [[UIMenuItem alloc] initWithTitle:underline ? #"Underline off" : #"Underline on" action:#selector(toggleUnderline:)];
NSArray *menuItems = #[menuItem1, menuItem2, menuItem3];
CGRect drawRect = [sender convertRect:[sender bounds] toView:self.view];
[sharedController setTargetRect:drawRect inView:self.view];
[sharedController setMenuItems:menuItems];
[sharedController setMenuVisible:YES animated:YES];
[sharedController setMenuItems:nil];
}
Can anyone explain me how to do this?
Thanks!
Make a subclass of UITextView. In your subclass, override canPerformAction:withSender: to return NO if the action is #selector(select:) or #selector(selectAll:). For more information:
Managing the Selection and Edit Menu
UIMenuController Class Reference
-[UIResponder canPerformAction:withSender:]
UIResponderStandardEditActions Protocol Reference
I have a TabBar that I've created through IB, I chose "create new project" -> "Tab bar application". Is there a way for me to access one of the TabBarItems for customization through the code?
It seems to me that something like:
[[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:#"Button one"];
should set the title of that item to "Button one", but it doesen't. The title itself is not a problem (I can set that through IB aswell), however adding an Icon seems to be.
So to sum up, what I really want to know is: Is there a way to add an Icon to a TabBarItem created through IB?
SOLUTION:
Adding in viewDidLoad in the first view, being loaded automatically upon starting the app:
UITabBarController *tb = [self tabBarController];
[[tb.tabBar.items objectAtIndex:1] setTitle:#"Title"];
Let me set the title of the second button (objectAtIndex: 1). I was also able to set the image the same way, which also worked for buttons one (objectAtIndex: 0) and three (objectAtIndex: 2).
Add this to your viewDidLoad: method of one of the tabBar viewControllers and it should work:
- (void)viewDidLoad
{
[super viewDidLoad];
//Get the tabBarItem
UITabBarItem *tbi = [self tabBarItem];
//Give it a lable
[tbi setTitle:#"Title A"];
//create a image from a file for the tabBar
UIImage *i = [UIImage imageNamed:#"NiceImage.png"];
//and put it on the tabBar
[tbi setImage:i];
}
You should be able to set the image and title properties on the TabBarItems:
UITabBarItem *item = (UITabBarItem *)[tabBarController.tabBar.items objectAtIndex:0];
item.image = [UIImage imageNamed:#"home.png"];
Don't forget that the UITabBar only uses the alpha values out of the image you set, so if you don't have an alpha channel in the image you may not see anything when you set an image on the tab bar item.
I've never created a tab bar through IB (always through code), however to set title and icon I use
controller.title = #"Controller";
controller.tabBarItem.image = [UIImage imageNamed:#"image.png"];
where controller is the UIViewController added to the viewControllers' array of the UITabBarController.
I'm making a game that involves have buttons move across the screen until the user taps them. What I have so far is buttons moving across being generated randomly. My problem is when a new button is created, the last one stops moving. I'm trying to stick them into an array, but I'm not sure that's really gonna keep them moving.
-(void) generateStickFig:(NSTimer *)timer {
int x = random() % 100;
NSMutableArray *enemies = (NSMutableArray *)timer.userInfo;
if (x == 1) {
stickFig = [[UIButton alloc] initWithFrame:CGRectMake(0, 650, 50, 50)];
[stickFig setBackgroundColor:[UIColor blackColor]];
[stickFig addTarget:self action:#selector(tapFig:) forControlEvents:UIControlEventTouchUpInside];
// [enemies setObject:object forKey:[NSString stringWithFormat:#"object%i",i]];
[enemies addObject:stickFig];
int b = [enemies count];
[self.view addSubview:stickFig];
arrayNum++;
}
CGPoint oldPosition = stickFig.center;
stickFig.center = CGPointMake(oldPosition.x + 1 , oldPosition.y);
}
Where the buttons are stored is immaterial to moving them. You need to have a routine which animates them. You can do this by hand by moving them a small amount in response to a timer tick. The easiest method there would be fast enumeration over your array.
So you'd have the generation routine you have in your statement, a movement routine, and a touch IBAction handler which removes the button from the array. The generation routine, and the movement routine would both be called in your timer tick handler. (I tend to call that method "handleTick")
In high level psuedo-code it'd be something like this:
//tick handler
handleTick:
one out a hundred times, make a new button
give it a random starting location
store it in the buttons array
every time:
for button in buttons:
move button a few pixels
//button touch handler
buttonWasTouched:button :
[buttons removeObject: button];
I'm just getting into the system provided animation, so I don't know if your button can accept touches while being animated, but I wouldn't be surprised.
In order to know what button was touched from the array, you can use the tag option for the button:
- (IBAction) buttonTouched:(id) sender withEvent:(UIEvent *) event
{
UIButton *btn = (UIButton *)sender;
// in my case, I store the position on the array as a tag for the button
NSUInteger index = btn.tag;
UIControl *control = sender;
// Now you know what button was touched
// Apply actions on to it: remove from superview, animate explosion...
}
Cheers