Nested UIScrollViews - ios

I have a mainScrollView scrolling horizontally with photos added to it and mainScrollView's contentSize is set according to the number of photos.
To achieve the effect of scrolling to the end and swiping in a UIView with display info for user, I added the overlayScrollView to mainScrollView's subviews.
overlayScrollView contains the contentView positioned outside the frame.
CGRect frame = self.mainScrollView.frame;
frame.origin.x = CGRectGetWidth(frame) * (count-1);
UIScrollView *overlayScrollView = [[UIScrollView alloc] initWithFrame:frame];
[self.mainScrollView addSubview:overlayScrollView];
// moves contentView one frame beyond
frame.origin.x = CGRectGetWidth(frame);
UIView *contentView = [[UIView alloc] initWithFrame:frame];
contentView.backgroundColor = [UIColor redColor];
[overlayScrollView addSubview:contentView];
overlayScrollView.contentSize = CGSizeMake(overlayScrollView.frame.size.width * 2, overlayScrollView.frame.size.height);
overlayScrollView.pagingEnabled = YES;
I searched through SO on nested UIScrollViews and so subclassed UIScrollView for mainScrollView with the following method:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
for (UIView *child in self.subviews){
if([child isMemberOfClass:[UIScrollView class]])
if(CGRectContainsPoint(child.frame, point))
return child;
}
return self;
}
But it doesn't work. My code could be wrong. hitTest did return child (overlayScrollView) but did not scroll. Any suggestions how to code this?

Related

Adding scrollview to a view

I have tried Adding UIScrollView to a UIViewController, and I can never get my view to move down when I scroll, either on a simulator on a real device. I have also tried increasing the size, so my code is as follows:
+(void)addScrollViewToView:(UIView *)view
{
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.width * 2, view.bounds.size.height * 2)];
scrollView.backgroundColor = [UIColor clearColor];
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = YES;
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.contentSize = CGSizeMake(view.bounds.size.width * 2, view.bounds.size.height * 2);
[view insertSubview:scrollView atIndex:0];
//[view sendSubviewToBack:scrollView]; - commented out
}
It is called within viewDidLoad like so:
#implementation MyLoginViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[Utils addScrollViewToView:self.view];
self.passwordTextField.secureTextEntry = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
} // viewDidLoad method ends here
There is never any response when I try and scroll up with my mouse on a simulator. There is a background image to my view controller that I created using a storyboard. Would changing the index make any difference?
It happen because you add gesture recognizer. Gesture recognizer blocks and not pass your touch.
You can make this for dismiss keyboard with UIScrollView:
+(void)addScrollViewToView:(UIView *)view
{
// your code
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
}
And remove UITapGestureRecognizer.
Or is it because your frame size is the same as your content size? The scroll view will only scroll if its content is bigger than its frame.
What did the trick was adding the view to the scrollview.
I.e.:
View >
ScrollView >
View that I wanted to scroll
Not:
View that I wanted to scroll >
ScrollView.

UIScrollView, magic scrollable area

A simple example is used for demonstration only strange features of the UIScrollView.
Have an instance of UIScrollView that occupies the entire screen area. An instance of UIView is placed on UIScrollView instance and entirely overlapping it.
ScrollView is configured so that all swipes on him transferred in the overlapping View and the scroll didn't happen. So it should work, all swipes redirected to the view, since it completely overlaps the ScrollView.
But there is a region located in the lower left corner of the screen, when you swipe where sometimes (not always) starts scrolling.
What is this magical area? How to avoid this behavior?
Repeated only on the device (tried on iPhone6, iPhone6+, iPad Air), on the simulator was not able to repeat.
That's how we create and custom views:
- (void)viewDidLoad
{
[super viewDidLoad];
MyScrollView *sv = [[MyScrollView alloc] initWithFrame:self.view.bounds];
sv.canCancelContentTouches = NO;
sv.delaysContentTouches = NO;
sv.multipleTouchEnabled = NO;
sv.contentSize = CGSizeMake(sv.bounds.size.width, sv.bounds.size.height + 300);
sv.backgroundColor = [UIColor redColor];
[self.view addSubview:sv];
UIView *topView = [[UIView alloc] initWithFrame:sv.bounds];
topView.backgroundColor = [UIColor greenColor];
[sv addSubview:topView];
}
MyScrollView has a single overridden method:
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
return YES;
}
Thanks, sorry for my English.

UIScrollView can zoom, but cant pan

