Issues with UISegmentedControl and switch statements - ios

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.

Related

Extra White Space in Horizontal Scrollview of UITableViews

Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[self colorWithHexString:#"d944f7"]];
self.pageControl = [[UIPageControl alloc] init];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width - 20, self.view.frame.size.height)];
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.scrollView.delegate = self;
self.scrollView.clipsToBounds = NO;
self.scrollView.pagingEnabled = YES;
self.automaticallyAdjustsScrollViewInsets = NO;
self.scrollView.contentInset = UIEdgeInsetsMake(0,0,0,0);
[self.view addSubview:self.scrollView];
}
- (void)reloadData
{
self.pageControl.numberOfPages = self.sectionsArray.count;
self.pageControl.currentPage = 0;
self.pageControl.pageIndicatorTintColor = [UIColor colorWithRed:(rand()%256)/256.0 green:(rand()%256)/256.0 blue:(rand()%256)/256.0 alpha:1];
self.pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:(rand()%256)/256.0 green:(rand()%256)/256.0 blue:(rand()%256)/256.0 alpha:1];
self.pageControl.center = CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.height - 20);
self.pageControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.pageControl];
self.scrollView.contentOffset = CGPointMake(0, 0);
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width*self.pageControl.numberOfPages, self.scrollView.frame.size.height);
NSMutableArray *array = [NSMutableArray array];
NSMutableArray *mvcs = [NSMutableArray array];
NSMutableArray *shadows = [NSMutableArray array];
for (int i = 0 ; i < self.pageControl.numberOfPages; i ++) {
UIView *shadow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width - 20, self.scrollView.frame.size.height - 80)];
shadow.backgroundColor = [UIColor grayColor];
shadow.alpha = 1;
CGPoint pageCenter = CGPointMake((i + 0.5)*self.scrollView.frame.size.width, self.scrollView.frame.size.height/2.0);
shadow.center = pageCenter;
// [shadows addObject:shadow];
//[self.scrollView addSubview:shadow];
MDIMovingView *mv = [[MDIMovingView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width - 5, self.scrollView.frame.size.height - 80)];
mv.identity = 1;
mv.delegate = self;
mv.moveEnabled=NO;
mv.backgroundColor = [self colorWithHexString:#"e6e6e6"];
mv.center = pageCenter;
mv.layer.cornerRadius=10;
mv.clipsToBounds=NO;
[array addObject:mv];
[self.scrollView addSubview:mv];
UIView *topHeader=[[UIView alloc] initWithFrame:CGRectMake(0, 0, mv.frame.size.width, 50)];
[topHeader setRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight radius:10.0];
[topHeader setBackgroundColor:[self colorWithHexString:#"dca2e8"]];
[mv addSubview:topHeader];
UIView *invisibleSeperator =[[UIView alloc] initWithFrame:CGRectMake(0, 50, mv.frame.size.width, 2)];
[invisibleSeperator setBackgroundColor:[self colorWithHexString:#"d944f7"]];
[mv addSubview:invisibleSeperator];
MDITableViewController *vc = [[MDITableViewController alloc] init];
[mvcs addObject:vc];
vc.objects = self.sortedTasksArray[i];
vc.delegate = self;
vc.dataSoure = self;
vc.movingViewDelegate = self;
[self addChildViewController:vc];
vc.view.frame = CGRectMake(5, 60, mv.frame.size.width - 10, mv.frame.size.height - 80);
[mv addSubview:vc.view];
}
self.shadowViews = shadows;
self.movingViews = array;
self.tableVCs = mvcs;
}
Here is what its doing- note the space between navigation bar and the movingview:
You have to disable the automaticallyAdjustsScrollViewInsets in your view controller
[self setAutomaticallyAdjustsScrollViewInsets:NO];
or in Storyboard
Uncheck the option

ScrollView for images Objective C (iOS)

I am trying to create a welcome page for my app (with screenshots).
I have taken some samples and made it into a text scroll. I have tried for weeks without any success. Thanks
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
someScrollView.pagingEnabled = YES;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 0; i < numberOfPages; i++) {
UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * someScrollView.frame.size.width + 20,
20,
someScrollView.frame.size.width - 40,
20)];
tmpLabel.textAlignment = UITextAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i];
[someScrollView addSubview:tmpLabel];
}
Here is my failed attempt to do with images:
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
someScrollView.pagingEnabled = YES;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 0; i < numberOfPages; i++) {
UIImageView *tmpLabel = [[UIImageView alloc] initWithFrame:CGRectMake(i * someScrollView.frame.size.width + 20,
20,
someScrollView.frame.size.width - 40,
20)];
tmpLabel.imageAlignment = UIImageAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i];
[someScrollView addSubview:tmpLabel];
}
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:subview1.frame];
if (i == 0) {
imageView1.image = [UIImage imageNamed:#"openingScreen1.png"];
}else if (i == 1){
imageView1.image = [UIImage imageNamed:#"openingScreen2.png"];
}else if (i == 2){
imageView1.image = [UIImage imageNamed:#"openingScreen3.png"];
}else {
imageView1.image = [UIImage imageNamed:#"openingScreen4.png"];
}
I also might need to remove the word "Parse" unless the images above sit on top of it? I cannot locate the word.
try this
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
someScrollView.pagingEnabled = YES;
[someScrollView setAlwaysBounceVertical:NO];
//setup internal views
NSInteger numberOfPages = 5;
for (int i = 0; i < numberOfPages; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width+10;
UILabel *tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOrigin,70,self.view.frame.size.width,25)];
tmpLabel.textAlignment = NSTextAlignmentCenter;
tmpLabel.text = [NSString stringWithFormat:#"THIS ACTUALLY WORKS!! %d", i+1];
[someScrollView addSubview:tmpLabel];
// calculate the width
UIImageView *image = [[UIImageView alloc] initWithFrame:
CGRectMake(xOrigin, tmpLabel.frame.size.height + tmpLabel.frame.origin.y + 10,
self.view.frame.size.width,
self.view.frame.size.height)]; // customize your width, height and y co ordinates
image.image = [UIImage imageNamed:[NSString stringWithFormat:
#"openingScreen%d.jpg", i+1]];
image.contentMode = UIViewContentModeScaleAspectFit;
[someScrollView addSubview:image];
}
//set the scroll view content size
someScrollView.backgroundColor = [UIColor blueColor];
someScrollView.contentSize = CGSizeMake(self.view.frame.size.width *
numberOfPages,
self.view.frame.size.height);
//add the scrollview to this view
[self.view addSubview:someScrollView];
the sample OutPut is
here I attached the sample Project also, please use this
Please change your code to something like
int numberOfPages = 5;
UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
someScrollView.pagingEnabled = YES;
//set the scroll view delegate to self so that we can listen for changes
someScrollView.delegate = self;
someScrollView.contentSize = CGSizeMake(numberOfPages * someScrollView.frame.size.width, someScrollView.frame.size.height);
[self.view addSubview:someScrollView];
for (int i = 1; i <= numberOfPages; i++) {
//set the origin of the sub view
CGFloat myOrigin = i * self.view.frame.size.width;
//get the sub view and set the frame
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, someScrollView.frame.size.height)];
imageView1.image = [UIImage imageNamed: [NSString stringWithFormat:#"openingScreen%d.png",i ]];
//add the subview to the scroll view
[someScrollView addSubview:imageView1];
}
Add scroll view Delegate methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
}
//dragging ends, please switch off paging to listen for this event
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *) targetContentOffset NS_AVAILABLE_IOS(5_0)
{
//find the page number you are on
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
NSLog(#"Dragging - You are now on page %i",page);
}

How to add images in nested UIScrollview

I need to add images in scroll view multiple times. Am created a scrollview but images not append correctly in that.
My code is here :
-(void)sampleScroll
{
int x = 10;
int y = 20;
mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
mainScrollView.contentSize = CGSizeMake(50, (y + 95) * 5);
// further configure
[self.view addSubview: mainScrollView];
images = [[NSMutableArray alloc] initWithObjects:#"image0.jpg",#"image1.jpg",#"image2.jpg",#"image3.jpg", nil];
for(int i=0; i<[images count]; i++)
{
NSLog(#"%#",images);
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 250, 150)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
scrollview.backgroundColor = [UIColor whiteColor];
NSString *img = [images objectAtIndex:i];
NSLog(#"%#",img);
imageView.image = [UIImage imageNamed:#"image3.jpg"];
NSLog(#"%#",imageView.image);
scrollview.contentSize = CGSizeMake(1250,250);
[scrollview addSubview:imageView];
[mainScrollView addSubview:scrollview];
y=y+155;
//[self myscrollView];
}
}
Please give me a solution. Thanks in advance..
Frame adjustment needed,
int x = 10;
int y = 10;
UIScrollView * mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
mainScrollView.contentSize = CGSizeMake(mainScrollView.frame.size.width, mainScrollView.frame.size.height * COUNTS);
mainScrollView.pagingEnabled = YES;
for(int i = 0; i < COUNTS; i++)
{
UIScrollView * scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(x, y, mainScrollView.frame.size.width - 20.0, mainScrollView.frame.size.height - 20.0)];
scrollview.showsVerticalScrollIndicator = YES;
scrollview.scrollEnabled = YES;
scrollview.userInteractionEnabled = YES;
scrollview.backgroundColor = [UIColor whiteColor];
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(x,10.0, mainScrollView.frame.size.width - 40.0, mainScrollView.frame.size.height - 40.0)];
imageView.image = [UIImage imageNamed:#"owl.jpg"];
[scrollview addSubview:imageView];
[mainScrollView addSubview:scrollview];
y += mainScrollView.frame.size.height;
}
[self.view addSubview:mainScrollView];

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

layered UIScrollView

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];

Resources