Search code examples
iphoneiosuiscrollviewuipagecontroltouches

UIScrollView only accepting touches on second page


I have a subclassed UIScrollView that passes touches onto the buttons(UIImageViews). The ScrollView has paging enabled and is spread across between 2 and 7 pages programmitically.

However the touches are only recognised when page 2 is shown. The scrolling works fine.

Any Ideas?

Here is the settings I have when I add the ScrollView:

lsScroll.pagingEnabled = YES;
[lsScroll setCanCancelContentTouches:NO];
lsScroll.frame = CGRectMake(0, 0, 320, 480);
lsScroll.bounds = CGRectMake(0, 0, 320, 480);
lsScroll.contentSize = CGSizeMake(lsScroll.bounds.size.width, lsScroll.bounds.size.height*maxSection);
[lsScroll setScrollEnabled:YES];
lsScroll.delaysContentTouches = YES;
lsScroll.delegate = self;
lsScroll.bounces = NO;
lsScroll.showsHorizontalScrollIndicator = NO;
lsScroll.showsVerticalScrollIndicator = NO;
lsScroll.clipsToBounds = YES;

(where maxSection) is a variable that defines the number of pages

Section pages are then added in the same manner:

section1View = [[UIImageView alloc] initWithFrame:self.view.frame];
section1View.bounds = CGRectMake(0, 0, 320, 480);
section1View.frame = CGRectMake(0, 0, 320, 480);
[section1View setBackgroundColor:[UIColor clearColor]];
[lsScroll addSubview:section1View];

section2View = [[UIView alloc] initWithFrame:self.view.frame];
section2View.bounds = CGRectMake(0, 480, 320, 480);
section2View.frame = CGRectMake(0, 480, 320, 480);
[section2View setBackgroundColor:[UIColor clearColor]];
[lsScroll addSubview:section2View];

section3View = [[UIImageView alloc] initWithFrame:self.view.frame];
section3View.bounds = CGRectMake(0, 960, 320, 480);
section3View.frame = CGRectMake(0, 960, 320, 480);
[section3View setBackgroundColor:[UIColor clearColor]];
[lsScroll addSubview:section3View];

UPDATE: UIImageViews are then added to the section#View's and these are the objects that I am checking for touches on. The UIImageViews that are on section2View register the touch but any UIImageViews on the other section arent registering touches.

Everything displays correctly and in the correct place the scrolling works, the touch events work if you are on page/section 2 work - but all other touches fail.

Any help gratefully appreciated.

UPDATE: Here is the code for adding the 'UIImageViews'. So you can see that I already have everything correct with these. The first is for a 'UIImageViw that appears on page 1 - touch not recieved and the second for a 'UIImageView' on page 2 - touches do work.

ls1= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ls1.png"]];
ls1.center = (CGPoint){ 215, 65 };
ls1.transform = CGAffineTransformMakeScale(0.33,0.33);
[ls1 setUserInteractionEnabled: YES];
[section1View addSubview: ls1];

ls20= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ls20.png"]];
ls20.center = (CGPoint){ 215, 615 };
ls20.transform = CGAffineTransformMakeScale(0.33,0.33);
[ls20 setUserInteractionEnabled: YES];
[section2View addSubview: ls20];

These lines of code were created using a macro in a spreadsheet - so I know that they are all the same and all contain setUserInteractionEnabled.


Solution

  • section2View is created with UIView, the rest with UIImageView. If userInteractionEnabled is NO on these image views, their subviews won't receive touches.