How to handle event of code-created UIView - ios

I have unknown amount of UIImageViews which are created in the code, not in the xib file and i need to handle the taps on those images. The handling of each of this image view is going to be the same. How do i do this?

Demo code for your implemantation
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
singleTap.numberOfTapsRequired = 1;
[imgView1 setUserInteractionEnabled:YES];
[imgView1 addGestureRecognizer:singleTap];
imgView1.tag = 1;
imgView1.backgroundColor = [UIColor redColor];
[self.view addSubview:imgView1];
UIImageView *imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 200, 100, 100)];
UITapGestureRecognizer *singleTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
singleTap.numberOfTapsRequired = 1;
[imgView2 setUserInteractionEnabled:YES];
[imgView2 addGestureRecognizer:singleTap2];
imgView2.tag = 2;
imgView2.backgroundColor = [UIColor blueColor];
[self.view addSubview:imgView2];
}
-(void)tapDetected:(UITapGestureRecognizer *)gestureRecognizer{
UIImageView *myImg = (UIImageView*)gestureRecognizer.view;
NSLog(#"tag : %ld",(long)myImg.tag);
}

Try this code to add a gesture recogniser
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(action:)];
[_imgview addGestureRecognizer:tap];
_imgview.userInteractionEnabled = YES;

You can add UITapGestureRecognizer to detect touch on UIImageView.
Just use below method with argument, your image view and a unique tag, and you're done !
- (void) setTapGestureOnImageView:(UIImageView *)imageView withTag:(NSInteger)tag {
//this is important, by default user interaction isn't enabled, we have to enable it.
imageView.userInteractionEnabled = YES;
//create a tap gesture (in example this is sinlge tap) with target and action to call when user tap
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(detectTap:)];
//you can customize taps too
//tap.numberOfTapsRequired = 2;
//tap.numberOfTouchesRequired = 2;
//add gesture on image view
[imageView addGestureRecognizer:tap];
}
- (void)detectTap:(UIGestureRecognizer *)recognizer {
//Get the tapped image view from recognizer
UIImageView *imageView = (UIImageView *)recognizer.view;
//Check for condition, which image view tapped
if(imageView.tag == 1) {
//do something 1st imageview
}
else if(imageView.tag == 2) {
//do something for 2nd imageview
}
else {
//do something else
}
}

