layered UIScrollView - ios

I am using multiple UIScrollViews on-top of one another and Im using a UISegmentedControl to manage which one is active, my issue is, is that once I have set the position of a UIScrollView and gone to another, when I try and return I can only control the last selected one and the UIScrollView stays in the background. Does anyone know how to bring that UIScrollView back to the top?
Below is my code, cheers in advance!
- (void)layerSelected:(id)sender
{
int index = filterControl.selectedSegmentIndex;
switch (index)
{
case 0: if (scroll == nil)
{
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
scroll.delaysContentTouches = NO;
scroll.userInteractionEnabled = YES;
NSInteger viewCount = 15;
for (int i = 0; i < viewCount; i++)
{
CGFloat yOrigin = i * self.view.frame.size.width;
UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
[filterViewOverlay setImage:[filterManager objectAtIndex:i]];
[scroll addSubview:filterViewOverlay];
[filterViewOverlay release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
[self.scroll setContentOffset:currentPos1];
[self.view addSubview:scroll];
[scroll release];
}
else
{
[self.scroll setContentOffset:currentPos1];
scroll.scrollEnabled = YES;
}
break;

if both scroll views are a subview of the main view then you need to call [mainView bringSubviewToFront:scrollView];

Related

How to implement fling feature on an ImageView with Objective C?

I have an UIImageView and I want to implement fling feature, when users flings on the screen the page can go up or down quickly.
Now my thinks is add two UISwipeGesturesRecognizers, but I don't know how to do next, should I use an animation to do this, and how to calculate the animation distance and time?
Also I find other answers said no need gesture recognizer can use scroll view, I am totally confused, are there any sample I can learn?
You can do it like that :
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView setPagingEnabled:YES];
[scrollView setAlwaysBounceVertical:YES];
NSArray *imagesArray = [NSArray arrayWithObjects:#"img1.png", #"img2.png", #"img3.png", nil];
for (int i = 0; i < [imagesArray count]; i++)
{
CGFloat xOrigin = i * scrollView.frame.size.width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
[imageView setImage:[UIImage imageNamed:[imagesArray objectAtIndex:i]]];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[scrollView addSubview:imageView];
}
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height*[imagesArray count])];

How to Zoom Image on double tap and pinch with 2 finger in iOS?

In my application , used gallery concept. And this want to Zoom Image with double tap and using 2 finger.
I used below code for this but its not working for me. i enable to achieve this functionality..
Please resolve my problem.
- (void)viewDidLoad
{
[self setupScrollView];
}
-(void) setupScrollView
{
//add the scrollview to the view
image_scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
image_scroll.pagingEnabled = YES;
[image_scroll setAlwaysBounceVertical:NO];
//setup internal views
for (int i = 0; i < [self.PhotoImgArr count]; i++) {
current_page =(int)self.PhotoImgArr[i];
CGFloat xOrigin = i * self.view.frame.size.width;
AsyncImageView *image = [[AsyncImageView alloc] initWithFrame:
CGRectMake(xOrigin, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
image.imageURL = [NSURL URLWithString:self.PhotoImgArr[i]];
image.contentMode = UIViewContentModeScaleAspectFit;
[image_scroll addSubview:image];
}
//set the scroll view content size
[image_scroll setShowsHorizontalScrollIndicator:NO];
[image_scroll setShowsVerticalScrollIndicator:NO];
[image_scroll setMaximumZoomScale:8.0];
self.image_view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
image_scroll.decelerationRate = UIScrollViewDecelerationRateFast;
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[image_scroll addGestureRecognizer:doubleTap];
image_scroll.contentSize = CGSizeMake(self.view.frame.size.width *
self.PhotoImgArr.count,
self.view.frame.size.height);
//add the scrollview to this view
[self.view addSubview:image_scroll];
[image_scroll setZoomScale:[image_scroll minimumZoomScale]];
}
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
if(image_scroll.zoomScale > image_scroll.minimumZoomScale)
[image_scroll setZoomScale:image_scroll.minimumZoomScale animated:YES];
else
[image_scroll setZoomScale:image_scroll.maximumZoomScale animated:YES];
}
You should set userInteractionEnabled of image_scroll
image_scroll.userInteractionEnabled = YES;

Want to make my UIScrollView be infinite (like dates in a calendar)

I have a UIScrollView like so:
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
}
[scroll setShowsHorizontalScrollIndicator:NO];
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
This is the right gestures I want and is fine for a finite set of custom subviews, but how do I make the scollview go infinite?
I want the subviews to be dates and swiping left and right will bring the next or previous date.
Either use UIScrollViewDelegate method -(void)scrollViewDidScroll:(UIScrollView*)scrollView and adjust your view positions and the scrollviews content offset there, or have a look at Apple's example on how to create a infinite horizontal ScrollView by subclassing the UIScrollView: https://developer.apple.com/library/ios/samplecode/StreetScroller/

How can i add multiple images to my scrollView in ios

i have scroll View with three slides,i want to add images to my scroll view.this is my code after this what do i need to add
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];
}
If you want to add Images horizontally (say 3 images), than you have to add scroll view with the width of 3*yourImageView.frame.size.width and than you can add those Image on (x=0,y=0),
(x = yourImageView.frame.size.width, y=0), and (x = 2*yourImageView.frame.size.width, y=0)
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, yourImageView.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
imageView1.frame = CGrectMake(0,0,imageView1.frame.size.width,imageView1.frame.size.height);
imageView2.frame = CGrectMake(0,imageView1.frame.size.width,imageView2.frame.size.width,imageView2.frame.size.height)
imageView3.frame = CGrectMake(0,2*imageView1.frame.size.width,imageView3.frame.size.width,imageView3.frame.size.height)
[scroll addSubview:imageView1];
[scroll addSubview:imageView2];
[scroll addSubview:imageView3];
enable horizontal scrolling

