I have two UIViews. On second "UIView" I have one UIButton, but it must be over the both views.
When I click lower middle UIButton - event is fire, but when I click above the middle - nothing works.
How to fix it?
example image
move the button outside of both views and center it vertically
You should create a subclass of UIView for UIView2 and override hitTest like this:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if (!self.clipsToBounds && !self.hidden && self.alpha > 0) {
for (UIView *subview in self.subviews.reverseObjectEnumerator) {
CGPoint subPoint = [subview convertPoint:point fromView:self];
UIView *result = [subview hitTest:subPoint withEvent:event];
if (result != nil) {
return result;
}
}
}
return nil;
}
Related
I have UIBUtton in UICollectionViewCell as show in image. I had set touchUpInside Action in xib. I had set background image of button is pink and lable as yellow. UIButton comes at top of the label. Now the problem is that UIButton in not getting touch event in pink area it only get touch events on orange area. why it is so.
- (IBAction)checkButtonTap:(id)sender event:(id)event
{
DLog(#"%s",__func__);
}
O a hidden view is overlapping the button due to which it is touch event was not reaching to the button.
Just to be sure I understand your issue, you have your UICollectionView in yellow and your UIbutton in pink, correct ?
If so, it seems you want to intercept the touchUpInside event outside your button's superview in yellow. You can look at this answer which deal with kind of problem.
Even if you finally find the solution to your problem, to clarify my answer, if you want to interact with an UIButton which is not inside its superview's frame, you may need to need to implement the method - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event of the superview (here the UICollectionViewCell):
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if (!self.clipsToBounds && !self.hidden && self.alpha > 0) {
for (UIView *subview in self.subviews.reverseObjectEnumerator) {
CGPoint subPoint = [subview convertPoint:point fromView:self];
UIView *result = [subview hitTest:subPoint withEvent:event];
if (result != nil) {
return result;
}
}
}
return nil;
}
(thanks Noam!)
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[buttonname addGestureRecognizer:tapGesture];
buttonname.userInteractionEnabled = YES;
(IBAction)imageTapped:(UITapGestureRecognizer *)gestureRecognizer {
if(gestureRecognizer.state == UIGestureRecognizerStateRecognized) {
[buttonname becomeFirstResponder];
}
}
O sorry, there was hidden view that is overlapping the button due to which touch event was not reaching to the button.
I'm creating a container view controller where the child view controllers are shown like a paged scrollView. In this container controller I want to change between pages scrolling horizontally using two fingers. So I used a UIScrollView and I have set it like that:
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.delegate = self;
[self.scrollView setContentOffset:[self rectForPage:1].origin];
for (UIGestureRecognizer *gestureRecognizer in self.scrollView.gestureRecognizers) {
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
UIPanGestureRecognizer *panGR = (UIPanGestureRecognizer *) gestureRecognizer;
panGR.minimumNumberOfTouches = 2;
}
}
I also have implemented the - (void)scrollViewDidScroll:(UIScrollView *)sender method to avoid the vertical scrolling
- (void)scrollViewDidScroll:(UIScrollView *)sender {
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x +pageWidth/2) / pageWidth);
self.pageControl.currentPage = page;
[self.scrollView setContentOffset: CGPointMake(self.scrollView.contentOffset.x, 0)];
}
Now the problem is that the childViewControllers subviews can't receive the touches from the user. I want the single touch to be passed throw the view hierarchy. I have read about subclassing UIScrollView to overwrite the method - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view but it's only called if the subviews are UIControl. How I can do it?
The purpose is that this ChildViewControllers have UIButtons inside and ScrollViews that should be used with singleTouches
maybe not exactly, but this might help you. Create a scroll view subclass and implement this;
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
// UIView will be "transparent" for touch events if we return NO
for (id subview in self.subviews) {
if ([subview isKindOfClass:[YourViewController class]]) {
YourViewController *VC = (YourViewController*) subview;
if (CGRectContainsPoint(YourViewController.frame, point)) {
return YES;
}
}
}
return NO;
}
I'm dynamically creating buttons on UIView.
I can move them by dragging using this code
- (IBAction)draggedOut: (id)sender withEvent: (UIEvent *) event: (NSSet *)touches {
UIButton *selected = (UIButton *)sender;
selected.center = [[[event allTouches] anyObject] locationInView:hallView];
}
I can have more then one button. When I'm dragging some button, I need to check are there any intersections with other buttons?
How can I do this?
You should cycle through the other buttons in the view and, for each of them, check whether in intersect the dragged one via:
CGRectIntersectsRect (draggedButton.frame, anotherButton.frame);
You could use a function like:
- (BOOL)isThereButtonsIntersectionInView:(UIView *)containerView
forButton:(UIButton *)draggedButton
{
for(UIView *view in containerView.subviews){
if([view isKindOfClass:[UIButton class]] &&
view != draggedButton &&
CGRectIntersectsRect (view.frame, draggedButton.frame)){
return YES;
}
}
}
return NO;
}
Call this method passing as a parameter the view containing the buttons and the dragged button.
You can get frames of buttons and check using this
if(CGRectIntersectsRect(firstButton.frame, secondButton.frame)) {
return YES;
}
You simply need to compare the frame of this button with the frame of all the other buttons.
Here is a bit of sample code, which should probably go in your view controller because it requires knowledge of the other buttons.
- (BOOL) button:(UIButton*)button intersectsWithButtons:(NSArray*)moreButtons
{
CGRect buttonFrame = button.frame;
for (UIButton *checkButton in moreButtons) {
if (button != checkButton && CGRectIntersectsRect(buttonFrame, checkButton.frame))
return YES;
}
return NO;
}
I have the following structure of the views
UIView(1)
|--> UIScrollView
|-----------> UIView(2)
|------> UIButton
hitTest in UIView(1) has been overridden with the following:
- (UIView *)hitTest:(CGPoint) point withEvent:(UIEvent *)event
{
/* this view should have only view only: the scroll view */
UIView * vv = [[self subviews] objectAtIndex:0];
if ([vv pointInside:point withEvent:event])
{
UIView *rv = [vv hitTest:point withEvent:event];
return rv;
}
if ([self pointInside:point withEvent:event])
{
return vv;
}
return nil;
}
This is required in order to catch UIScrollView dragging outside of the scroll view itself.
The issue is that when the UIButton is clicked, the event attached to it does not fire. The view returned from the hitTest is UIView(2).
How can I make the UIButton to respond to clicking event?
Edit
After suggestion of Mundi, I exchanged hitTest with the following and it works. Of course, I forgot to call [super hitTest:].
- (UIView *)hitTest:(CGPoint) point withEvent:(UIEvent *)event
{
/* this view should have only view only: the scroll view */
UIView * scrv = [self findScrollView];
UIView *superView = [super hitTest:point withEvent:event];
if (superView == self)
{
return scrv;
}
return superView;
}
How about calling [super hitTest:point withEvent:event] at the appropriate point?
I have a UIWindow with a text field, a button, and a table.
I would like to be able to track all the touches on the screen and then forward them to the element being touched.
I have read about overriding sendEvent in the Apple documentation but I still do not understand:
How to use hitTest to retrieve the element being touched
How to forward touches
This is what I have so far.
- (void) sendEvent:(UIEvent *)event
{
for (UITouch *touch in [event allTouches])
{
/* Get coordinates of touch */
CGPoint point = [touch locationInView:self];
/* Get subview being touched */
/* something like this???
UIView *receiver = [self hitTest:point withEvent:event];
*/
/* Forward touch event to right view */
/* how??? */
}
[super sendEvent:(UIEvent *)event];
}
Thank you.
I am not sure this is the best solution but I am following what has been posted here.
Basically I have a subclass of UIView covering the entire space. Such class contains a reference to ALL the elements that can be touched. (I wish there was a way to avoid that)
This is the code in the header
#interface SubclassUIView : UIView {
UITextField *text;
UITableView *table;
UIButton *button;
UIToolbar *toolbar;
}
And this is the implementation:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
CGPoint tableHit = [table convertPoint:point fromView:self];
CGPoint buttonHit = [button convertPoint:point fromView:self];
CGPoint toolbarHit = [toolbar convertPoint:point fromView:self];
CGPoint messageHit = [text convertPoint:point fromView:self];
if ([table pointInside:tViewHit withEvent:event]) return table;
else if ([button pointInside:buttonHit withEvent:event]) return button;
else if ([toolbar pointInside:toolbarHit withEvent:event]) return toolbar;
else if ([text pointInside:messageHit withEvent:event]) return text;
return [super hitTest:point withEvent:event];
}