May be this will be useful to you
- (void)viewDidLoad
{
NSArray *imagesArray = [NSArray arrayWithObjects:#"statement_card_1.png", #"statement_card_2.png", #"statement_card_3.png", #"statement_card_4.png", #"statement_card_5.png", nil];
short xPadding = 10;
for (int i = 0; i< imagesArray.count; i++)
{
UIImage *imageRecipe =[UIImage imageNamed:[imagesArray objectAtIndex:i]];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPadding, xPadding+(i*60),imageRecipe.size.width, imageRecipe.size.height)];
imgView.tag =IMAGETAG + i;
[imgView setImage:imageRecipe];
imgView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
[imgView addGestureRecognizer:singleTap];
[self.view addSubview:imgView];
}
}
-(void)tapDetected:(UITapGestureRecognizer *)recognizer
{
UIImageView *TempImg = (UIImageView *)recognizer.view;
NSLog(#"tag : %ld",(long)TempImg.tag);
}

Related

how to get current image tag in scroll view?

//add the scrollview to the view
Friend_Request_Scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-60,70,120,140)];
[Friend_Request_Scroll setAlwaysBounceVertical:NO];
Friend_Request_Scroll.delegate=self;
Friend_Request_Scroll.pagingEnabled=YES;
[Friend_Request_Scroll setShowsHorizontalScrollIndicator:NO];
[Friend_Request_Scroll setShowsVerticalScrollIndicator:NO];
CGFloat xOrigin = 0;
for (int i = 0; i <[imageCollection count]; i++) {
Back_Request_Scroll=[[UIView alloc]init];
Back_Request_Scroll.frame=CGRectMake(xOrigin,Friend_Request_Scroll.frame.origin.y-135,120,140);
Back_Request_Scroll.backgroundColor=[UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:0.5];
Back_Request_Scroll.clipsToBounds=YES;
[Friend_Request_Scroll addSubview:Back_Request_Scroll];
image = [[UIImageView alloc] initWithFrame:
CGRectMake(0, 0,
Back_Request_Scroll.frame.size.width,
Back_Request_Scroll.frame.size.height)];
[image sd_setImageWithURL:[imageCollection objectAtIndex:i] placeholderImage:[UIImage imageNamed:#""]];
image.tag=i;
[Back_Request_Scroll addSubview:image];
xOrigin= xOrigin+120;
image.userInteractionEnabled=YES;
}
Friend_Request_Scroll.contentSize = CGSizeMake(xOrigin,
0);
[self.view addSubview:Friend_Request_Scroll];
swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeleft:)];
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[image addGestureRecognizer:swipeleft];
swiperight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swiperight:)];
swiperight.direction=UISwipeGestureRecognizerDirectionRight;
[image addGestureRecognizer:swiperight];
pageControl = [[UIPageControl alloc] init];
pageControl.frame = CGRectMake(self.view.frame.size.width/2-60,Friend_Request_Scroll.frame.size.height+Friend_Request_Scroll.frame.origin.y,image.frame.size.width,20);
pageControl.numberOfPages = [imageCollection count];
pageControl.currentPage = currentValue;
[pageControl addTarget:self action:#selector(changePage) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pageControl];
Try this Delegate Method.This delegate will tell you that scrolling has finished.
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
Try this code, you use recongizer object view tag.
- (void)viewDidLoad
{
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:scroll];
NSInteger i;
for (i=0; i<20; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*90 + i*15, 300, 90)];
imageView.backgroundColor = [UIColor blackColor];
imageView.userInteractionEnabled = YES;
imageView.tag = i;
NSLog(#“image Tag = %d”, imageView.tag);
[scroll addSubview:imageView];
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp)];
[imageView addGestureRecognizer: recognizer];
}
scroll.contentSize = CGSizeMake(320, 110*i);
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"Swipe received.");
NSLog(#“image Tag = %d”, recognizer.view.tag);
}
Update
No need swipe.
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (sender == scroll) {
int pageNum = (int)(scroll.contentOffset.x / scroll.frame.size.width);
NSLog(#"%d",pageNum);
//self.pagecontroller.currentPage =pageNum;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height)];
scroll.delegate = self;
scroll.pagingEnabled = YES;
[self.view addSubview:scroll];
CGFloat currentXOffset = 0;
NSInteger i;
for (i=0; i<3; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(currentXOffset, 100, self.view.frame.size.width, 200)];
if (i == 0){
imageView.backgroundColor = [UIColor blackColor];
}else if (i == 1){
imageView.backgroundColor = [UIColor orangeColor];
}else if (i == 2){
imageView.backgroundColor = [UIColor grayColor];
}
imageView.userInteractionEnabled = YES;
imageView.tag = i;
currentXOffset = currentXOffset + imageView.frame.size.width;
[scroll addSubview:imageView];
// UISwipeGestureRecognizer *recognizer;
//
// recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
// [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp)];
// [imageView addGestureRecognizer: recognizer];
}
scroll.contentSize = CGSizeMake(currentXOffset, self.view.frame.size.height);
}
Add tap gesture action to the image and you can get the tag using [sender tag]
I have Few ImageView each of them have tag and I have an array of images. when user tap on image then I get image tag and take action on it.
- (void)viewDidLoad
{
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:scroll];
NSInteger i;
for (i=0; i<20; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*90 + i*15, 300, 90)];
imageView.backgroundColor = [UIColor blackColor];
imageView.userInteractionEnabled = YES;
imageView.tag = i;
NSLog(#“image Tag = %d”, imageView.tag);
[scroll addSubview:imageView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(findTheTag:)];
[imageView addGestureRecognizer:tap];
}
scroll.contentSize = CGSizeMake(320, 110*i);
}
- (void)findTheTag:(id)sender
{
NSLog(#“image Tag = %d”, sender.tag);
}

UIGestureRecognizer in objective-C++

I want to know the UIGestureRecognizer working or not in Objective-C++ because i've implemented this one but tap method never calling. So please let me know is it possible or not in Objective-C++.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
imgView.image = [UIImage imageNamed:#"dharm"];
[self.view addSubview:imgView];
imgView.backgroundColor = [UIColor redColor];
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
tapRecognizer.numberOfTapsRequired = 1;
[tapRecognizer setDelegate:self];
[imgView addGestureRecognizer:tapRecognizer];
}
- (void)tap:(id)sender {
NSLog(#"Tap Pressed");
}
Try adding [imgView setUserInteractionEnabled:YES];

How to make dim background when tap on 1 uiimageview in iOS

I have 1 tabelview, add 4 imageviews in cell. When user tap on any imageview, the background will be dim and the imageview just selected will be bring on top of the dim view.
I tried this code :
self.dimView = [[UIView alloc]initWithFrame:self.view.frame];
self.dimView.backgroundColor = [UIColor blackColor];
self.dimView.alpha = 0.7;
self.dimView.hidden = YES;
[self.sellItemtbl addSubview:self.dimView];
//[self.view bringSubviewToFront:dimView];
//add tap gesture to dimview
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[self.dimView addGestureRecognizer:singleFingerTap];
But it only create 1 dim view, and the image just selected cannot bring on top of this view. How can i do that? Please give me some advice. Thanks in advance.
It is not clear by background did you mean only your table view cell or the entire view. I have assumed it to be only table view cell and enclosing you the code of table view cell. You can extend it to entire view by making use of - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method.
- (void)awakeFromNib
{
[self setSelectionStyle:UITableViewCellEditingStyleNone];
[self.contentView setBackgroundColor:[UIColor colorWithRed:1.f green:0.f blue:0.f alpha:1.f]];
[self.imgView1 setUserInteractionEnabled:YES];
[self.imgView2 setUserInteractionEnabled:YES];
[self.imgView3 setUserInteractionEnabled:YES];
[self.imgView4 setUserInteractionEnabled:YES];
[self.imgView1 setTag:1];
[self.imgView2 setTag:2];
[self.imgView3 setTag:3];
[self.imgView4 setTag:4];
[self.imgView1 addGestureRecognizer:[self getTapGestureRecognizer]];
[self.imgView2 addGestureRecognizer:[self getTapGestureRecognizer]];
[self.imgView3 addGestureRecognizer:[self getTapGestureRecognizer]];
[self.imgView4 addGestureRecognizer:[self getTapGestureRecognizer]];
}
- (UITapGestureRecognizer *)getTapGestureRecognizer
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapMade:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
tapGesture.cancelsTouchesInView = NO;
return tapGesture;
}
- (void)tapMade:(UITapGestureRecognizer *)recognizer
{
UIImageView *imgView = (UIImageView *)recognizer.view;
NSInteger tag = imgView.tag;
[self.contentView setBackgroundColor:[UIColor colorWithRed:1.f green:0.f blue:0.f alpha:0.7f]];
[self.imgView1 setAlpha:(tag == 1)? 1.f: 0.7f];
[self.imgView2 setAlpha:(tag == 2)? 1.f: 0.7f];
[self.imgView3 setAlpha:(tag == 3)? 1.f: 0.7f];
[self.imgView4 setAlpha:(tag == 4)? 1.f: 0.7f];
}
Let me know if you have any doubts.

changing the border color of a label by tapping and dragging

I want to be able to change the border color of my label when it is tapped and dragged and when it is not being tapped and dragged, the border color should return to its previous colour. I have employed both pan and tap gesture recognisers but I have no idea in writing the code to change this. This is my code below:
(void)change:(id)sender {
CGRect labelFrame = CGRectMake(230, 240, 300, 30);
UILabel *headingLabel = [[UILabel alloc] initWithFrame:labelFrame];
headingLabel.layer.borderColor = [UIColor clearColor].CGColor;
headingLabel.layer.borderWidth = 1.0;
headingLabel.backgroundColor = [UIColor blackColor];
headingLabel.textColor = [UIColor redColor];
[self.view addSubview:headingLabel];
[headingLabel setUserInteractionEnabled:YES];
headingLabel.text = _textField.text;
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(panDetected:)];
[self.view addGestureRecognizer:panRecognizer];
[headingLabel addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapDetected:)];
[self.view addGestureRecognizer:tapRecognizer];
[headingLabel addGestureRecognizer:tapRecognizer];
}
(void)panDetected:(UIPanGestureRecognizer *)paramSender{
if (paramSender.state != UIGestureRecognizerStateEnded &&
paramSender.state != UIGestureRecognizerStateFailed){
CGPoint location = [paramSender locationInView:paramSender.view.superview];
paramSender.view.center = location;
}
}
(void) tapDetected:(UITapGestureRecognizer *)paramSender {
NSUInteger touchCounter = 0;
for (touchCounter = 0;
touchCounter < paramSender.numberOfTouchesRequired;
touchCounter++){
CGPoint touchPoint =
[paramSender locationOfTouch:touchCounter
inView:paramSender.view];
}
}
#end
You have to use the view delegate methods :
touchesBegan, touchesEnd..
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html

