"Stealing" the UIPanGestureRecognizer from UIScrollView - ios

I've got a UIScrollView (A) parent on the screen, inside it's content I have two controls -
another UIScrollView (B) at the Top an an UIView (C) at the bottom,
A is full screen (460px)
B 460px but content is longer then the screen (600px) so it has it's scrolling inside
C 460px fixed
also paging is enabled so B is the 1st page and C is the 2nd,
When I pan down B is scrolling and when it reaches the bottom it's bounces instead of pulling view C, if I set the bounce to NO then it's stuck at the bottom and only if I raise the finger and pan again it pulls the view C..
I saw some related questions but non of them helped me (How to steal touches from UIScrollView?)
a code sample to recreate the situation
(or download from my dropbox https://www.dropbox.com/s/f9j0vkg902214ab/Test2.zip)
- (void)viewDidLoad{
[super viewDidLoad];
// create main scroll
UIScrollView *scrollA = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollA.pagingEnabled = YES;
scrollA.bounces = YES;
[self.view addSubview:scrollA];
// create top scroll B
UIScrollView *scrollB = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollB.backgroundColor = [UIColor greenColor];
scrollB.bounces = YES;
[scrollA addSubview:scrollB];
// create something to put in B
CGRect frameViewB = scrollB.frame;
frameViewB.origin.x = 30;
frameViewB.size.width = 260;
frameViewB.size.height = 600;
UIView *viewInsideB = [[UIView alloc] initWithFrame:frameViewB];
viewInsideB.backgroundColor = [UIColor blueColor];
[scrollB addSubview:viewInsideB];
[scrollB setContentSize:viewInsideB.frame.size];
// create bottom view
CGRect frameC = self.view.frame;
frameC.origin.y = 460;
UIView *viewC = [[UIView alloc] initWithFrame:frameC];
viewC.backgroundColor = [UIColor yellowColor];
[scrollA addSubview:viewC];
// set content for 2 pages
[scrollA setContentSize:CGSizeMake(320, 920)];}
Thanks

Related

iOS Reproduce a view n times

I have a static UITableViewController who has 3 what I call Widgets in them. These widgets display data about a game (score, players etc).
In one of my widget I want to display all the players and there icon. I don't know how many players need to be set because this depends on the result the server gives me.
So what I want is to add N times a UIView for all the players the servers returns. I had an idea to do this in a UITableView, but I read somewhere that I cannot add a UITableView inside a TableViewCell.
I would like to be able to make a "Dummy" UIView inside IB and copy and add this for the amount of players or isn't this possible?
I would recomend you to do it in code, it's easy you can create subclass on UIView and you can add UIScrollView. You can override initWithFrame to accept array with yours views:
-(id)initWithFrame:(CGRect)frame data:(NSArray*)myViews
{
if (self = [super initWithFrame:frame])
{
_viewsArray = myViews;
self.scrollView = [[UIScrollView alloc] init];
self.scrollView.backgroundColor = [UIColor clearColor];
[self.scrollView setShowsHorizontalScrollIndicator:NO];
[self addSubview:self.scrollView];
for (int i = 0; i < _viewsArray.count; i++)
{
// This view (v) needs to initialised base on your array content
id obj = _viewsArray[i];
UIView *v = [[UIView alloc] init];
[self.scrollView addSubview:v];
}
}
return self;
}
The last bit left to do is set up a frame for views and scrollView and contentSize in layoutSubviews:
-(void)layoutSubviews
{
[self.scrollView setFrame:self.bounds];
// change your views frames if you need
[self.scrollView setContentSize:CGSizeMake(self.frame.size.width, _viewsArray.count * VIEWHEIGHT)];
}

iOS Nested UIScrollViews Not Responding

I've been searching around on how to nest UIScrollViews.
It seems like it should be as easy as adding the inner scroll views by using addSubview: of the container scroll view. I have everything showing up visually correct but the functionality of the inner scroll views is non existent despite supplying them with proper frames and content sizes.
My code below shows what I have so far. Only the outer scroll view scrolls. I want it so that the outer scroll view controls left to right scrolling and each inner scroll view controls vertical scrolling of their related page content.
self.containerScroll = [[UIScrollView alloc]init];
self.containerScroll.frame = CGRectMake(0,(self.headerView.frame.size.height + self.pageControl.frame.size.height),screenWidth, (screenHeight - (self.headerView.frame.size.height + self.pageControl.frame.size.height)));
self.containerScroll.backgroundColor = [UIColor clearColor];
self.containerScroll.alpha = 1;
self.containerScroll.pagingEnabled = YES;
self.containerScroll.contentSize = CGSizeMake(self.containerScroll.bounds.size.width*3,1);
self.containerScroll.bounces = NO;
self.containerScroll.delegate = self;
[self.view addSubview:self.containerScroll];
self.page1Scroll = [[UIScrollView alloc]init];
self.page1Scroll.frame = CGRectMake(0,0,self.containerScroll.bounds.size.width,self.containerScroll.bounds.size.height);
self.page1Scroll.backgroundColor = [UIColor redColor];
self.page1Scroll.alpha = 1;
[self.page1Scroll addSubview:self.feedPageVC.view];
self.page1Scroll.contentSize = CGSizeMake(320,500);
self.page1Scroll.delegate = self;
[self.containerScroll addSubview:self.page1Scroll];
self.page2Scroll = [[UIScrollView alloc]init];
self.page2Scroll.frame = CGRectMake(self.containerScroll.bounds.size.width,0,self.containerScroll.bounds.size.width,self.containerScroll.bounds.size.height);
self.page2Scroll.backgroundColor = [UIColor greenColor];
self.page2Scroll.delegate = self;
[self.containerScroll addSubview:self.page2Scroll];
self.page3Scroll = [[UIScrollView alloc]init];
self.page3Scroll.frame = CGRectMake(self.containerScroll.bounds.size.width*2,0,self.containerScroll.bounds.size.width,self.containerScroll.bounds.size.height);
self.page3Scroll.backgroundColor = [UIColor blueColor];
[self.page3Scroll addSubview:self.detailsPageVC.view];
self.page3Scroll.contentSize = CGSizeMake(320,500);
self.page3Scroll.delegate = self;
[self.containerScroll addSubview:self.page3Scroll];
self.containerScroll.contentSize = CGSizeMake(self.containerScroll.bounds.size.width*3,1);
Looks like the height of your content is set to 1. If a child is larger than its parent, it won't function correctly (works the same for buttons).
Also, make sure you know which scrollview you're handling in the delegate methods. All the scrollviews will be handled in the same method and it could cause you problems.

Adding the scrollview created by photoscroller to a subview

I'm trying to modify Apple's PhotoScroller example to make the scrollview that is created into a subview instead of it being a view that takes up the entire screen. Any ideas on how this can be accomplished?
- (void)loadView
{
// Step 1: make the outer paging scroll view
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor blackColor];
pagingScrollView.showsVerticalScrollIndicator = NO;
pagingScrollView.showsHorizontalScrollIndicator = NO;
pagingScrollView.contentSize = [self contentSizeForPagingScrollView];
pagingScrollView.delegate = self;
// When I do this it fails
[self.view addSubview:pagingScrollView];
// Step 2: prepare to tile content
recycledPages = [[NSMutableSet alloc] init];
visiblePages = [[NSMutableSet alloc] init];
[self tilePages];
}
You just need to modify the frame of the scrollview to be positioned and sized how you want:
This is the line in the view controller that sets it up in the example
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
As an example here is a sample frame with some hardcoded values:
CGRect scrollFrame = CGRectMake(100,100,100,100);
pagingScrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
So, I found out that I was able to add the scrollView as a subview by changing the method from loadView to viewDidLoad.
I have no clue why that works, but it does. I'd love to know why that's the case however...

