UITapGestureRecognizer on multiple UIView (scrollview subviews) - ios

I have set a scrollview and a bench of UIView (scrollview subviews) with gestures on them:
for (id element in array) {
CustomView *view = [[CustomView alloc] init];
[view setFrame:CGRectMake(x, 16, self.view.frame.size.width, self.view.frame.size.height)];
[self.scrollView setContentSize:CGSizeMake(scrollContentSizeWidth, self.scrollView.frame.size.height)];
UITapGestureRecognizer *tap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(selectView:)];
[self.view setTag:[[element valueForKey:#"Id"] integerValue]];
[self.view addGestureRecognizer:tap];
view.userInteractionEnabled = YES;
[self.scrollView addSubview:view];
scrollContentSizeWidth +=110;
x += 110;
}
The called method when a view is touched:
-(void)selectView:(UITapGestureRecognizer *)recognizer{
NSLog(#"id : %i",recognizer.view.tag);//always last assigned value in the loop above
}
So how to fix that? UITapGestureRecognizer seems to be affected to the last view only.

replace this line
[self.view addGestureRecognizer:tap];
with this
[view addGestureRecognizer:tap];

Related

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.

Touch events on views inside UI Scroll View

I have Implemented a UIScrollView on MainView as follows:
UIScrollView* scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(-160, -540, 480, 1300)];
[scrollView addSubview:self.MainView];
scrollView.contentSize = CGSizeMake(400, 2000);
[self.view addSubview:scrollView];
I have the following Views under MainView in Storyboard:
Main View ----> View1, View2
I want to implement touch events with View 1 and View 2 separately. For that I have the following code:
UITapGestureRecognizer *tapSlidersView1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapSlidersActionView1:)];
[self.View1 addGestureRecognizer:tapSlidersView1];
UITapGestureRecognizer *tapSlidersView2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapSlidersActionView2:)];
[self.View2 addGestureRecognizer:tapSlidersView2];
And the corresponding event handlers:
- (void)tapSlidersActionView1:(UITapGestureRecognizer *)sender
{
[self.navigationController pushViewController:DropDownView1 animated:YES];
}
- (void)tapSlidersActionView2:(UITapGestureRecognizer *)sender
{
[self.navigationController pushViewController:DropDownView2 animated:YES];
}
The tap events does not seem to work at all. please suggest. Thanks in advance
First thing, you have to make sure that your view is touchable by enabling userInteractionEnabled.
Second, you need to declare numberOfTapsRequired for your tap event.
Then you have your code like this:
UITapGestureRecognizer *tapSlidersView1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapSlidersActionView1:)];
tapSlidersView1.numberOfTapsRequired = 1;
self.View1.userInteractionEnabled = YES;
[self.View1 addGestureRecognizer:tapSlidersView1];
Try following code and also every view is created programmatically..
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *mainView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[mainView setBackgroundColor:[UIColor blackColor ]];
[self.view addSubview:mainView];
UIScrollView* scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,mainView .bounds.size.width, mainView.bounds.size.height)];
scrollView.contentSize = CGSizeMake(400, 2000);
[mainView addSubview:scrollView];
UIView *View1=[[UIView alloc]initWithFrame:CGRectMake(50, 150, 100, 100)];
[View1 setBackgroundColor:[UIColor grayColor]];
[scrollView addSubview:View1];
UIView *View2=[[UIView alloc]initWithFrame:CGRectMake(200, 150, 100, 100)];
[View2 setBackgroundColor:[UIColor greenColor]];
[scrollView addSubview:View2];
UITapGestureRecognizer *tapSlidersView1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapSlidersActionView1:)];
[View1 addGestureRecognizer:tapSlidersView1];
UITapGestureRecognizer *tapSlidersView2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapSlidersActionView2:)];
[View2 addGestureRecognizer:tapSlidersView2];
// Do any additional setup after loading the view from its nib.
}
- (void)tapSlidersActionView1:(UITapGestureRecognizer *)sender
{
//your class
// secViewController *DropDownView1=[[secViewController alloc]init];
[self.navigationController pushViewController:DropDownView1 animated:YES];
}
- (void)tapSlidersActionView2:(UITapGestureRecognizer *)sender
{
//your class
// secViewController *DropDownView2=[[secViewController alloc]init];
[self.navigationController pushViewController:DropDownView2 animated:YES];
}

UIscrollview and uibuttons tap

