set UIscrollView page size - iphone

I have been searching for how to make a scroll menu with a UIScrollview wraping (looping).
I checked this question and the example works and maybe it's a good approach.
But instead of showing just one page (image) I need to display 3 pages and being able to scroll one page at time.
So how can I set the UIScrollView page size 1/3 smaller than the frame (I think this way it will work)??
Is it achievable?? If not, please direct me to another direction
Thanks

I believe what you are talking about is having a portion of the previous page and a portion of the next page visible. The way to do this is to set the scroll view's frame to be what you want (less than the screen, for example) and then turn off clipping of subviews for your scroll view. This will achieve the effect you are after.

[theScrollView setFrame:<a frame>]; will set the frame of the scroll view
[theScrollView setContentSize:<a Size>]; to set the content size of your scroll view
The size in 1. should be 1/3 of the size set in 2.

After some time, I end up needing to solve this issue again.
I found a Solution that worked for me.
Check this answer:
Change your scrollView size to the page size you want
Set your scroll.clipsToBounds = NO
Create a UIView subclass (e.g HackClipView) and override the hitTest:withEvent: method
-(UIView *) hitTest:(CGPoint) point withEvent:(UIEvent *)event
{
UIView* child = [super hitTest:point withEvent:event];
if (child == self && self.subviews.count > 0)
{
return self.subviews[0];
}
return child;
}
Set the HackClipView.clipsToBounds = YES
Put your scrollView in this HackClipView (with the total scrolling size you want)
See this answer for more details
Update:
As stated in lucius answer you can now implement the UIScollViewDelegate protocol and use the - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset method. As the targetContentOffset is a pointer. Using this method will not guarantee you the same result with scroll view pages as the user can scroll through many pages at once. But setting the descelerationRate to fast will almost give you the same result

Related

Create a scrollable menu with UIImages representing each possible selection. These UIImages must be able to snap to the grid one by one

I'm trying to create a menu (using UIScrollView) where the user can select images (using UIImageViews).
I intend to have at least 3 images visible at anytime. When I scroll, the images will be able to snap into position.
However, if I am to use a UIScrollView which is large enough to display 3 images at anyone time, and I enable paging, the paging will move all 3 images together and snap only at the next 3 images.
And if I am to use UIScrollView that fits just 1 image, and I enable clipsToBounds (which helps me draw the images beyond the boundary of the UIScrollView). My figure gestures can only be detected within UIScrollView itself, I want my figure gestures to be detected as long as any of the images are touched.
Thanks.
You can use following method of UIScrollView to scroll it from code as much as you want.
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
you can call this from following method of UIScrowViewDelegate.
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
Thus, you can manage how much you scroll view will call.
And for getting touches, implement a separate class that display images(inherit it from UIImageView or UIView) and implement touchesBegin or touchesEnded in that. Here when you get the touch in your custom view call some method of parent view and notify it with proper arguments(you can assign tag property and pass it to scroll view to find exactly which image was tapped).
.....I am not sure weather I am solving your problem or not.....so post here if you need any further assistance.
I wrote a custom UIView class which overrides the hitTest method of UIView.
In the header of the custom UIView class, declare a UIScrollView, later you will need to assign the UIScrollView which is displaying the menu to this variable.
UIScrollView *thatScrollView;
In the implementation, remember to synthesize the variable and overwrite hitTest
#synthesize thatScrollView;
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if ([self pointInside:point withEvent:event]) {
return thatScrollView;
}
return nil;
}
Then in the UIViewController class, create an instance of the class and assign the scrollview you want to scroll:
hiddenView.underneathButton = scrollView1;

Changing View Sizes when Scrolled

I have a scroll view with a couple of views in it. I want the views to be displayed such that the size of the view in the middle of the scrollview is the largest and the size of the other views keep decreasing from the middle to the ends. Also, when the view is scrolled, the views should be resized to follow this behavior. How do I start about with this?
You could try having your view controller adapt the UIScrollViewDelegate protocol and then use the scrollViewDidScrollFunction.
Inside the function you can check the content offset of your scrollview and resize your images accordingly. Something like:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGPoint offset = scrollView.contentOffset;
// In here goes your resizing code
}
Cheers

ScrollView Zooming problem