Multiple UIScrollViewControllers on the same window, some not showing their views

I am trying to display 3 UIScrollViewControllers in the same window as 3 horizontal stripes 3 times the screen wide scrolling indipendently. The code to achieve this is below but for some reason it doesn't work, showing only the upper stripe.
With the help of the additional white background view I placed on the window I can see the other 2 regions of the screen are scrollable as well, but for some reason their background color is not showing up...I can't figure out why.
Here is the code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Creating the window programmatically
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CGRect bounds = [self window].bounds;
float x = bounds.origin.x;
float y = bounds.origin.y;
float w = bounds.size.width;
float h = bounds.size.height;
CGRect upperFrame = CGRectMake(x,y,w,h/3);
CGRect middleFrame = CGRectMake(x,y + h/3,w,h/3);
CGRect lowerFrame = CGRectMake(x, y + 2 * h/3, w, h/3);
// Creating the scroll view which will contain the 3 views
UIScrollView *upperScrollView = [[UIScrollView alloc] initWithFrame:upperFrame];
UIScrollView *middleScrollView = [[UIScrollView alloc] initWithFrame:middleFrame];
UIScrollView *lowerScrollView = [[UIScrollView alloc] initWithFrame:lowerFrame];
// ContentSize should be wide enough for 3 pages
[upperScrollView setContentSize:CGSizeMake(3 * w, h/3)];
[middleScrollView setContentSize:CGSizeMake(3 * w, h/3)];
[lowerScrollView setContentSize:CGSizeMake(3 * w, h/3)];
// Enforce the display of only one page at a time
[upperScrollView setPagingEnabled:YES];
[middleScrollView setPagingEnabled:YES];
[lowerScrollView setPagingEnabled:YES];
// UPPER SCROLLVIEW
// This will be the first view (red)
UIView *redViewU = [[UIView alloc] initWithFrame:upperFrame];
[redViewU setBackgroundColor:[UIColor redColor]];
// The second view will start horizontally when the first view ends
upperFrame.origin.x += w;
// This will be the second view (green)
UIView *greenViewU = [[UIView alloc] initWithFrame:upperFrame];
[greenViewU setBackgroundColor:[UIColor greenColor]];
// The third view will start horizontally when the second view ends
upperFrame.origin.x += w;
// This will be the third view (blue)
UIView *blueViewU = [[UIView alloc] initWithFrame:upperFrame];
[blueViewU setBackgroundColor:[UIColor blueColor]];
// Adding the 3 views to the scroll view
[upperScrollView addSubview:redViewU];
[upperScrollView addSubview:greenViewU];
[upperScrollView addSubview:blueViewU];
// Now creating the view controller, father of the scrollview
UIViewController *upperViewController = [[UIViewController alloc] init];
[upperViewController setView:upperScrollView];
// MIDDLE SCROLLVIEW
// This will be the first view (red)
UIView *redViewM = [[UIView alloc] initWithFrame:middleFrame];
[redViewM setBackgroundColor:[UIColor redColor]];
// The second view will start horizontally when the first view ends
middleFrame.origin.x += w;
// This will be the second view (green)
UIView *greenViewM = [[UIView alloc] initWithFrame:middleFrame];
[greenViewM setBackgroundColor:[UIColor greenColor]];
// The third view will start horizontally when the second view ends
middleFrame.origin.x += w;
// This will be the third view (blue)
UIView *blueViewM = [[UIView alloc] initWithFrame:middleFrame];
[blueViewM setBackgroundColor:[UIColor blueColor]];
// Adding the 3 views to the scroll view
[middleScrollView addSubview:redViewM];
[middleScrollView addSubview:greenViewM];
[middleScrollView addSubview:blueViewM];
// Now creating the view controller, father of the scrollview
UIViewController *middleViewController = [[UIViewController alloc] init];
[middleViewController setView:middleScrollView];
// LOWER SCROLLVIEW
// This will be the first view (red)
UIView *redViewL = [[UIView alloc] initWithFrame:lowerFrame];
[redViewL setBackgroundColor:[UIColor redColor]];
// The second view will start horizontally when the first view ends
lowerFrame.origin.x += w;
// This will be the second view (green)
UIView *greenViewL = [[UIView alloc] initWithFrame:lowerFrame];
[greenViewL setBackgroundColor:[UIColor greenColor]];
// The third view will start horizontally when the second view ends
lowerFrame.origin.x += w;
// This will be the third view (blue)
UIView *blueViewL = [[UIView alloc] initWithFrame:lowerFrame];
[blueViewL setBackgroundColor:[UIColor blueColor]];
// Adding the 3 views to the scroll view
[lowerScrollView addSubview:redViewL];
[lowerScrollView addSubview:greenViewL];
[lowerScrollView addSubview:blueViewL];
// Now creating the view controller, father of the scrollview
UIViewController *lowerViewController = [[UIViewController alloc] init];
[lowerViewController setView:lowerScrollView];
// A white background view to see at least the scroll indicators
UIView *whiteView = [[UIView alloc] initWithFrame:bounds];
[whiteView setBackgroundColor:[UIColor whiteColor]];
// Finally the window will hold the view controllers' views
[[self window] addSubview:whiteView];
[[self window] addSubview:upperViewController.view];
[[self window] addSubview:middleViewController.view];
[[self window] addSubview:lowerViewController.view];
// Displaying the window
[self.window makeKeyAndVisible];
return YES;
}
Solved: after succeeding doing the same thing with IB I realized that for some reason the views inside the UIScrollView want RELATIVE x,y coordinates for their frames, not ABSOLUTE.
Changing the frames of the views in the above code with the following (the same for every UIScrollView):
CGRect redViewFrame = CGRectMake(x,y,w,h/3);
CGRect greenViewFrame = CGRectMake(w,y,w,h/3);
CGRect blueViewFrame = CGRectMake(2 * w,y,w,h/3);
solved the issue, and now I can see the 3 scrolling backgrounds.
Hope it helps someone else :) I am done, for now.

