UITapGestureRecognizer Not Getting Called - ios

I am adding the following UITapGestureRecognizer to my view.
- (void)viewDidLoad {
_focusView = [[PBJFocusView alloc] initWithFrame:CGRectZero];
_focusTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(_handleFocusTapGesterRecognizer:)];
_focusTapGestureRecognizer.delegate = self;
_focusTapGestureRecognizer.numberOfTapsRequired = 1;
_focusTapGestureRecognizer.enabled = NO;
[_previewView addGestureRecognizer:_focusTapGestureRecognizer];
}
- (void)_handleFocusTapGesterRecognizer:(UIGestureRecognizer *)gestureRecognizer {
NSLog(#"Focus");
}

_focusTapGestureRecognizer should be enable. Try this
_focusTapGestureRecognizer.enabled = YES;
OR
Remove this line
_focusTapGestureRecognizer.enabled = NO;

Try this inside your code
_focusView.userInteractionEnabled = YES;
May be helpfull...

_focusTapGestureRecognizer.enabled = NO;
Remove this line

To make this work check these three things as it is suggested by other guys i only writing them step by step
Check did you set the delegate of TapGesture in .h file.
Check on the UIView you are adding gesture its userInteractionEnabled is True.
Make that enable of UITapGesture Yes .//Why it is no ?
Fourth is , did you gave any size to the UIView , for checking that add some background color to it,see is it visible or not .
if these 4 doesnot help you out type your code what you used it .

You might also want to check that you're not trying to reuse the UITapGestureRecognizer object for different views.
It seems like you can because you add the gesture to the view, but in ultimately the view needs to be assigned to the view property on the gesture object so adding the gesture object to a second view will just overwrite the first.

Related

Swift : How to open a link when tap on text in label?

I want to add a link to some text. At the moment the text is in a UILabel. I want the link to be on the text so the user would not visually see the link. How can this be done?
In web development it looks like this.
Example 1
UILabel is not made for that, but you can link the text with an action (like tapping) in order to do whatever you like. Don't know about swift but in Objective-C is something like:
// If you have UILabel* myLabel
myLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(myAction:)];
[myLabel addGestureRecognizer:gr];
gr.numberOfTapsRequired = 1;
gr.cancelsTouchesInView = NO;
And then you can add the action:
- (void) myAction: (UITapGestureRecognizer *) gr {
// Code here
}
Maybe you can figure out how to do this in swift
You can also make a button look invisible. This could create the effect you are looking for.

Is UICollectionView.backgroundView broken