Issues with UISegmentedControl and switch statements

I am trying to develop this app and Im have a really hard time. Im am trying to use a UISegmentedControl to load a set of UIScrollViews which act as image filters.
Aside from having huge issues with the UIScrollViews (which is why a new one is creed individually in each segment) for some reason, clicking on segment 1 loads all 6 UIScrollViews and only lets me scroll the first one. When I select the last segment only 1 UIScrollView is loaded...
I really don't know what Im doing wrong here so any suggestions would be great!
Thanks very much,
Lawrence
~~~~ Code Below ~~~
- (void)layerSelected:(id)sender
{
int index = filterControl.selectedSegmentIndex;
switch (index)
{
case 0: if (scroll == nil)
{
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
scroll.delaysContentTouches = NO;
scroll.userInteractionEnabled = YES;
NSInteger viewCount = 15;
for (int i = 0; i < viewCount; i++)
{
CGFloat yOrigin = i * self.view.frame.size.width;
UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
[filterViewOverlay setImage:[filterManager objectAtIndex:i]];
[scroll addSubview:filterViewOverlay];
[filterViewOverlay release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
[self.scroll setContentOffset:currentPos1];
[self.view addSubview:scroll];
[scroll release];
}
else
{
//Set the position to the correct one
}
case 1: if (scroll1 == nil)
{
scroll1 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll1.pagingEnabled = YES;
scroll1.delaysContentTouches = NO;
scroll1.userInteractionEnabled = YES;
NSInteger viewCount = 15;
for (int i = 0; i < viewCount; i++)
{
CGFloat yOrigin = i * self.view.frame.size.width;
UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
[filterViewOverlay setImage:[filterManager objectAtIndex:i]];
[scroll1 addSubview:filterViewOverlay];
[filterViewOverlay release];
}
scroll1.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
[self.scroll1 setContentOffset:currentPos2];
[self.view addSubview:scroll1];
[scroll1 release];
}
else
{
//Set the position to the correct one
}
case 2: if (scroll2 == nil)
{
scroll2 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll2.pagingEnabled = YES;
scroll2.delaysContentTouches = NO;
scroll2.userInteractionEnabled = YES;
NSInteger viewCount = 15;
for (int i = 0; i < viewCount; i++)
{
CGFloat yOrigin = i * self.view.frame.size.width;
UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
[filterViewOverlay setImage:[filterManager objectAtIndex:i]];
[scroll2 addSubview:filterViewOverlay];
[filterViewOverlay release];
}
scroll2.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
[self.scroll2 setContentOffset:currentPos3];
[self.view addSubview:scroll2];
[scroll2 release];
}
else
{
//Set the position to the correct one
}
case 3: if (scroll3 == nil)
{
scroll3 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll3.pagingEnabled = YES;
scroll3.delaysContentTouches = NO;
scroll3.userInteractionEnabled = YES;
NSInteger viewCount = 15;
for (int i = 0; i < viewCount; i++)
{
CGFloat yOrigin = i * self.view.frame.size.width;
UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
[filterViewOverlay setImage:[filterManager objectAtIndex:i]];
[scroll3 addSubview:filterViewOverlay];
[filterViewOverlay release];
}
scroll3.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
[self.scroll3 setContentOffset:currentPos4];
[self.view addSubview:scroll3];
[scroll3 release];
}
else
{
//Set the position to the correct one
}
case 4: if (scroll4 == nil)
{
scroll4 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll4.pagingEnabled = YES;
scroll4.delaysContentTouches = NO;
scroll4.userInteractionEnabled = YES;
NSInteger viewCount = 15;
for (int i = 0; i < viewCount; i++)
{
CGFloat yOrigin = i * self.view.frame.size.width;
UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
[filterViewOverlay setImage:[filterManager objectAtIndex:i]];
[scroll4 addSubview:filterViewOverlay];
[filterViewOverlay release];
}
scroll4.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
[self.scroll4 setContentOffset:currentPos5];
[self.view addSubview:scroll4];
[scroll4 release];
}
else
{
//Set the position to the correct one
}
case 5: if (scroll5 == nil)
{
scroll5 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll5.pagingEnabled = YES;
scroll5.delaysContentTouches = NO;
scroll5.userInteractionEnabled = YES;
NSInteger viewCount = 15;
for (int i = 0; i < viewCount; i++)
{
CGFloat yOrigin = i * self.view.frame.size.width;
UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
[filterViewOverlay setImage:[filterManager objectAtIndex:i]];
[scroll5 addSubview:filterViewOverlay];
[filterViewOverlay release];
}
scroll5.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
[self.scroll5 setContentOffset:currentPos6];
[self.view addSubview:scroll5];
[scroll5 release];
}
else
{
//Set the position to the correct one
}
}
}
You missed the break statements. Switch needs a break statement at the end of each case statement. Otherwise it executes all the code after the first susccessful match, regardless of the case values. ie.
switch (soemthing) {
case 1: // do something
break;
case 2: // do something else
break;
default:
}
Works as you expect. If you drop the breaks, then case 1 will execute all the code below it.

Resources