I have a UIScrollView with a UIView inside it, in side the UIView I made a button to add textLabels to it.
and ideally I would want a really big canvas and be able to put text on it and pan and zoom around. however with the UIScrollView it does zoom, but does not pan at all
It seems that when I remove the UIView that i add inside the UIScrollView it works fine.
heres viewDidLoad:
[super viewDidLoad];
CGFloat mainViewWidth = 700;
CGFloat mainViewHeight = 500;
//scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height * kNumberOfPages);
//self.mainScrollView.bounds = CGRectMake(0., 0., 3000, 3000);
self.mainScrollView.scrollsToTop = NO;
self.mainScrollView.delegate = self;
self.mainScrollView.maximumZoomScale = 50.;
self.mainScrollView.minimumZoomScale = .1;
self.mainScrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.mainView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, mainViewWidth, mainViewHeight)];
[self.mainView setUserInteractionEnabled:NO];
self.mainView.backgroundColor = [[UIColor alloc] initWithRed:0.82110049709463495
green:1
blue:0.95704295882687884
alpha:1];
[self.mainScrollView setContentSize:CGSizeMake(5000, 5000)];
[self.mainScrollView insertSubview:self.mainView atIndex:0];
Edit:
Heres the all I have for UIScrollViewDelegate
#pragma mark - Scroll View Delegate
- (void)scrollViewDidScroll:(UIScrollView *)sender {
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
//
return self.mainView;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)zoomedScrollView withView:(UIView *)view atScale:(float)scale
{
}
I just went through this exact same dilemma.
My UIScrollView exists within the storyboard, and if I add a UIView (containerView) within that storyboard to the UIScrollView, the image fails to pan. And all sorts of other centering weirdness occurs too.
But if I do it through code:
// Set up the image we want to scroll & zoom
UIImage *image = [UIImage imageNamed:#"plan-150ppi.jpg"];
self.imageView = [[UIImageView alloc] initWithImage:image];
self.containerView = [[UIView alloc] initWithFrame:self.imageView.frame];
[self.containerView addSubview:self.imageView];
[self.scrollView addSubview:self.containerView];
// Tell the scroll view the size of the contents
self.scrollView.contentSize = self.containerView.frame.size;
... then it works just fine.

UIScrollviews zoomToRect only sets the zoomScale but not the contentOffset

My UIScrollView is not setting its contentOffset when using zoomToRect.
I have an UIScrollView with an UIImageView inside. Scrolling and zooming itself is working so far. Now I want to give the scrollview at app start a certain zoomed rect of the image view. For this I implemented zoomToRect: and it is setting the zoomsScale correctly, but it does not set the contentOffset.
Expected outcome when using zoomToRect is that the UIScrollView zooms in or out according to the selected rect and set its contentOffset according to the origin coordinates of the rect given to the zoomToRect method.
The actual behaviour is that it zooms to the correct zoomScale but my UIImageView is always at the origin 0,0 and not the expected origin of the x (475) and y (520) coordinated of the rect I specified in zoomToRect.
My images size is 1473x1473.
Here is some the code
- (void)viewDidLoad {
CGRect bounds = self.view.frame;
_imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bgImage.png"]];
self.containerView = [[UIView alloc] initWithFrame:bounds];
_containerView.contentMode = UIViewContentModeCenter;
_scrollView = [[UIScrollView alloc] initWithFrame:bounds];
_scrollView.delegate = self;
_scrollView.contentSize = _imageView.bounds.size;
_scrollView.minimumZoomScale = 0.2;
[_scrollView addSubview:_containerView];
[_containerView addSubview:_imageView];
[self.view addSubview:_scrollView];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_scrollView zoomToRect:CGRectMake(475.0, 150.0, 520.0, 747.0) animated:NO];
}
#pragma mark UIScrollViewDelegate methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return _containerView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
[self printVisibleRectToConsole:scrollView];
CGSize newImageViewSizeWithScale = CGSizeMake(_imageView.bounds.size.width * _scrollView.zoomScale,
_imageView.bounds.size.height * _scrollView.zoomScale);
_scrollView.contentSize = newImageViewSizeWithScale;
}
My questions:
Why does zoomToRect does not set the contentOffset?
How can I get zoomToRect to change my contentOffset as expected?
The problem is that the view you're zooming (containerView) is not as big as the image view it contains (and which you actually want to zoom). Its frame is set to the frame of the view controller. You don't see this because a UIView doesn't clip its subviews by default.
You should initialize containerView with the bounds of your image view instead.
self.containerView = [[UIView alloc] initWithFrame:_imageView.bounds];

iphone::touchesMoved: Move UIImageView inside UIView?

This is my code:
- (void) touchesBegan: (NSSet *)touches withEvent: (UIEvent *)event {
CGPoint pt = [[touches anyObject] locationInView:self.view];
startLocation = pt;
[super touchesBegan: touches withEvent: event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pt = [[touches anyObject] locationInView:self.view];
CGRect frame = [self.view frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
[self.view setFrame:frame];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *v = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 100, 100)];
v.backgroundColor = [UIColor redColor];
UIImageView *imv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"boo2.png"]];
imv.frame = CGRectMake(0, 0, 50, 50);
[v addSubview:imv];
[self.view addSubview: v];
[v release];
UIView *v2 = [[UIView alloc] initWithFrame: CGRectMake(100, 100, 100, 100)];
v2.backgroundColor = [UIColor blueColor];
UIImageView *imv2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"boo2.png"]];
imv2.frame = CGRectMake(0, 0, 50, 50);
[v2 addSubview:imv2];
[self.view addSubview: v2];
[v2 release];
}
What I want to do is to move the UIImageView inside the UIView, not the entire view. But, the code above, when I try to drag, it moves the entire view.
NOTE: The images are return from others functions, not declared in viewdidload. Assume, you cant declare UIImageView variables.
I'm not quite sure what you're trying to do. and I don't know why you put each of the UIImageView's inside a UIView which you put into the view controller's view. You can just as well add the UIImageView's as sub-views of the view controller's view directly.
Nevertheless, from what I can make out it looks like the user must be able to drag the images on the screen around.
If this is the case, I would suggest that you store each one of the UIImageView's in an array. Then, in touchesBegan:withEvent:, you will have to determine which UIImageView is being touched. Getting the CGPoint representing the location in the main view, as you do in your code, is a good start. If you added the UIImageView's directly as sub-views of the main view, you can then compare the coordinates of this CGPoint with the frames of these UIImageView's to determine which one is being pressed.
Then, to allow dragging, you need to save the coordinates of the UIImageView at the beginning of the touch. Then, everytime the touch moves, you can compute the new x-position of the UIImageView as the original x-position, plus the distance moved. That is,
newImageViewX = imageViewStartX + (newTouchX - touchStartX);
and similarly for the y-position.

Resources