Touch events on views inside UI Scroll View - ios

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];
}

Related

UIPinchGestureRecognizer questions with subviews

I have the following code:
m_singleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, nWidth - 1, nHeight - 1)];
m_singleView.backgroundColor = [UIColor clearColor];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinch:)];
[pinchGesture setCancelsTouchesInView:YES];
[m_singleView addGestureRecognizer:pinchGesture];
[m_singleView setUserInteractionEnabled:YES];
[m_MainView addSubview:m_singleView];
The issue that I'm having is that the pinch event for some reason does not fire. However, if i change line from [m_singleView addGestureRecognizer:pinchGesture] to [m_MainView addGestureRecognizer:pinchGesture]; then everything will work fine... can I not add the event for subview only?
Thanks!
Yes,you can add gesture to subview. i tested your code like below works fine.
First add delegate.
#interface ViewController : UIViewController<UIGestureRecognizerDelegate>
- (void)viewDidLoad {
[super viewDidLoad];
UIView *m_singleView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, self.view.frame.size.width - 50, self.view.frame.size.height - 50)];
self.view.backgroundColor=[UIColor greenColor];
m_singleView.backgroundColor = [UIColor redColor];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinch)];
pinchGesture.delegate=self;
[pinchGesture setCancelsTouchesInView:YES];
[m_singleView addGestureRecognizer:pinchGesture];
[m_singleView setUserInteractionEnabled:YES];
[self.view addSubview:m_singleView];
}
-(void)pinch{
NSLog(#"In PInch");
}
you have used pinchgesture so you can use it like below.
Use Debug view hierarchy to see whether the size of m_singleView are 0 or not? If so, the size of m_singleView needs to be changed until you can touch it.

Segment control background color not getting changed

I am working on a project where I am using custom buttons for login and signup.
All I want is when I click on login, the color of signup button changes and when I click on signup button again the color of login button changs back.
I tried the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
// [self.view setNuiClass:#"ViewInit"];
// Do any additional setup after loading the view from its nib.
UIImageView *img = [[UIImageView alloc ] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
img.image = [UIImage imageNamed:#"iphone-4-apple-logo-wallpapers-set-2-05.jpg"];
[self.view addSubview:img];
NSLog(#"Height Pro %f",[[DeviceClass instance] getResizeScreen:NO].size.height);
scrollView = [[UIScrollView alloc] initWithFrame:[[DeviceClass instance] getResizeScreen:NO]];
scrollView.alwaysBounceVertical = YES;
scrollView.delegate = self;
[self.view addSubview:scrollView];
segmentControl = [[UISegmentedControl alloc] initWithItems:#[NSLocalizedString(#"profile_tab_signin", nil),NSLocalizedString(#"profile_tab_signup", nil)]];
segmentControl.frame = CGRectMake(10, 10, 300, 40);
segmentControl.selectedSegmentIndex = 0;
[segmentControl addTarget:self action:#selector(valueChanged) forControlEvents: UIControlEventValueChanged];
[scrollView addSubview:segmentControl];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleTap];
//Init
[self autoLogin];
[self.view setNeedsDisplay];
//[signUpBtn setBackgroundColor:[UIColor redColor]];
//[loginBtn setBackgroundColor:[UIColor yellowColor]];
}
- (void)valueChanged
{
//For SignIn/SignUp
if(segmentControl.selectedSegmentIndex == 0)
{
[usernameSignUp removeFromSuperview];
[emailSignUp removeFromSuperview];
[firstNameSignUp removeFromSuperview];
[lastNameSignUp removeFromSuperview];
[passwordSignUp removeFromSuperview];
[retypepasswordSignUp removeFromSuperview];
[signUpBtn removeFromSuperview];
//[signUpBtn setBackgroundColor:[UIColor redColor]];
[self signInForm];
}
else
{
[lostPassword removeFromSuperview];
[username removeFromSuperview];
[password removeFromSuperview];
[loginBtn removeFromSuperview];
[self signUpForm];
}
}
Please suggest me some useful code as I am new in iOS.

UITapGestureRecognizer on multiple UIView (scrollview subviews)

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

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

Resources