I don't get why this #selector isn't working - ios

I am creating several UIView's which I add a UITapGestureRecognizer with an action thats suppose to call a method in the viewController, but it doesn't seem to work.
-(void)setupFreeMoneyView {
NSArray *array = #[#"Facebook", #"Twitter", #"SMS", #"Email"];
int index = 0;
for (NSString *str in array) {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100*index++, 0, 100, 100)];
[imgView setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#Logo", str]]];
[imgView addGestureRecognizer:[[UITapGestureRecognizer alloc]
initWithTarget:self
action:NSSelectorFromString([NSString stringWithFormat:#"postTo%#", str])]];
[freeView addSubview:imgView];
}
}
freeView is a UIView created in the MainStoryboard.
Ive checked if its the NSSelectorFromString() by using a normal #selector() thats not working but it isn't the cause.
It does work if I change the line [imgView addGestureRecognizer:... to [freeView addGestureRecognizer:...]
I just don't seem to understand why this is happening.

try this
imgView.userInteractionEnabled = YES ;
userInteractionEnabled of UIImageView is set to NO by default.

maybe you need set userInteractionEnabled = YES for the UIImageView

Related

Bug about UIImageView display gif?

I create a UIImageView and add to VC UIView , then I create a image with animatedImageWithImages:duration:
When I execute the code, it works normally. It displays image animation. But when I push to next VC and in the process of swipe back , I find the UIImageView display nothing.
At the moment finger left screen , everything back to normal.
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imgView = [[UIImageView alloc] init];
imgView.backgroundColor = [UIColor orangeColor];
imgView.frame = CGRectMake(20, 200, 80, 80);
[self.view addSubview:imgView];
NSArray *imgs = #[[UIImage imageNamed:#"img1"],[UIImage imageNamed:#"img2"]];
imgView.image = [UIImage animatedImageWithImages:imgs duration:0.5];
}
BugGif
I notice the imageView has CAKeyframeAnimation. I guess the conflict that runmode and CAKeyframeAnimation.
SDWebImage has the same problem. Because it also use the method that create UIImage. I think the point is the method of animatedImageWithImages:duration: and CAKeyframeAnimation.
Anyone knows the solution? Thanks a lot.
The event that the finger left the screen will make imageView display normally. Why and How to resolve?
GitHubBugDemo
You can use UIImageView another methods to set images and animation duration as below
UIImageView *imgView = [[UIImageView alloc] init];
imgView.backgroundColor = [UIColor orangeColor];
imgView.frame = CGRectMake(20, 200, 80, 80);
NSArray *imgs = #[[UIImage imageNamed:#"img1"],[UIImage imageNamed:#"img2"]];
[imgView setAnimationImages:imgs];
[imgView setAnimationDuration:1];
[imgView setAnimationRepeatCount:HUGE_VAL];
[imgView startAnimating];
[self.view addSubview:imgView];

Get click of imageview(added dynamically) within a row of uitableview in ios

I have a tableview whose rows are dynamic and each row have n numbers of imageviews as it is in the screenshot attached
Now what I want is to know which imageview I have clicked.
NOTE : imageview is added dynamically to one view and that view is added to scrollview so that it can scroll horizontally.
And there are n numbers of row in a tableview.
EDIT:
I tried adding simple button as well above the image view just to try out if that clicks but its click also didn't work.
I tried the solution of Gesture as well but that also didn't work
CODE:
-(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *strrowId ;
UITableViewCell *cell = [_tblviewScroll1 dequeueReusableCellWithIdentifier:#"cellOne"];
// create and add labels to the contentView
[cell.contentView setBackgroundColor:[UIColor colorWithRed:231.0/255.0 green:231.0/255.0 blue:231.0/255.0 alpha:1.0]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// cell.contentView.userInteractionEnabled=NO;
scrollView1 = (UIScrollView*)[cell.contentView viewWithTag:3];
[scrollView1 setShowsVerticalScrollIndicator:NO];
scrollView1.delegate = self;
NSArray* subviews1 = [scrollView1 subviews];
for (UIView* subview in subviews1) {
[subview removeFromSuperview];
}
if (scrollView1.contentOffset.y > 0 || scrollView1.contentOffset.y < 0 )
scrollView1.contentOffset = CGPointMake(scrollView1.contentOffset.x, 0);
[scrollView1 setBackgroundColor:[UIColor colorWithRed:231.0/255.0 green:231.0/255.0 blue:231.0/255.0 alpha:1.0]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleBlack;
scrollView1.clipsToBounds = YES
scrollView1.scrollEnabled = YES;
for (int i = 0; i <mutSubTitle.count; i++)
{
NSString *ids = #"";
UIView *viewvertically1;
viewvertically1=[[UIView alloc]init];
viewvertically1.tag = (i+1);
[viewvertically1 setUserInteractionEnabled:YES];
[viewvertically1 setBackgroundColor:[UIColor whiteColor]];
//Displaying image
NSString *strImgUrl = #"https://img.xxxx.com/";
NSString *strimge=[mutSubImag objectAtIndex:i];
strImgUrl = [strImgUrl stringByAppendingString:strimge];
NSURL *url = strImgUrl;
UIImage *image = [UIImage imageNamed:url];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame= CGRectMake(0, 10, 170, 110);
imageView.tag = i; // tag our images for later use when we place them in serial fashion
[imageView setUserInteractionEnabled:YES];
// images with Lazy Loading
[imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:#"Placeholder.png"]];
UITapGestureRecognizer *imgTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(gestureTapEvent:)];
imgTapGesture.numberOfTouchesRequired = 1;
[imageView addGestureRecognizer:imgTapGesture];
[viewvertically1 addSubview:imageView];
[viewvertically1 addSubview:lblDesc];
[scrollView1 addSubview:viewvertically1];
//[cell bringSubviewToFront:scrollView1];
}
return cell;
}
In your cellForRowAtIndexPath method add the UITapGestureRecognizer for your Concept
// by default the imageview userInteraction is disable you need to manually enable
cell.yourimageView.userInteractionEnabled = YES;
// the following line used for assign the different tags for eachImage
cell.yourimageView.tag = indexPath.row;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(ImageTapped:)];
tap.numberOfTapsRequired = 1;
[cell.yourimageView addGestureRecognizer:tap];
finally need to check which imageView was clicked, check the flag in selector method
-(void)ImageTapped :(UITapGestureRecognizer *) gesture
{
// you can get tag in which image is selected
NSLog(#"Tag = %d", gesture.view.tag);
}
Try giving a button instead of image view added to a view. Assign the image to button's backgroundImageView. You can then give actions for the buttons
Use UIButton as #Arun says or the imageview ur using currently add button on Image with clear background color and add tag to that button and use that click action.

UITextField and tap gesture in leftView

I've an icon on leftView of textField and I want to add tap gesture to it. This is my code :
if (!textField.leftView){
UIImageView *infoImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 18)];
infoImage.image = [UIImage imageNamed:#"info"];
infoImage.contentMode = UIViewContentModeCenter;
textField.leftView = infoImage;
textField.leftViewMode = UITextFieldViewModeAlways;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showErrorMessage:)];
tapRecognizer.numberOfTapsRequired = 1;
[infoImage addGestureRecognizer:tapRecognizer];
}
The problem when user click on the image on leftview nothing is happend and the showErrorMessage is not called .
Update
The tap issue was solved by enabling userInteraction on the infoImage.
But now I'm getting [UITapGestureRecognizer tag]: unrecognized selector sent to instance at second line of my Show errorMessage :
-(void)showErrorMessage:(id)sender{
UIImageView *icon = (UIImageView *)sender;
NSString *key = [#(icon.tag) stringValue];
NSLog(#"%#", [_errorDictionary objectForKey:key]);
}
Missing infoImage.userInteractionEnabled = YES;
you have to set the image's property userInteractionEnabled to TRUE

Adding long press gesture to UIImageView inside Scrollview

I've been researching this for a couple of hours now and nothing seems to work. I have a UIScrollView as an IBOutlet and am adding two UIImageViews via code. One is a background image and the 2nd is a photo. Everything works fine here. I am trying to add a long press gesture to the second photo but it doesn't seem to be working. Here is my code. I hope someone can see my problem.
// loop thru images array
UIImage *bgFrame = [UIImage imageNamed:#"photo-frame.png"];
UIImageView *bgImageView = [[UIImageView alloc] initWithImage:bgFrame];
bgImageView.frame = CGRectMake(posX, posY, 100, 100);
[self.scroll addSubview:bgImageView];
NSString *photoName = [self.photoArray objectAtIndex:i];
UIImage *photo = [self.utils getClientPhoto:photoName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:photo];
imageView.userInteractionEnabled = YES;
imageView.frame = CGRectMake(6, 9, 89, 71);
imageView.tag = i;
[bgImageView addSubview:imageView];
// add long press for deletion
UILongPressGestureRecognizer *lPressed = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(imageLPressed:)];
lPressed.delegate = self;
lPressed.minimumPressDuration = 0.4;
[imageView addGestureRecognizer:lPressed];
imageView.userInteractionEnabled = YES; not needed, it always work fine. Setting delegate to gestureRecognizer is optional too. First of all try to add imageView to self.scroll subviews not to bgImageView. After that check your scrollView content size, if your image is out of dat size it can be visible but not available. Try to replace imageview with uibutton and check for it's uitouchupinside event.
Enable UserInteraction on bgImageView andimageView.

TapGesture recognizer on multiple UIImageView not working

TapGesture recognizer on multiple UIImageView is not working, while it detects last added imageviews gesture.. I have done this,
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(myFunction:)];
tapped.numberOfTapsRequired = 1;
tapped.delegate = self;
UIImageView *sample_book1= [[UIImageView alloc]initWithFrame:CGRectMake(70, 135, 100,125) ];
sample_book1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"mathematics.png"]];
sample_book1.userInteractionEnabled = YES;
sample_book1.tag = 0;
[sample_book1 addGestureRecognizer:tapped];
[self.view addSubview:sample_book1];
UIImageView *sample_book2= [[UIImageView alloc]initWithFrame:CGRectMake(220, 135, 100,125) ];
sample_book2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"biology.png"]];
sample_book2.userInteractionEnabled = YES;
sample_book2.tag = 1;
[sample_book2 addGestureRecognizer:tapped];
[self.view addSubview:sample_book2];
UIImageView *sample_book3= [[UIImageView alloc]initWithFrame:CGRectMake(370, 135, 100,125) ];
sample_book3.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"chemistry.png"]];
sample_book3.userInteractionEnabled = YES;
sample_book3.tag = 2;
[sample_book3 addGestureRecognizer:tapped];
[self.view addSubview:sample_book3];
The tap gesture is not working in sample_book1,sample_book2.... it's only working in sample_book3.. What i'm doing wrong..
As borrrden said, when trying to track gesture, each view must have its own gestureRecognizer.
For each of your sample_books, you should use
[sample_bookX addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(myFunction:)]];
instead of trying to add multiple times the same GR
The argument received by myFunction: would then be the proper tapGR and you could get to the tapped imageView by calling sender.view (providing your myFunction signature look like
- (void) myFunction:(UIGestureRecognizer *)sender
Cheers,
What you are doing wrong is trying to use the gesture in a way that it is not supposed to be used. A gesture can only be attached to one view. You need to make a new one for each view.

Resources