Nested uiscrollviews and custom handling event routing - ios

I have nested UIScrollViews in a such way that inner UIScrollView is located at second page of outer UIScrollView. (i.e. inner one's frame would be CGRectMake(320, 0, view.width, view.height)). Two scrollviews only goes horizontally. I would like to swipe only inner scroll view once it is on the screen (i.e outer scroll view moves to second page) and until inner one reaches to the end. Once inner one reaches to the end, then I would like to recognize swipe for outer one. I first try to set outerScrollView.scrollEnabled = NO on second page, but then inner one also didn't get received swipe gesture. I also tried to subclass UIScrollView and override hitTest:event to return inner one's UIView, that also didn't work out. Is there a way to handle swipe event to a specific view and block for other views?

Try detecting when the scroll view reaches the bottom of the inner view by using the code here. If it's reached the bottom, flip a boolean (remember to flip it back if the user moves the scroll view away from the bottom!). If the boolean is flipped, use the code here to pass the touches to the superview and allow horizontal scrolling of the containing UIScrollView. If you're having trouble forcing the scroll try the code here to force scrolling.
EDIT:
I just wrote some code to test this out. By restricting the directions each view could scroll, I was able to get it to work as you described without using most of what I referred to above (much simpler). Here's the code for the scroll views:
outer = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[outer setBackgroundColor:[UIColor redColor]];
[outer setShowsHorizontalScrollIndicator:true];
[outer setShowsVerticalScrollIndicator:false];
[outer setScrollEnabled:true];
[outer setDelegate:self];
[outer setAlwaysBounceHorizontal:true];
[outer setAlwaysBounceVertical:false];
outerContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width*2, self.view.bounds.size.height)];
[outerContent setBackgroundColor:[UIColor blueColor]];
[outer addSubview:outerContent];
outer.contentSize = outerContent.frame.size;
[self.view addSubview:outer];
inner = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[inner setBackgroundColor:[UIColor yellowColor]];
[inner setShowsHorizontalScrollIndicator:false];
[inner setShowsVerticalScrollIndicator:true];
[inner setAlwaysBounceHorizontal:false];
[inner setAlwaysBounceVertical:true];
[inner setScrollEnabled:true];
[inner setDelegate:self];
innerContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height*2)];
[innerContent setBackgroundColor:[UIColor greenColor]];
inner.contentSize = innerContent.frame.size;
[inner addSubview:innerContent];
[self.outerContent addSubview:inner];
I color coded the views so you could tell what was going on. "inner" and "outer" are UIScrollViews that I declared in my header file and synthesized in the class. "innerContent" and "outerContent" are UIViews also in the header file.

Related

UITapGestureRecognizer only triggered for starting area of UIScrollView

I have several sub views added to a UIScrollView in the main view controller. Each sub view's view controller has its own tap recognizer. The problem is, only tapping in the starting area on the screen is recognised. After scrolling the view and tapping beyond the starting area, the tapping handler will no longer be called.
This SO post UIButton in non-visible area of UIScrollView seems to be relevant but its solution description wasn't really clear to me...
A simple report project could be found at: dropbox link
Thanks.
Please check your contentview size or setbackground color for scrollview and contentview. Contenview size is not increasing i think.
I have downloaded your project and see that the
SSubViewController *pvc = [SSubViewController controllerWithSubViewID:0];
pvc frame is out of bound
SSubViewController *pvc = [SSubViewController controllerWithSubViewID:0];
[self.scrollView addSubview:pvc.view];
[self addChildViewController:pvc];
[pvc.view setBackgroundColor:[UIColor grayColor]];
[pvc.view setFrame:CGRectMake(0, 0, 400, 200)];
[pvc didMoveToParentViewController:self];
SSubViewController *pvc1 = [SSubViewController controllerWithSubViewID:1];
[self.scrollView addSubview:pvc1.view];
[pvc1.view setBackgroundColor:[UIColor yellowColor]];
[self addChildViewController:pvc1];
[pvc1 didMoveToParentViewController:self];
[pvc1.view setFrame:CGRectMake(0, 400, 400, 200)];
Now see the Colours
As you can see Gray color, and Yellow color frame

UIScrollView The view change at run

I'm trying to implement a ScrollView, but when I put a View inside, the View looks good in the preview but it's not the same when I run the application:
http://i.stack.imgur.com/ZZfEg.jpg
And I don't know how to resize a text of the width of the view.
Without the ScrollView I can do a good structured view but when I use ScrollView I cannot get a good view, how to do ?
Code here: http://pastebin.com/LN5FySku
I think this task is very easy if you use code rather than storyboard.
Use this code..
You can do this in Storyboard as well but i prefer using code for such tasks. Hope that helped.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 520)]; // little less than 568 to accommodate the tabBar
[scrollView setContentSize:CGSizeMake(320, 1500)];
UIView *largeView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 320, 100)];
[scrollView addSubview:largeView];
[self.view addSubview:scrollView];
for further more information check this link.
My problem is solved, the reasons:
UIImageView: I think my view have lost the images when i did a copy past of the view, so click on your UIImageView and set the image field.
UILabel: For the cropped text you have to create the Equal Width Constraint between the ContentView and the View, select the ContentView on the view hierarchy and Control + Drag to the View – you should get a pop-up that gives you the “Equal Widths” option.