In TapToZoom sample(apple code), If i pinch(inwards) the image becomes smaller than the window size(If i remove fingers it will fit correctly to window). How to fix the image to the window if the content size of scroll view is less than window size. I spent a day to fix this problem but of no use...
I am attaching the snap..
The black color is my window of size(320*480) if i pinch the image has gone smaller than the window size(highlighted image) If i stop pinching at this time It will fit correctly to window. But i don't want my imageView to become smaller than window while pinching how to accomplish this?
Thanks,
You may want to look at the minimumZoomScale and maximumZoomScale properties of UIScrollView (Reference here)
I adding these condintions in my delegate to fix the problem...
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if(scrollView.zoomScale < minimumScale)
{
[scrollView setZoomScale:minimumScale];
}
else if(scrollView.zoomScale > 1)
{
[scrollView setZoomScale:1];
}
}

Can a UIScrollView be adapted to zoom horizontally but not vertically? (how)

I'm trying to make a timeline view that shows events as UIButtons that can be pressed for more info.
Ideally I want to lay out the buttons on a UIScrollView, and allow them to stretch/shrink horizontally, but not vertically.
What I'm trying to accomplish would basically be equivalent to using pinch gestures to resize the content view, and using a scroll view to move side to side - could you point me in the right direction, or suggest how you might accomplish something like this?
My first thought is a UIScrollView to scale a UIView subview, and have the UIView subview contain another subview that does not resize vertically, but only does horizontally when bounds change, and disable vertical scrolling. Maybe I could skip one of these UIView subviews and have only one do everything. It just feels like I'm trying to hack this together like an HTML page or something with all these containers.
Not sure if I've explained any of this well enough to hope for an answer, any help is appreciated though.
I've implemented horizontal-only scaling by creating a subclass of UIView that simply overrides setTransform and sets the Y scale to 1 whenever the UIScrollView changes the scale:
#interface DiagramStripView : UIView { }
#end
#implementation DiagramStripView
- (void)setTransform:(CGAffineTransform)newValue;
{
newValue.d = 1.0;
[super setTransform:newValue];
}
#end
As the class name suggests, my view holds a series of diagrams that are each one screen wide. When the user lets go, the view controller resets the view's scale to 1 and redraws everything to the new scale:
- (void) scrollViewDidEndZooming:(UIScrollView *)scrollView
withView:(UIView *)view atScale:(float)scale
{
diagramStripView.transform = CGAffineTransformIdentity;
[self redrawDiagrams:scale];
}
Haven't tried this, but you could try placing everything in a UIScrollView, and whenever you detect a zoom level change, adjust all of the child view transforms to CGAffineTransformMakeScale(1.0, 1.0/zoomLevel). This way, the scrollview is zooming everything up, but each subview is scaling themselves vertically downward, canceling out the zoom.
Assuming you don't actually want to stretch/shrink the button text, the easiest way is to resize all the buttons. I know, it's a pain.

UIScrollView doesn't bounce

I have a UIScrollView contained within a custom UIView with a content size larger than the ScrollView's frame.
I am able to drag scroll as I expect, but the thing doesn't give me the rubber banding effect that you get with the UITableView or UIWebView. It just stops when you get to one of the extremes.
I have set bounce = YES, is there something else I'm supposed to do?
I read the docs, and they say I have to implement the delegate. I did that.
They also say I should change the zoom levels, but I don't want the user to actually be able to zoom so I haven't set these.
For anyone that finds this thread later, if you are subclassing UIView and re-setting the UIScrollView's frame on every layoutSubviews, that is the problem - it cancels the bounce:
http://openradar.appspot.com/8045239
You should do something similar to this:
- (void)layoutSubviews;
{
[super layoutSubviews];
CGRect frame = [self calculateScrollViewFrame];
if (!CGRectEqualToRect(frame, self.scrollView.frame))
self.scrollView.frame = frame;
}
I had the same problem, on a UIScrollView that wasn't all filled up (but I still wanted it to bounce). Just setted:
scroll.alwaysBounceVertical/Horizontal = YES;
And it worked as expected
It turns out that keeping the UIScrollView within my custom UIView was causing the trouble.
Once I switched my custom UIView to instead inherit from UIScrollView, then the bouncing started working.
That is interesting... Is there a lot going on while the user scrolls the scroll view? Maybe that could cause the problem. The iPhone can multitask, but not too much. Can I see your entire code having to do with the scroll view?