I have 5-10 diffrent buttons inside a UIScrollView.
I want to add a UILongPressGestureRecognizer to all the buttons inside my UIScrollView.
-(IBAction)CheckIfUserWantsToDoSomething:(id)sender {
HoldTimer = [NSTimer scheduledTimerWithTimeInterval:1.2 target:self selector:#selector(DoAction:) userInfo:nil repeats:NO];
}
-(void)DoAction:(id)sender {
[HoldTimer invalidate];
//My Code...
}
You can get all the UIScrollView's subviews, and filter out the UIButton's like so:
for (UIButton *button in myScrollView.superview.subviews) {
if ([button isKindOfClass[UIButton Class]]) {
}
}
Then you can create and add your UILongPressGestureRecognizer to those buttons.
UILongPressGestureRecognizer *holdRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(DoAction:)];
[holdRecognizer setMinimumPressDuration:2];
[button addGestureRecognizer:holdRecognizer];
Related
I have a UIView where i added a UITapGestureRecognizer:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
tapRecognizer.numberOfTapsRequired=1;
tapRecognizer.numberOfTouchesRequired=1;
[self.myView addGestureRecognizer:tapRecognizer];
I then add a UIToolBar with a button to the view:
UIToolbar *topBar = [[UIToolbar alloc ]initWithFrame:CGRectMake(0, 0, self.myView.frame.size.width, 44)];
topBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *logout = [[UIBarButtonItem alloc] initWithTitle:#"Logout" style:UIBarButtonItemStyleBordered target:self action:#selector(logout)];
[topBar setItems:#[logout] animated:NO];
I'm having an issue where I click on the logout button, and my tap recognier fires instead of my logout action. If I click and hold, then the logout action will fire (I'm guessing the tap recognizer is failing so lets the buttion action fire).
how can I not fire the gesture recognizer when the button is pressed?
Just had the same problem. Because I don't want to introduce container views (the UIToolbar should cover my existing view). With the help of Patrick.Ji's coarsely pointing I came up with this:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view.superview isKindOfClass:[UIToolbar class]]) {
return NO;
}
return YES;
}
Don't forget to set the delegate of the gesture to self
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *mainTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(mainTapGesture:)];
mainTapGestureRecognizer.delegate = self;
[self.view addGestureRecognizer:mainTapGestureRecognizer];
}
Check the view in your tap recognizer. If it is your logout button, let the touch fail to pass it up the chain via super.
Alternatively, make sure your toolbar is not a subview of your view. Instead, have a container view containing with your toolbar and your content view, and add the gesture recognizer to this content view.
implement this delegate method of UIGestureRecognizer (remember to set your tapRecognizer.delegate = self)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch: (UITouch *)touch {
if ([touch.view isKindOfClass:[UIBarButtonItem class]])
{
return NO;
}
return YES;
}
I've got the View-Controller with a button. I want the new view to be loaded over my View-Controller when the button is pressed. It'd not replace the existing view, I want it to be smaller than the screen and hide when I tap out of the small view.
How should it be implemented in code?
- (IBAction)button:(id)sender
{
UIView *view2=[[UIView alloc]initWithFrame:CGRectMake(0,0,200,200)];
[self.view addSubview:view2];
UITapGestureRecognizer *Tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(Tapview)] autorelease];
[view2 addGestureRecognizer:Tap];
}
-(void)Tapview
{
[view2 removeFromSuperview];
}
Add Tap Gesture to self.view like this:
UITapGestureRecognizer *oneFinger =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(oneFingerAction:)] autorelease];
// Set required taps and number of touches
[oneFinger setNumberOfTapsRequired:1];
[oneFinger setNumberOfTouchesRequired:1];
// Add the gesture to the view
[[self view] addGestureRecognizer:oneFinger];
Add one BOOL flag in .h file; in ViewDidLoad method add this:
flag = FALSE;
Now I assume u have UIView *smallView which be added on screen like this:
[self.view addSubView:smallView];
flag = TRUE;
smallView.center = self.view.centre;
Now when tapped on self.view tap gesture action called
- (void)oneFingerAction:(UITapGestureRecognizer*)sender
{
if(sender.view == self.view)
{
if(flag){
if(smallView)
{
[smallView removeFromSuperView];
}
}
}
}
I have a UITableView where I want the user to be able to click two buttons, in the header of the section, one for adding a new record, and the other one for editing them, so I first tried using tableView viewForHeaderInSection and tableView heightForHeaderInSection like this:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView* v = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 360, 44)];
UIToolbar* tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 360, 44)];
UIBarButtonItem* spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem* addBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(onAddVehicleInterest:)];
UIBarButtonItem* editBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(onEditVehiclesInterest:)];
[tb setItems:[NSArray arrayWithObjects:spacer, addBtn, editBtn, nil]];
[v addSubview:tb];
return v;
}
- (IBAction)onAddVehicleInterest: (id) sender {
NSLog(#"Add");
}
- (IBAction)onEditVehiclesInterest:(id)sender {
NSLog(#"Edit");
}
When I run the app, I can see the three UIBarButtonItems correctly, and I can click them, but the NSLog line is never called. So I added a UIToolbar directly in the nib file, added the 3 UIBarButtonItem controls to it, and tied the onAddVehicleInterestand onEditVehiclesInterest to the respective buttons. But that doesn't work either...
So, as a last test, I added a UIButton to the nib and tied its Touch Up Inside event to one of the methods, and that works as expected. So I'm confused. What am I doing wrong?
I had a UITapGestureRecognizer on the parent view of the view controller. Once I removed the tap gesture, my UIBarButtonItems began to respond properly to all selectors.
I have also faced the same problem and I get it solved by changing:-
- (IBAction)onAddVehicleInterest: (id) sender {
to
- (IBAction)onAddVehicleInterest: (UIBarButtonItem *) sender {
I also dont know why it was not working earlier, but my problem solved with it.
You can try this.
Try this:
UIBarButtonItem* addBtn;
if([self respondsToSelector:#selector(onAddVehicleInterest:)])
{
addBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(onAddVehicleInterest:)];
}
UIBarButtonItem* editBtn
if([self respondsToSelector:#selector(onEditVehiclesInterest:)])
{
editBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(onEditVehiclesInterest:)];
}
if(addBtn && editBtn)
{
[tb setItems:[NSArray arrayWithObjects:spacer, addBtn, editBtn, nil]];
}
Currently I'm facing a problem, I would like to perform action when the UIImageView on my UITableViewCell had been tapped.
Question: How could I do it? Could any one show me the code, or any tutorial?
Thanks in advance!
This is actually easier than you would think. You just need to make sure that you enable user interaction on the imageView, and you can add a tap gesture to it. This should be done when the cell is instantiated to avoid having multiple tap gestures added to the same image view. For example:
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(myTapMethod:)];
[self.imageView addGestureRecognizer:tap];
[self.imageView setUserInteractionEnabled:YES];
}
return self;
}
- (void)myTapMethod:(UITapGestureRecognizer *)tapGesture
{
UIImageView *imageView = (UIImageView *)tapGesture.view;
NSLog(#"%#", imageView);
}
Try this
//within cellForRowAtIndexPath (where customer table cell with imageview is created and reused)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleImageTap:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:tap];
// handle method
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer
{
RKLogDebug(#"imaged tab");
}
make sure u have....
imageView.userInteractionEnabled = YES;
you can use a customButton instead UIImageView
A tap gesture recogniser can be very memory hungry and can cause other things on the page to break. I would personally reconmend you create a custom table cell, insert a button into the custom cell frame and in the tableview code set the self.customtablecell.background.image to the image you want. This way you can assign the button an IBaction to make it push to whatever view you want.
Use ALActionBlocks to action in block
__weak ALViewController *wSelf = self;
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithBlock:^(UITapGestureRecognizer *weakGR) {
NSLog(#"pan %#", NSStringFromCGPoint([weakGR locationInView:wSelf.view]));
}];
[self.imageView addGestureRecognizer:gr];
I'm trying to use this to add button to toolbar:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setToolbarHidden:NO animated:NO];
self.navigationController.toolbar.translucent = YES;
self.navigationController.toolbar.barStyle = UIBarStyleBlack;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0.0, 0.0, 10.0, 10.0);
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithCustomView:button];
NSMutableArray *a = [NSMutableArray arrayWithObject:infoButton];
[self.navigationController setToolbarItems:a];
}
But there is no button in toolbar when I started application! :(
Instead of setting the navigation controllers toolBarItems property try setting it in the currently displayed view controller like so:
[self setToolbarItems:[NSArray arrayWithObjects:infoButton, nil]];
Also adding infoButton to the toolBarItems will automatically retain it. So remember to place
[infoButton release];
at the bottom of your viewWillAppear method.