iOS UIScrollView in UIView

I have a little bit specific question. It might not matter for most people but I have had to deal with it and I had to solve the issue described below. I tried to find some information about it using Google and the Apple SDK documentation but did not succeed.
I was a designing a screen where there were many images in horizontal scrolls. There three three same scrolls. Every scroll had title. I have implemented custom class derived from UIView and placed there UIScrollView for scroll and UILabel for title text:
#interface MyView : UIView {
UIScrollView *iScrollView;
UIView *iTitleView;
}
I then put objects of this class on the view of a UIViewController:
#implementation MyViewController
- (void) viewDidLoad {
[super viewDidLoad];
...
iScrollViewTop = [[MyView alloc] init];
[self.view addSubview:iScrollViewTop];
...
}
#end
When I filled the internal scroll view with images and ran my application it looked OK. But there was some strange behavior. First, scroll did not have bounces as if I had set
iScrollView.bounces = NO;
and second, when I swiped to scroll, after the scroll stopped, the scroll bar did not disappear within one second. It was strange for me, because when I usually create a UIScrollView and add it to the UIViewController's view it has bounces and scroll bar disappears immediately when it stops. I tried to change UIScrollView's properties, such as directionalLockEnabled, pagingEnabled, canCancelContentTouches, delaysContentTouches, decelerationRate and others. In fact, I have tried to change almost all properties of UIScrollView but I could not get the scroll bars to immediately disappear.
If I try to add UIScrollView instead MyView to the UIViewController.view, it bounces and scroll bar disappears immediately after it stops. Also I get correct behavior if I subclass MyView from UIScrollView but in this case I cannot manage the title label because it scrolls together with other content.
So here are my questions:
Do you know why I am seeing this behavior?
How can I get "usual" behavior for scroll encapsulated by UIView?
ok, hacky code follows, so ignore all my other issues, but follow this pattern (westie.jpg = image that was 360x200)
#interface MyView : UIView
{
UIScrollView *sv;
UILabel *l;
}
-(MyView*)initWithFrame:(CGRect)frame;
#end
#implementation MyView
-(MyView*)initWithFrame:(CGRect)frame;
{
self = [super initWithFrame:frame];
sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,360,200)];
sv.scrollEnabled = YES;
sv.contentSize = CGSizeMake(360*3,200);
[self addSubview:sv];
UIImage *i1 = [UIImage imageNamed:#"westie.jpg"];
UIImageView *iv1 = [[UIImageView alloc] initWithImage:i1];
iv1.frame = CGRectMake(360*0, 0, 360, 200);
[sv addSubview:iv1];
UIImageView *iv2 = [[UIImageView alloc] initWithImage:i1];
iv2.frame = CGRectMake(360*1, 0, 360, 200);
[sv addSubview:iv2];
UIImageView *iv3 = [[UIImageView alloc] initWithImage:i1];
iv3.frame = CGRectMake(360*2, 0, 360, 200);
[sv addSubview:iv3];
l = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
l.text = #"Hello World";
l.backgroundColor = [UIColor blueColor];
[self addSubview:l];
return self;
}
#end
later, in your outer view creation:
[window addSubview:[[MyView alloc] initWithFrame:CGRectMake(0, 20, 360, 200)]];

Resources