I'm writing something relatively simple, or so I thought.
Firstly, the code, for which I'm trying to place an image on the background of the UICollectionView if there are no results returned from my server. The image is 200 by 200:
UIView *myView = [[UIView alloc] initWithFrame:self.view.bounds];
CGRect myViewSpace = self.view.bounds;
CGFloat myX = (myViewSpace.size.width /2.0) - 100;
CGFloat myY = (myViewSpace.size.height /2.0) - 100;
UIImageView *imView = [[UIImageView alloc] initWithFrame:CGRectMake(myX, myY, 200, 200)];
imView.image = [UIImage imageNamed:#"imNotHome"];
[myView addSubview:imView];
myCollectionView.backgroundView = myView;
Once there are results, I want to be able to remove it.
I thought it'd be as simple as placing the following, before I reloaded the UICollectionView:
[myCollectionView.backgroundView removeFromSuperview];
However, it appears to be doing nothing.
Am I missing something?
Thanks in advance!
It should be done this way instead:
myCollectionView.backgroundView = nil;
Explanation: You should unset the UICollectionView's background in the same way as you set it. You didn't set it by manipulating the view hierarchy, but by setting the background property. You did call addSubview in the previous line, but that was to add a subview to your background view, not to add the background view itself.
Edit:
There is a very good article about this UICollectionView bug here:
http://blog.spacemanlabs.com/2013/11/uicollectionviews-backgroundview-property-is-horribly-broken/
The solution the author gives is to reset the background to an empty opaque view:
UIView *blankView = [UIView new];
blankView.backgroundColor = [UIColor whiteColor];
[myCollectionView.backgroundView removeFromSuperview];
myCollectionView.backgroundView = blankView;
Also, the author recommends not using the backgroundView property at all but doing it yourself:
Frankly, I think the best solution is to just ignore the backgroundView property all together. Instead, make the collection view’s background clear, and implement your own backgroundView; just throw a view behind the collection view.

What method do I use when wanting to display multiple images and respond to user interaction?

Alright, so I have 5 custom images that I want in my game.
So each variable for example will have a value set with it, image 1 = 1, image 2 = 2, etc. I want the user to be able to press one of these images and when they do, it will change from say image 5 to image 4.
Do I need to put these images in an array/dictionary/etc? I honestly have no idea how to go about it so dont know what to search for on google.
Any help or advice is appreciated.
You should use imageViews and add a gesture recognizer to each imageView, and then give each imageView tags in order to organize them. You can set tags in storyboard by pressing the imageView and then entering the tag under the shield. Each imageView should have a distinct tag. I'm giving a programatic example below for clarity, though.
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(wasTapped:)];
tap.delegate = self;
imageView.tag = 1;
[imageView addGestureRecognizer:tap];
When an imageView is tapped the selector will be called and you can change the imageView like so.
-(void) wasTapped:(UIGestureRecognizer *) recognizer
{
UIImageView * imageView = (UIImageView*) recognizer.view;
if (imageView.tag == 1){
// change image to something else
UIImage * newImage = [UIImage imageNamed:#"newImage"];
imageView.image = newImage;
}
}
You may have to call [imageView setNeedsDisplay] to make the new image appear. You also will have to devise some system to organize the imageViews. You may also have to set imageView.userInteractionEnabled to YES.

Simple zoom in for UIScrollView inside of UIView

Hi I have a UIScrollView inside of a UIView. I have tried to use code snippets that I found online but they simply don't change anything. Also they are mostly for an image or custom view done within UIView, whereas in my case I have an array of programatically created UILabels. I have tried to change boundary values as well, it simply does not do anything. This is basically how I establish the size of it within viewDidAppear:
[scrollView setContentSize:CGSizeMake([screenView getWidth], [screenView getHeight])];
scrollView.showsHorizontalScrollIndicator = true;
scrollView.showsVerticalScrollIndicator = true;
screenView is a UIView variable.
This is the settings that I use(also in viewDidAppear):
doubleTapRecogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTapResponse:)];
[doubleTapRecogniser addTarget:self action:#selector(doubleTapResponse:)];
doubleTapRecogniser.delegate = self;
doubleTapRecogniser.numberOfTapsRequired = 2;
[self.scrollView addGestureRecognizer:doubleTapRecogniser];
This is how I implemented my double tap method:
- (void) doubleTapResponse:(UITapGestureRecognizer *)recogniser
{
CGFloat newZoomScale = self.scrollView.zoomScale / 1.5f;
newZoomScale = MAX(newZoomScale, self.scrollView.minimumZoomScale);
[self.scrollView setZoomScale:newZoomScale animated:YES];
}
When I use NSLog messages within my doubleTapResponse, I can get responses from my console. However it does not do anything. What could be the problem?I am using iOS6.1
The error clearly says that the run time searched for a method named doubleTapResponse in the scrollview class you are using. Even if changing the target to self doesn't work, its the method definition place you have to change either the scrollview or the viewcontroller.
[doubleTapRecogniser addTarget:scrollView action:#selector(doubleTapResponse:)];
should be
[doubleTapRecogniser addTarget:self action:#selector(doubleTapResponse:)];
because the scrollview does not know what that method doubleTapResponse is.
Currently it is throwing an exception because it is trying to call the target of the UISCrollView with your doubleTapResponse method, you must add the target of self, and implement this method yourself. In here goes the logic for zooming I presume.
You must also define: doubleTapResponse in your viewcontroller (or class that you are using)
see this for more info:
Ray Wenderlich guide
In order to zoom please look at the following article: QUESTION

Moving a cell using CGRectOffset - what am I doing wrong?

I stumbled upon this fantastic library for implementing table view cell sliding, but there's a few minor differences between what it offers and what I'm looking to have, so I'm continuing with my implementation.
The way the developer moved the table view cells was creative, I thought, in that he used CGRectOffset() to add almost padding to the sides of the cell as it moved to look like it was moving.
I want to do something similar for mine, as it seems like the easiest way to accomplish this. However, when I do the following, nothing happens to the cell:
(Just an example):
CGFloat offset = 11;
self.contentView.frame = CGRectOffset(self.contentView.bounds, offset, 0);
I set a breakpoint there, so it's indeed being called when I pan across the cell (it's in the gesture recognizer method). Yet after it gets called, the cell looks identical in position.
And I have this at the beginning, similar to how he set up the view:
- (void)awakeFromNib {
[super awakeFromNib];
self.contentView.backgroundColor = [UIColor whiteColor];
UIView *backgroundView = [[UIView alloc] initWithFrame:self.contentView.frame];
backgroundView.backgroundColor = [UIColor whiteColor];
self.backgroundView = backgroundView;
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(pannedCell:)];
panGestureRecognizer.delegate = self;
[self addGestureRecognizer:panGestureRecognizer];
}
So I'm not sure exactly why it's not working. Does anyone have any ideas?

Resources