Detect tap on UIImageView inside UIScrollView

I have a horizontal scrollview filled with UIImageViews.
I want to detect a tap on the UIImageView and have its background color changed.
Somehow the tap gesture is not working or so.
However, when I add a tap gesture to the scrollview, it works. The scrollview.background color can be changed.
But I want to detect a tap on the UIImageViews it contains!
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, 768, 127)];
[scrollView setScrollEnabled:YES];
scrollView.backgroundColor = [UIColor orangeColor];
[scrollView setShowsHorizontalScrollIndicator:NO];
UIImageView *contentOfScrollView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 1, 1130, 125)];
scrollView.contentSize = CGSizeMake(contentOfScrollView.frame.size.width, contentOfScrollView.frame.size.height);
for (int aantal=0; aantal < 6; aantal++) {
UIImageView *item = [[UIImageView alloc] initWithFrame:CGRectMake(3+(aantal*188), 0, 185, 125)];
item.backgroundColor = [UIColor yellowColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:item action:#selector(imageTapped:)];
tap.numberOfTapsRequired = 1;
tap.cancelsTouchesInView=YES;
item.userInteractionEnabled = YES;
[item addGestureRecognizer:tap];
[contentOfScrollView addSubview:item];
}
//UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
//[scrollView addGestureRecognizer:tap];
scrollView.userInteractionEnabled=YES;
scrollView.delaysContentTouches=NO;
[scrollView addSubview:contentOfScrollView];
[self.view addSubview:scrollView];
And this is the imageTapped function.
-(void)imageTapped:(UITapGestureRecognizer *)gesture
{
NSLog(#"tapped!");
gesture.view.backgroundColor = [UIColor whiteColor];
}
User interaction is set to NO by default for UIImageView, so you need to set it to YES.
You set it to yes for "item", but not for contentOfScrollView.
Your error is here:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:item action:#selector(imageTapped:)];
You need to change the target to "self" instead of "item", then it won't crash.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];

Resources