I have a UICollectionViewCell with a button/UIView inside of it.
It requires 2 taps to execute a view with TapGestureRecognizer:
singleTapBG.numberOfTapsRequired = 1;
self.btnSubmit.userInteractionEnabled = YES;
[self.btnSubmit addGestureRecognizer:singleTapBG];
Remove the gesture recognizer and add a target to your button. This should solve the 2 taps issue and only require one.
self.btnSubmit.addTarget(self, action: #selector(myClass.pressed(_:)), forControlEvents: .TouchUpInside)
Related
Adding the tap gesture to the imageView present above the header and not getting inside the action
let headerTap = UITapGestureRecognizer(target: self, action: "headerClicked")
headerTap.delegate = self
headerTap.numberOfTapsRequired = 1
headerTap.numberOfTouchesRequired = 1
image.addGestureRecognizer(headerTap)
You can add UIButton instead of UIImageView it will reduce the tap gesture code I think.
I've got a program with slide out menu. There's Menu button(BarButtonItem) in the right position. When the view controller loads I'm doing next
_menuBarButton.target = self.revealViewController;
_menuBarButton.action = #selector(revealToggle:);
So, when I click on the this button the View Controller slides out to right and I see another View Controller.
So, I want to make the main view controller blur when it slides out. I've got a code how to do it blur, but I can't implement this code because when I tap on the bat button it runs revealToggle: selector.
I've tried:
1. To set action for bar button. And firstly blur view controller:
- (IBAction)menuBarButtonTapped:(UIBarButtonItem *)sender {
[self setBlurEffect];
_menuBarButton.target = self.revealViewController;
_menuBarButton.action = #selector(revealToggle:);
[self.menuBarButton.target performSelector:#selector(revealToggle:)];
}
But app crashes with "unrecognized selector" (the solutions of this problem doesn
t help too).
I've wanted to use willDisapear method but it doesn't run, because the main view controller doesn't disapear. It just slides out.
So, could you help me with this problem?
P.S. I'll be happy if you propose any other effects for main view controller except blur.
P.P.S. Sorry for many mistakes in question.
I've found a good answer for me. I don't use several ViewControllers to implement menu panel.
Now I'm using another UIView calls menuPanel. It contains tableView(UITableView) and blurView(UIView). The second one is under first one.
You can blur this view in to ways:
From the code using next method.
(void) setBlurEffect {// Add blur view
CGRect boundsView = self.someView.bounds;
UIVisualEffectView *tableViewVisualEffect = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
self.tableViewVisualEffect.frame = boundsView;
self.tableViewVisualEffect.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableViewVisualEffect.backgroundColor = [UIColor clearColor];
[UIView transitionWithView:self.view
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations: ^ {
[self.someView addSubview:self.tableViewVisualEffect];
}
completion: nil ];
// Here you can add visual effects to any UIView control.
// Replace custom view with navigation bar in above code to add effects to custom view.
}
Where someView is view you want to do blur(or any other effect)
From the story board with Visual Effect View.(I've chosen)
After all I've set tableView and blurView into menuPanelView as pinned to all sides of the menuPanel with 0 distance.
And the last thing I've done I'm changing the position of the menuPanelView with animation:
[self.menuPanelView setFrame:self.visibleTableViewFramePosition];
[UIView animateWithDuration:0.8
animations:^{
[self.menuPanelView setFrame:self.invisibleTableViewFramePosition];
} completion:nil];
Where invisibleTableViewFramePosition is CGRect variable contains position of the menuPanelView when it has to be invisible. You also need to create visibleTableViewFramePosition.
That's all. Hope it may be helpful for someone=)
I got a (stupid) question. I'm busy with a 'tap' app (i dont know how to name it) it's a game with a button in the middle of the screen and when a play start tapping the button it need's to count the click's.
Does anoyone know what i need to use (classes) in objective-c
Thanks
Hook up an action to the tap event :
[button addTarget:self action: #selector(button_tapped) forControlEvents: UIControlEventTouchUpInside];
And increment a counter when tapped (tapCount is a property of your view controller)
- (void)button_tapped
{
self.tapCount += 1;
}
I have a popover that contains a UITableView. This UITableView has a cell with a text field in it:
alt text http://cl.ly/1b50a21ca8202d22db1b/content
When the popover opens near the bottom of the screen, and I tap the text field to edit it, the keyboard comes up, and the popover moves up to avoid being covered by the keyboard. But as it moves up, the table view in the popover scrolls up out of bounds:
alt text http://cl.ly/4fe64fbfe9518f20560d/content
I can scroll it back down, but how do I prevent this from happening.
I've come to the conclusion that this is a bug. I have filed a bug report with Apple (rdar://8156616) as well as a report on OpenRadar.
For anyone interested, here is a sample project that demonstrates the issue.
Try disabling scrolling on the table view.
[self.tableView scrollingEnabled:NO];
How are you setting the content view of your pop over controller. Please try editing the auto-resizing mask of the content view and set it from top-left.
Hope this helps.
Thanks,
Madhup
Perhaps you can set the UITableView's contentSize manually? So that there is no excess for it to scroll up.
I had the same problem but when hiding the keyboard and reloading the table view!
There is one solution for this problem! What you have to do is first hide the keyboard and reload the table view or change the table view in method recieving keyboard did hide notification!
At first I called
[textView resignFirstResponder]; or [textField resignFirstResponder];
and then
-(void)keyboardDidHide:(NSNotification *)notif {
//Check some conditions if you want
[tableView reloadData];
}
You need to listen to the keyboard will show notification:
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardStatusForPopover(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
Then you have to see if the keyboard frame intersects with the popover and subtract the values added from the keyboard to the popover container frame:
#objc func handleKeyboardStatusForPopover(_ notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if keyboardSize.intersects((self.resultPopOverTableViewController!.view.superview!.superview!.frame).insetBy(dx: 0, dy: 60)) {
let popOverContainer = viewControllerDisplayedbyPopover?.view.superview!.superview!
// cellDisplayingDetailInfo = sourceview of popover
popOverContainer!.frame.origin.y = popOverContainer!.frame.minY - keyboardSize.height + cellDisplayingDetailInfo.frame.maxY
}
}
}
In one of my iPhone projects, I have three views that you can move around by touching and dragging. However, I want to stop the user from moving two views at the same time, by using two fingers. I have therefore tried to experiment with UIView.exclusiveTouch, without any success.
To understand how the property works, I created a brand new project, with the following code in the view controller:
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
UIButton* a = [UIButton buttonWithType:UIButtonTypeInfoDark];
[a addTarget:self action:#selector(hej:) forControlEvents:UIControlEventTouchUpInside];
a.center = CGPointMake(50, 50);
a.multipleTouchEnabled = YES;
UIButton* b = [UIButton buttonWithType:UIButtonTypeInfoDark];
[b addTarget:self action:#selector(hej:) forControlEvents:UIControlEventTouchUpInside];
b.center = CGPointMake(200, 50);
b.multipleTouchEnabled = YES;
a.exclusiveTouch = YES;
[self.view addSubview:a];
[self.view addSubview:b];
}
- (void)hej:(id)sender
{
NSLog(#"hej: %#", sender);
}
When running this, hej: gets called, with different senders, when pressing any of the buttons - even though one of them has exclusiveTouch set to YES. I've tried commenting the multipleTouchEnabled-lines, to no avail. Can somebody explain to me what I'm missing here?
Thanks,
Eli
From The iPhone OS Programming Guide:
Restricting event delivery to a single view:
By default, a view’s exclusiveTouch property is set to NO. If you set
the property to YES, you mark the view so that, if it is tracking
touches, it is the only view in the window that is tracking touches.
Other views in the window cannot receive those touches. However, a
view that is marked “exclusive touch” does not receive touches that
are associated with other views in the same window. If a finger
contacts an exclusive-touch view, then that touch is delivered only if
that view is the only view tracking a finger in that window. If a
finger touches a non-exclusive view, then that touch is delivered only
if there is not another finger tracking in an exclusive-touch view.
It states that the exclusive touch property does NOT affect touches outside the frame of the view.
To handle this in the past, I use the main view to track ALL TOUCHES on screen instead of letting each subview track touches. The best way is to do:
if(CGRectContainsPoint(thesubviewIcareAbout.frame, theLocationOfTheTouch)){
//the subview has been touched, do what you want
}
I was encountering an issue like this where taps on my UIButtons were getting passed through to a tap gesture recognizer that I had attached to self.view, even though I was setting isExclusiveTouch to true on my UIButtons. Upon reviewing the materials here so far, I decided to put some code in my tap gesture code that checks if the tap location is contained in any UIButton frame and if that frame is also visible on the screen at the same time. If both of those conditions are true, then the UIButton will already have handled the tap, and the event triggered in my gesture recognizer can then be ignored as a pass through of the event. My logic allows me to loop over all subviews, checking if they are of type UIButton, and then checking if the tap was in that view and the view is visible.
#objc func singleTapped(tap: UITapGestureRecognizer)
{
anyControlsBreakpoint()
let tapPoint = tap.location(in: self.view)
// Prevent taps inside of buttons from passing through to singleTapped logic
for subview in self.view.subviews
{
if subview is UIButton {
if pointIsInFrameAndThatFrameIsVisible(view: subview, point: tapPoint)
{
return // Completely ignores pass through events that were already handled by a UIButton
}
}
}
Below is the code that checks if point was inside a visible button. Note that I hide my buttons by setting their alpha to zero. If you are using the isHidden property, your logic might need to look for that.
func pointIsInFrameAndThatFrameIsVisible(view : UIView, point : CGPoint) -> Bool
{
return view.frame.contains(point) && view.alpha == 1
}