iOS) How to change location of already added subview

I have three views : topView, middleView, bottomView.
When I clicked the button in the topView, I want to insert alpha view between topView and middleView.
but middleView and bottomView is already located by calling method [self.view addsubView:] at viewDidLoad.
So I have to change middleView and bottomView below the alpha view...
So I called method 'setFrame', but already added views didn't move to where i want to put them.
So I have to call method 'removeFromSuperView' but there is a big problem that middleView and bottomView are the master views that has multiple subviews into them...
I don't know how to solve this problem, please help me!
! middleView and bottomView, they just need to go down , moving issue is simple.
****UPDATE
My code about the issue is below :
authCodeView is alphaView
authCodeView = [[UIView alloc] initWithFrame:CGRectMake(0, topView.frame.size.height + 8, [UIScreen mainScreen].bounds.size.width, topView.frame.size.height)];
[authCodeView setBackgroundColor:[UIColor whiteColor]];
authCodeField = [[UITextField alloc] initWithFrame:CGRectMake(30, 0, authCodeView.frame.size.width - 30, authCodeView.frame.size.height)];
authCodeField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"인증번호 입력" attributes:#{NSForegroundColorAttributeName:tempGray}];
[authCodeView addSubview:authCodeField];
NSLog(#"%f",authCodeView.frame.origin.y+authCodeView.frame.size.height + 8);
[middleView setFrame:CGRectMake(100, authCodeView.frame.origin.y+authCodeView.frame.size.height + 8, [UIScreen mainScreen].bounds.size.width, 308.0f)];
[bottomView setFrame:CGRectMake(0, middleView.frame.origin.y+middleView.frame.size.height+8, [UIScreen mainScreen].bounds.size.width, bottomView.frame.size.height)];
[self.view addSubview:authCodeView];
[self.view addSubview:middleView];
[self.view addSubview:bottomView];
So I called method 'setFrame', but already added views didn't move to where i want to put them.
Then you passed the wrong rectangle to -setFrame:. That method changes the frame of the view, i.e. it's location and size in the coordinate system of its superview.
So I have to call method 'removeFromSuperView' but there is a big problem that middleView and bottomView are the master views that has multiple subviews
You don't need to remove a view just to change its position. You'd only need to remove it if you want to add it to some other view instead, or if you no longer want it in the superview at all. That said, it's not a problem if a view like middleView has subviews -- those will go along with middleView if you remove it from its superview, or if you change middleView's position, etc.
I don't know how to solve this problem, please help me!
When asking for help, you really need to post code that reproduces the problem you're trying to solve.

iOS - Background view is consuming touch events

I'll start by describing how the screen looks. It is a UIPageViewController that covers 100% of the screen and a UIView (on top of the page vc) that covers 20% of the screen. Easy, right? basically UIView is over UIPageViewController.
So, I'm adding said UIPageViewController programatically to my view controller with self.view.addSubview(pageVC.view). but the UIView is defined in the interface builder
For this reason, since the UIPageViewController is added last it covers the UIView which forced me to call myUIView.layer.zPosition = 1 so that the UIView is visible.
The problem is any touch made on the UIView is instead consumed by the UIPageViewController. how can I solve this?
The layer property of a particular view is only used for the rendering of that specific view. That's the reason why changes to the layer won't impact anything regarding the interaction with that specific view.
However, the -sendSubviewToBack: and -bringSubviewToFront: methods on UIView were designed to achieve exactly what you are looking for. Here you can read the docs for them.
I wrote this piece of code so that you can test what this method is doing. Try to comment/uncomment the last line or changing the order in which the subviews are added to get a better understanding of how it works.
UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
UIView *secondView = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 150, 150)];
firstView.backgroundColor = [UIColor greenColor];
secondView.backgroundColor = [UIColor redColor];
[self.view addSubview:firstView];
[self.view addSubview:secondView];
UITapGestureRecognizer *firstTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(firstTap)];
UITapGestureRecognizer *secondTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(secondTap)];
[firstView addGestureRecognizer:firstTap];
[secondView addGestureRecognizer:secondTap];
[self.view sendSubviewToBack:secondView];

Changing programatic uiscrollview to IB

got a bit of a small problem here. I've been following this tutorial which creates an automatic scrolling slideshow using uiscrollview. This piece of code is used in my viewdidload -
UIScrollView *scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 120, 320, 180)];
scr.tag = 1;
scr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:scr];
[self setupScrollView:scr];
UIPageControl *pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 280, 320, 60)];
[pgCtr setTag:12];
pgCtr.numberOfPages=10;
pgCtr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:pgCtr];
As you can see a small uiscrollview is created programatically which is 320x180. It works perfectly except for the fact that my UI has another uiscrollview which takes up the whole ui, which is around 320x700. I need to embed this programatically created scrollview into it, any ideas? Or alternatively I need to create my own scrollview using storyboards and link it across using code, but have no idea how I would go about this.
Any ideas would be much appreciated.
You have to embed the scroll view inside the scroll view not the main view
[self.scrollView addSubview:scr];

Resources