I am using a UIScrollView where I got some UIButtons, to recognize that I pressed the button above, I created a subclass of UIScrollView where rewrite the method
- (BOOL) touchesShouldCancelInContentView (UIView *) view
but only enters that method when the touch is a journey, never comes when I press on the screen without more.
the UIScrollView I've created thus:
`
CGSize containerSize = CGSizeMake(320.0f, 480.0f);
CGSize containerSize2 = CGSizeMake(640.0f, 960.0f);
self.scrollView = [[UIScrollViewWithButtons alloc] initWithFrame:(CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=containerSize}];
[self.scrollView setMinimumZoomScale:0.5f];
[self.scrollView setMaximumZoomScale:1.0f];
[self.scrollView setZoomScale:0.5f];
[self.scrollView setDelegate:self];
self.scrollView.bounces = NO;
self.scrollView.bouncesZoom = NO;
self.scrollView.delaysContentTouches = NO;
self.scrollView.userInteractionEnabled = YES;
[self.scrollView setContentInset:UIEdgeInsetsZero];
[self.view addSubview:self.scrollView];
self.containerView = [[UIView alloc] initWithFrame:(CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=containerSize2}];
self.containerView.userInteractionEnabled = YES;
[self.scrollView addSubview:self.containerView];
[self.scrollView setClipsToBounds:NO];
//instanciar uimageview
fondo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 640, 960)];
[self.containerView addSubview:fondo];
[fondo setImage:[UIImage imageNamed:#"Grande.png"]];
self.scrollView.contentSize = containerSize2;
and button add
imagen = [UIButton buttonWithType:UIButtonTypeCustom];
[imagen addTarget:self action:#selector(pulsadoIzq) forControlEvents:UIControlEventTouchUpInside];
[imagen setTitle:#"" forState:UIControlStateNormal];
[imagen setFrame:CGRectMake(xUser, yUser, 50.0f, 50.0f)];
[imagen setImage:[UIImage imageNamed:#"boton.png"] forState:UIControlStateNormal];
[self.containerView addSubview:fichaUserIzq];
imagen.center = CGPointMake(xUser, yUser);`enter code here`
I need to capture the tap on the screen to the buttons
How I can do?
I would also like to know because if I have implemented this code:
img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"img.png"]];
UIPanGestureRecognizer *panRecg = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(imgDragged:)];
img.userInteractionEnabled = YES;
[img addGestureRecognizer:panRecg];
img.center = CGPointMake(160, 436);
[self.scrollView addSubview:img];
When I click on the image nothing happens, does not enter the method, only comes when I press and drag. I also want to capture when pressed over this image and so I capture only when the move.
I solved the problem, I implement this method:
-(void)singleTapGestureCaptured:(UITapGestureRecognizer*)gesture{
CGPoint touchPoint = [gesture locationInView:self.scrollView];
if ((touchPoint.x > 88)&&(touchPoint.x < 226)&&(touchPoint.y > 172)&&(touchPoint.y < 319)) {
if (rodando) {
[self pararAnimacionDado];
}
}
}
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];

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:)];

Declare uibutton in a subview

I'm stuck at how to declare the uibutton in uiview:
- (void)viewWillAppear:(BOOL)animated {
NSLog(#"Added holderView.");
UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, _imagePicker.selectedImage.size.width, _imagePicker.selectedImage.size.height)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageView setImage:_imagePicker.selectedImage];
[holderView addSubview:imageView];
UIButton *removeSticker = [UIButton buttonWithType:UIButtonTypeCustom];
removeSticker.frame = CGRectMake(0, 0, 100, 100);
[removeSticker setImage:[UIImage imageNamed:#"cancel-disabled.png"] forState:UIControlStateNormal];
[removeSticker addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[holderView addSubview: removeSticker];
[removeSticker setHidden: YES];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[longPressRecognizer setDelegate:self];
[holderView addGestureRecognizer:longPressRecognizer];
[stickerView addSubview:holderView];
}
- (void)buttonClicked:(id)sender {
UIView *holderView = [(UIButton *)sender superview]; [holderView removeFromSuperview];
}
Here I want to make the uibutton *removeSticker visible to be able to do buttonClicked, however it said undeclared identifier *holderView:
-(void)longPress:(id)sender {
for(UIButton *removeSticker in holderView.subviews)[removeSticker setHidden: NO];
}
How should I write this line? Please help, thanks a lot.
Just this
-(void)longPress:(id)sender {
[removeSticker setHidden:NO];
}
The reason you get that error is that the scope for holderView is limited to the block it is declared in. For future reference, if you want it to have scope across your entire class, it should be an ivar or property.
You also want to make removeSticker an ivar for the same reason.
To make it an ivar, put the declaration in the header file:
UIButton *removeSticker;
Then in your viewWillAppear code you change this line
UIButton *removeSticker = [UIButton buttonWithType:UIButtonTypeCustom];
to this
removeSticker = [UIButton buttonWithType:UIButtonTypeCustom];
But barley has it right in his answer, too. sender is already pointing to the removeSticker object, whether you declare it as an ivar or not.
Try this.
-(void)longPress:(id)sender {
[(UIButton *)sender setHidden:NO];
}
In your original code, you are referencing a variable in different scope (i.e., holderView). Thats why the error.

Resources