Circular Scroll View - ios

I have implemented a circular scroll view.
It's working fine , but after sometimes the scroll view delegates are not getting called.
I have attached the code , what is the bug in it ....
//
// MyScrollViewViewController.h
// MyScrollView
//
// Created by Biranchi on 26/05/09.
// Copyright PurpleTalk 2009. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface MyScrollViewViewController : UIViewController <UIScrollViewDelegate> {
IBOutlet UIScrollView *scrollView;
IBOutlet UIView *view1;
IBOutlet UIView *view2;
IBOutlet UIView *view3;
IBOutlet UILabel *label1;
IBOutlet UILabel *label2;
IBOutlet UILabel *label3;
int viewAtIndex;
int currentOffset;
int firstOffset, lastOffset;
int currentContentInset;
int tempOffset;
NSMutableArray *arr;
}
#end
//
// MyScrollViewViewController.m
// MyScrollView
//
// Created by Biranchi on 26/05/09.
// Copyright PurpleTalk 2009. All rights reserved.
//
#import "MyScrollViewViewController.h"
#implementation MyScrollViewViewController
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
//static int j = 0;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
arr = [[NSMutableArray alloc] init];
[arr addObject:view1];
[arr addObject:view2];
[arr addObject:view3];
//NSLog(#"arr : %#", arr);
scrollView.minimumZoomScale = 1.0f;
scrollView.maximumZoomScale = 2.0f;
scrollView.delegate = self;
//scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
firstOffset = 0;
currentOffset = 320;
lastOffset = 640;
currentContentInset = 320;
CGRect frame;
//Scroll View
frame = [view1 frame];
[view1 setBackgroundColor:[UIColor redColor]];
frame.origin.x = 0;
[view1 setFrame:frame];
frame = [view2 frame];
[view2 setBackgroundColor:[UIColor greenColor]];
frame.origin.x = 320;
[view2 setFrame:frame];
frame = [view3 frame];
[view3 setBackgroundColor:[UIColor blueColor]];
frame.origin.x = 640;
[view3 setFrame:frame];
[scrollView addSubview:view1];
[scrollView addSubview:view2];
[scrollView addSubview:view3];
[scrollView setContentSize:CGSizeMake(3*320, 480)];
[scrollView setContentOffset: CGPointMake(currentOffset,0) animated:NO];
[self.view addSubview:scrollView];
//NSLog(#"Self.view Subviews : %#", [self.view subviews]);
}
#pragma mark -
#pragma mark ScrollView Delegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView1
{
NSLog(#"Will begin dragging ===");
CGPoint offset = [scrollView1 contentOffset];
tempOffset = offset.x;
NSLog(#"Offset : %#", NSStringFromCGPoint(offset));
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView1
{
NSLog(#"ScrollView did end decelerating ");
int i, result;
CGPoint offset = [scrollView1 contentOffset];
currentOffset = offset.x;
NSLog(#"Offset : %#", NSStringFromCGPoint(offset));
if( ( (currentOffset - tempOffset) > 0 ) || ( (currentOffset == tempOffset) && (currentOffset == lastOffset) ) )
{
NSLog(#"Show next page , current : %d, temp : %d", currentOffset, tempOffset);
for(i=0; i<[arr count]; i++)
{
if( [[arr objectAtIndex:i] frame].origin.x == currentOffset )
{
NSLog(#"Current view on screen : %d", i);
break;
}
}
NSLog(#"Before Current offset : %d, last offset : %d", currentOffset, lastOffset);
if( currentOffset == lastOffset )
{
result = i-2;
if(result < 0)
{
result += 3;
}
NSLog(#"Change the frame of object at index : %d", result);
[[arr objectAtIndex:result] setFrame:CGRectMake(currentOffset + 320, 0, 320, 480)];
[scrollView1 setContentSize:CGSizeMake(currentOffset+640,480) ];
lastOffset += 320;
firstOffset += 320;
}
NSLog(#"Current offset : %d, first offset : %d , last offset : %d", currentOffset, firstOffset, lastOffset);
}
else if( ( (currentOffset - tempOffset) < 0 ) || ( (currentOffset == tempOffset) && (currentOffset == firstOffset) ) )
{
//NSLog(#"Show prev page");
for(i=0; i<[arr count]; i++)
{
if( [[arr objectAtIndex:i] frame].origin.x == currentOffset )
{
NSLog(#"==Current view on screen : %d", i);
break;
}
}
NSLog(#"==Before Current offset : %d, First offset : %d", currentOffset, firstOffset);
if( currentOffset == firstOffset )
{
result = i+2;
if(result >= 3)
{
result -= 3;
}
NSLog(#"==Change the frame of object at index : %d", result);
[[arr objectAtIndex:result] setFrame:CGRectMake(currentOffset - 320, 0, 320, 480)];
UIEdgeInsets contentInset = {0,currentContentInset,0,0};
[scrollView1 setContentInset:contentInset ];
firstOffset -= 320;
currentContentInset += 320;
lastOffset -= 320;
}
NSLog(#"==Current offset : %d, first offset : %d, Last Offset : %d", currentOffset, firstOffset, lastOffset);
}
else
{
NSLog(#"Error in offset");
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView1
{
return [[scrollView1 subviews] objectAtIndex:viewAtIndex];
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView1 withView:(UIView *)view atScale:(float)scale
{
//NSLog(#"View Zoomed : %#, scale : %f", view, scale);
}
#pragma mark -
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[arr release];
[super dealloc];
}
#end

Related

how to add/show toolbar below status bar in iOS?

in my iOS application, I am adding functionality for collapsing toolbar when table view scrolls. But when toolbar moves above along y axis, I got following result (toolbar contents mixing with status bar contents).
#interface ListViewController () <UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UILabel *labelPageTitle;
#property (weak, nonatomic) IBOutlet UITableView *listTableView;
#property (nonatomic) CGFloat previousScrollViewYOffset;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *toolbarTop;
#end
#implementation ListViewController
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
// CGRect frame = self.toolbars.frame;
// CGFloat size = frame.size.height - 21;
// CGFloat framePercentageHidden = ((20 - frame.origin.y) / (frame.size.height - 1));
// CGFloat scrollOffset = scrollView.contentOffset.y;
// CGFloat scrollDiff = scrollOffset - self.previousScrollViewYOffset;
// CGFloat scrollHeight = scrollView.frame.size.height;
//
// NSLog(#"scrollView.frame - %#", NSStringFromCGRect(scrollView.frame));
// NSLog(#"scrollView.contentInset - %#", NSStringFromUIEdgeInsets(scrollView.contentInset));
//
// CGFloat scrollContentSizeHeight = scrollView.contentSize.height + scrollView.contentInset.bottom;
//
// if (scrollOffset <= -scrollView.contentInset.top) {
// frame.origin.y = 20;
// } else if ((scrollOffset + scrollHeight) >= scrollContentSizeHeight) {
// frame.origin.y = -size;
// } else {
// frame.origin.y = MIN(20, MAX(-size, frame.origin.y - scrollDiff));
// }
//
// [self.toolbars setFrame:frame];
// [self updateBarButtonItems:(1 - framePercentageHidden)];
// self.previousScrollViewYOffset = scrollOffset;
//}
//
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
// [self stoppedScrolling];
//}
//
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
// willDecelerate:(BOOL)decelerate
//{
// if (!decelerate) {
// [self stoppedScrolling];
// }
//}
//
//- (void)stoppedScrolling
//{
// CGRect frame = self.navigationController.navigationBar.frame;
// if (frame.origin.y < 20) {
// [self animateNavBarTo:-(frame.size.height - 21)];
// }
//}
//
//- (void)updateBarButtonItems:(CGFloat)alpha
//{
// self.buttonDismiss.customView.alpha = alpha;
// self.labelPageTitle.alpha = alpha;
// self.toolbars.tintColor = [self.toolbars.tintColor colorWithAlphaComponent:alpha];
//}
//
//- (void)animateNavBarTo:(CGFloat)y
//{
// [UIView animateWithDuration:0.2 animations:^{
// CGRect frame = self.toolbars.frame;
// CGFloat alpha = (frame.origin.y >= y ? 0 : 1);
// frame.origin.y = y;
// [self.toolbars setFrame:frame];
// [self updateBarButtonItems:alpha];
// }];
//}
#pragma mark - view controllers life cycle methods
- (void)viewDidLoad {
[super viewDidLoad];
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars=NO;
self.automaticallyAdjustsScrollViewInsets=NO;
[self.view layoutIfNeeded];
_toolbarTop.constant = -34;
[self.listTableView setDataSource:self];
[self.listTableView setDelegate:self];
[Utils updateLabelFontSize:self.labelPageTitle ForInitialHeight:22 andInitialSize:21];
[self.labelPageTitle setText:#"My Category"/*self.productCategory*/];
}
Finally after playing for 4-5 hours, I come across the solution. First of all thanks to #Lion & #Desdenova for help.
Here is the link where I found some hint.
iOS8: How do I make statusBar opaque after navigationBar is hidden using hidesBarsOnSwipe?
as per suggestions in the post I just assigned one UIView at status bar frame with same color of toolbar tint.
Here is my updated code in view did load. Remaining is the same
- (void)viewDidLoad {
[super viewDidLoad];
// if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
// self.edgesForExtendedLayout = UIRectEdgeNone;
//
// self.extendedLayoutIncludesOpaqueBars=NO;
// self.automaticallyAdjustsScrollViewInsets=NO;
[self.view layoutIfNeeded];
_toolbarTop.constant = -34;
[self.listTableView setDataSource:self];
[self.listTableView setDelegate:self];
//let topBar = UIView(frame: UIApplication.sharedApplication().statusBarFrame)
UIView *statusBarView = [[UIView alloc] initWithFrame:[[UIApplication sharedApplication] statusBarFrame]];
statusBarView.backgroundColor = self.view.backgroundColor;
[self.view addSubview:statusBarView];
[Utils updateLabelFontSize:self.labelPageTitle ForInitialHeight:22 andInitialSize:21];
[self.labelPageTitle setText:#"My Category"/*self.productCategory*/];
}
Final Result
Do not pin constraint from TopLayoutGuide.Bottom, instead pin it with superview's top. You your toolbar's top should be pinned with superview's top. Check the below screenshot,

UIViewControllers in ContentView of UIScrollView?

I don't want to use page control because i have to change the button when the user scroll horizontally.
So I am using UIScrollview and container view.
By following this tutorial
I am able to add child view controller in container view but the scroll view does not scroll with auto layout.
Here is my code
- (void)viewDidLoad {
[super viewDidLoad];
self.array_pageContent = [[NSMutableArray alloc] init];
CallViewController *objGameReviewPageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"CallViewController"];
[self addChildViewController:objGameReviewPageContentViewController];
UIView * view = objGameReviewPageContentViewController.view;
HomeViewController *objHomeViewController= [self.storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"];
[self addChildViewController:objHomeViewController];
UIView * view1 = objHomeViewController.view;
GroupViewController *objGroupViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"GroupViewController"];
[self addChildViewController:objGroupViewController];
UIView * view2 = objGroupViewController.view;
CallenderViewController *objCallenderViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"CallenderViewController"];
[self addChildViewController:objCallenderViewController];
UIView * view3 = objGroupViewController.view;
CasesViewController *objCasesViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"CasesViewController"];
[self addChildViewController:objCasesViewController];
UIView * view4 = objGroupViewController.view;
[self.scrollViewContent setPagingEnabled:YES];
[self.scrollViewContent setScrollEnabled:YES];
[self.scrollViewContent setShowsHorizontalScrollIndicator:YES];
[self.scrollViewContent setShowsVerticalScrollIndicator:NO];
[self.scrollViewContent setDelegate:self];
[self.array_pageContent addObject:view];
[self.array_pageContent addObject:view1];
[self.array_pageContent addObject:view2];
[self.array_pageContent addObject:view3];
[self.array_pageContent addObject:view4];
NSInteger pageCount = self.array_pageContent.count;
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;
self.mutableArray_pageContentViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i) {
[self.mutableArray_pageContentViews addObject:[NSNull null]];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CGSize pagesScrollViewSize = self.scrollViewContent.frame.size;
self.scrollViewContent.contentSize = CGSizeMake(pagesScrollViewSize.width * self.array_pageContent.count, pagesScrollViewSize.height);
[self loadVisiblePages];
}
- (void)loadPage:(NSInteger)page {
if (page < 0 || page >= self.array_pageContent.count) {
return;
}
UIView *pageView = [self.mutableArray_pageContentViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
CGRect frame = self.scrollViewContent.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
UIView *newPageView = nil;
newPageView = [self.array_pageContent objectAtIndex:page];
newPageView.frame = frame;
[self.scrollViewContent addSubview:newPageView];
[self.mutableArray_pageContentViews replaceObjectAtIndex:page withObject:newPageView];
}
}
- (void)purgePage:(NSInteger)page {
if (page < 0 || page >= self.array_pageContent.count) {
return;
}
UIView *pageView = [self.mutableArray_pageContentViews objectAtIndex:page];
if ((NSNull*)pageView != [NSNull null]) {
[pageView removeFromSuperview];
[self.mutableArray_pageContentViews replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
- (void)loadVisiblePages {
CGFloat pageWidth = self.scrollViewContent.frame.size.width;
NSInteger page = (NSInteger)floor((self.scrollViewContent.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
self.pageControl.currentPage = page;
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;
for (NSInteger i=0; i<firstPage; i++) {
[self purgePage:i];
}
for (NSInteger i=firstPage; i<=lastPage; i++) {
[self loadPage:i];
}
for (NSInteger i=lastPage+1; i<self.array_pageContent.count; i++) {
[self purgePage:i];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self loadVisiblePages];
}
And the hierarchy for storyboard is
Remove Container View From Views hierarchy your scroll will be work.
and replcae your viewDidLoad() metod from following code.
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat width = 0.0;
self.array_pageContent = [[NSMutableArray alloc] init];
CallViewController *objGameReviewPageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"CallViewController"];
[self addChildViewController:objGameReviewPageContentViewController];
width = objGameReviewPageContentViewController.view.frame.size.width;
[self.scrollViewContent addSubview:objGameReviewPageContentViewController.view];
// UIView * view = objGameReviewPageContentViewController.view;
HomeViewController *objHomeViewController= [self.storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"];
CGRect frame = objHomeViewController.view.frame;
frame.origin.x = objGameReviewPageContentViewController.view.frame.size.width;
objHomeViewController.view.frame = frame;
width += frame.size.width;
NSLog(#"%f",width);
[self addChildViewController:objHomeViewController];
[self.scrollViewContent addSubview:objHomeViewController.view];
// UIView * view1 = objHomeViewController.view;
GroupViewController *objGroupViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"GroupViewController"];
frame = objGroupViewController.view.frame;
frame.origin.x = objGameReviewPageContentViewController.view.frame.size.width*2;
objGroupViewController.view.frame = frame;
width += frame.size.width;
NSLog(#"%f",width);
[self addChildViewController:objGroupViewController];
[self.scrollViewContent addSubview:objGroupViewController.view];
// [self addChildViewController:objGroupViewController];
// UIView * view2 = objGroupViewController.view;
CallenderViewController *objCallenderViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"CallenderViewController"];
frame = objCallenderViewController.view.frame;
frame.origin.x = objGameReviewPageContentViewController.view.frame.size.width*3;
objCallenderViewController.view.frame = frame;
width += frame.size.width;
NSLog(#"%f",width);
[self addChildViewController:objCallenderViewController];
[self.scrollViewContent addSubview:objCallenderViewController.view];
// [self addChildViewController:objCallenderViewController];
// UIView * view3 = objGroupViewController.view;
CasesViewController *objCasesViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"CasesViewController"];
frame = objCasesViewController.view.frame;
frame.origin.x = objGameReviewPageContentViewController.view.frame.size.width*4;
objCasesViewController.view.frame = frame;
width += frame.size.width;
NSLog(#"%f",width);
[self addChildViewController:objCasesViewController];
[self.scrollViewContent addSubview:objCasesViewController.view];
// [self addChildViewController:objCasesViewController];
// UIView * view4 = objGroupViewController.view;
[self.scrollViewContent setPagingEnabled:YES];
[self.scrollViewContent setScrollEnabled:YES];
[self.scrollViewContent setShowsHorizontalScrollIndicator:YES];
[self.scrollViewContent setShowsVerticalScrollIndicator:NO];
[self.scrollViewContent setDelegate:self];
self.scrollViewContent.contentSize = CGSizeMake(width, self.scrollViewContent.frame.size.height);
// [self.array_pageContent addObject:view];
// [self.array_pageContent addObject:view1];
// [self.array_pageContent addObject:view2];
// [self.array_pageContent addObject:view3];
// [self.array_pageContent addObject:view4];
// NSInteger pageCount = self.array_pageContent.count;
//
// self.pageControl.currentPage = 0;
// self.pageControl.numberOfPages = pageCount;
//
// self.mutableArray_pageContentViews = [[NSMutableArray alloc] init];
// for (NSInteger i = 0; i < pageCount; ++i) {
// [self.mutableArray_pageContentViews addObject:[NSNull null]];
// }
}
Please follow below link. this will solve your problem. Also please don't write too much code, it will be easily done with storyboard.
https://github.com/mluton/EmbeddedSwapping

how to add text view and image view on different views

I have added view controllers according to my array count and set the content size of the horizontal scroll view.now i want some text to be displayed on each view.
for this i have taken a for loop but i'm not able to add text views over the views.
this is my code:
-(void)loadScrollView
{
scrollView.contentSize = CGSizeMake(0, scrollView.frame.size.height);
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < [_arrUrlLinks count]; i++) {
[controllers addObject:[NSNull null]];
}
// MyViewController1 *controller;
self.viewControllers = controllers;
// self.viewControllers =_txtView;
count=1;
// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [_arrUrlLinks count], scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator =YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
pageControl.numberOfPages = [_arrUrlLinks count];
pageControl.currentPage = 0;
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= [_arrUrlLinks count])
return;
controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:#"iPhone"])
{
controller = [[MyViewController1 alloc] initWithNibName:#"MyViewController1" bundle:nil];
}
else{
controller = [[MyViewController1 alloc] initWithNibName:#"MyViewController1_ipad" bundle:nil];
}
[controller initWithPageNumber: page];
NSLog(#"loadscrollviewwithpage");
[viewControllers replaceObjectAtIndex:page withObject:controller];
}
// add the controller's view to the scroll view
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
NSLog(#"111111%#",resultDicArray);
NSLog(#"0000000%#",body);
NSLog(#"header======.>>>>..%#",headerstr);
}
[scrollView addSubview:controller.view];
[self downLoadData:page];
}
- (void)unloadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= [_arrUrlLinks count]) return;
controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller != [NSNull null]) {
if (nil != controller.view.superview)
[controller.view removeFromSuperview];
[viewControllers replaceObjectAtIndex:page withObject:[NSNull null]];
NSLog(#"Unloadscrollviewwithpage");
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (pageControlUsed) {
return;
}
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
[self unloadScrollViewWithPage:page - 2];
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
[self unloadScrollViewWithPage:page + 2];
count=page+1;
// [self newCountTitleSet];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadScrollView];
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}
i have successfully added my controllers over the scroll view now how to add text view over the controllers.
Try this,
....
if (nil == controller.view.superview) {
.....
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
//Add textview as subview to controller
UITextView *textView = [[UITextView alloc] init];
textView.text = #"hello";
textView.frame = //set frame here
//set tag to textView if needed
[controller.view addSubView:textView];
[scrollView addSubview:controller.view];
NSLog(#"loadscrollviewwithpage................>>>>>>>>>>>>");
}
....
For example you want to 10 diffrent images or textview you could use this logic:
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scr.tag = 1;
scr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:scr];
[self setupScrollView:scr];
UIPageControl *pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 264, 480, 36)];
[pgCtr setTag:12];
pgCtr.numberOfPages=10;
pgCtr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:pgCtr];
}
- (void)setupScrollView:(UIScrollView*)scrMain {
// we have 10 images here.
// we will add all images into a scrollView & set the appropriate size.
for (int i=1; i<10; i++) {
// create image
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"sti%02i.jpeg",i]];
// create imageView
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height)];
// set scale to fill
imgV.contentMode=UIViewContentModeScaleToFill;
// set image
[imgV setImage:image];
// apply tag to access in future
imgV.tag=i+1;
// add to scrollView
[scrMain addSubview:imgV];
UITextView*txtview =[[UITextView alloc]initWithFrame:CGRectMake(10,50,200,200)];
[txtview setDelegate:self];
[txtview setReturnKeyType:UIReturnKeyDone];
[txtview setTag:1];
[txtview setText:[yourArray ObjectAtIndex:i]];
[txtview setCornerRadius:5];
[scrMain addSubview:txtview];
}
// set the content size to 10 image width
[scrMain setContentSize:CGSizeMake(scrMain.frame.size.width*10, scrMain.frame.size.height)];
}

How to change text label when scroll view goes to view 2?

I'm making a select level with 3 levels that get scrolled horizontally - the scroll controls three views "View 1" "view 2" and "View 3", I need to make an illusion to the user, Ive placed 3 buttons on each view and half a button on each side of the UIview with 2 labels one writes "lev" the other "el 2"...
When the user moves to View 2 I'd like the label to be set to "el 2" and as soon as the scroll has settled on level 2 i'd like the label to write "el 1" - This would make an illusion and it would be so fast that the user wouldn't notice.
heres the code:
This:
[_elone setText:[NSString stringWithFormat:#"Level 2"]];
But I don't know where to put it,
Should I make an outlet, an action a statement?
#interface PagerViewController ()
#property (assign) BOOL pageControlUsed;
#property (assign) NSUInteger page;
#property (assign) BOOL rotating;
- (void)loadScrollViewWithPage:(int)page;
#end
#implementation PagerViewController
#synthesize scrollViewTwo;
#synthesize pageControlTwo;
#synthesize pageControlUsed = _pageControlUsed;
#synthesize page = _page;
#synthesize rotating = _rotating;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.scrollViewTwo setPagingEnabled:YES];
[self.scrollViewTwo setScrollEnabled:YES];
[self.scrollViewTwo setShowsHorizontalScrollIndicator:NO];
[self.scrollViewTwo setShowsVerticalScrollIndicator:NO];
[self.scrollViewTwo setDelegate:self];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
for (NSUInteger i =0; i < [self.childViewControllers count]; i++) {
[self loadScrollViewWithPage:i];
}
self.pageControlTwo.currentPage = 0;
_page = 0;
[self.pageControlTwo setNumberOfPages:[self.childViewControllers count]];
UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControlTwo.currentPage];
if (viewController.view.superview != nil) {
[viewController viewWillAppear:animated];
}
self.scrollViewTwo.contentSize = CGSizeMake(scrollViewTwo.frame.size.width * [self.childViewControllers count], scrollViewTwo.frame.size.height);
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0)
return;
if (page >= [self.childViewControllers count])
return;
// replace the placeholder if necessary
UIViewController *controller = [self.childViewControllers objectAtIndex:page];
if (controller == nil) {
return;
}
// add the controller's view to the scroll view
if (controller.view.superview == nil) {
CGRect frame = self.scrollViewTwo.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[self.scrollViewTwo addSubview:controller.view];
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
_pageControlUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
_pageControlUsed = NO;
}
- (IBAction) changePage :(id) sender {
}
How is this done?
// Implement the UIScrollview Delegate method
-(void)scrollViewDidScroll:(UIScrollView *)sender
{
CGFloat pageWidth = yourScrollView.frame.size.width;
int currentPage = floor((yourScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
if (currentPage != featureVenPageControll.currentPage)
{
[_elone setText:[NSString stringWithFormat:#"Level 2"]];
}
}

Leaks in my UIScrollView helper class

I want to publish tutorial about how to easily create an UIScrollView on http://www.xprogress.com/ and I just want to check with you guys if the code is alright before I publish anything. Any help will be appreciated and I'll put your name / website on the bottom of the article :)
Thanks a lot :)
Ondrej
header file:
///
/// IGUIScrollViewImage.h
///
/// IGUILibrary
///
/// Created by Ondrej Rafaj on 7.4.10.
///
/// Copyright 2010 Home. All rights reserved.
///
/// #todo enable margin and center the image to the middle of the view
/**
<b>Examples:</b>
<i>This is just a short example how to use this class</i>
<pre>
- (NSArray *)getImages {
NSMutableArray *arr = [[[NSMutableArray alloc] init] autorelease];
[arr addObject:[UIImage imageNamed:#"image-1.jpg"]];
[arr addObject:[UIImage imageNamed:#"image-2.png"]];
[arr addObject:[UIImage imageNamed:#"image-3.png"]];
[arr addObject:[UIImage imageNamed:#"image-4.jpg"]];
return (NSArray *)arr;
}
- (void)viewDidLoad {
IGUIScrollViewImage *svimage = [[IGUIScrollViewImage alloc] init];
[svimage setSizeFromScrollView:self.scrView]; // takes size of the scroll view you've already placed on stage via Interface Builder
// or
//[svimage setWidth:320 andHeight:240]; // half screen
[svimage enablePositionMemory]; // enables position (pagination) memory for this scroll view
// or
//[svimage enablePositionMemoryWithIdentifier:#"myIdentifier"]; if you have more instances of this scroll view in your application
[svimage enablePageControlOnBottom];
// or
//[svimage enablePageControlOnTop];
[self.myUIView addSubview:[svimage get]]; // and place it on the stage :)
[super viewDidLoad];
}
</pre>
*/
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface IGUIScrollViewImage : NSObject <UIScrollViewDelegate> {
UIScrollView *scrollView;
UIPageControl *pageControl;
CGRect rectScrollView;
CGRect rectPageControl;
int scrollWidth;
int scrollHeight;
NSArray *contentArray;
UIColor *bcgColor;
BOOL pageControlEnabledTop;
BOOL pageControlEnabledBottom;
BOOL rememberPosition;
NSString *positionIdentifier;
}
#property (nonatomic, retain) UIScrollView *scrollView;
- (int)getScrollViewWidth;
- (void)setWidth:(int)width andHeight:(int)height;
- (void)setSizeFromScrollView:(UIScrollView *)scView;
- (void)setBackGroudColor:(UIColor *)color;
- (void)setContentArray:(NSArray *)images;
- (void)enablePageControlOnTop;
- (void)enablePageControlOnBottom;
- (void)enablePositionMemory;
- (void)enablePositionMemoryWithIdentifier:(NSString *)identifier;
- (UIScrollView *)getWithPosition:(int)page;
- (UIScrollView *)getWithPositionMemoryIdentifier:(NSString *)identifier;
- (UIScrollView *)get;
#end
And the implementation file:
//
// IGUIScrollViewImage.m
// IGUILibrary
//
// Created by Ondrej Rafaj on 7.4.10.
// Copyright 2010 Home. All rights reserved.
//
#import "IGUIScrollViewImage.h"
#define kIGUIScrollViewImagePageIdentifier #"kIGUIScrollViewImagePageIdentifier"
#define kIGUIScrollViewImageDefaultPageIdentifier #"Default"
#implementation IGUIScrollViewImage
#synthesize scrollView;
- (int)getScrollViewWidth {
return ([contentArray count] * scrollWidth);
}
- (void)setWidth:(int)width andHeight:(int)height {
scrollWidth = width;
scrollHeight = height;
if (!width || !height) rectScrollView = [[UIScreen mainScreen] applicationFrame];
else rectScrollView = CGRectMake(0, 0, width, height);
}
- (void)setSizeFromScrollView:(UIScrollView *)scView {
scrollWidth = scView.frame.size.width;
scrollHeight = scView.frame.size.height;
rectScrollView = CGRectMake(0, 0, scrollWidth, scrollHeight);
}
- (void)setContentArray:(NSArray *)images {
contentArray = images;
}
- (void)setBackGroudColor:(UIColor *)color {
bcgColor = color;
}
- (void)enablePageControlOnTop {
pageControlEnabledTop = YES;
}
- (void)enablePageControlOnBottom {
pageControlEnabledBottom = YES;
}
- (void)enablePositionMemoryWithIdentifier:(NSString *)identifier {
rememberPosition = YES;
if (!identifier) identifier = kIGUIScrollViewImageDefaultPageIdentifier;
positionIdentifier = identifier;
}
- (void)enablePositionMemory {
[self enablePositionMemoryWithIdentifier:nil];
}
- (UIScrollView *)getWithPosition:(int)page {
if (!contentArray) {
contentArray = [[[NSArray alloc] init] autorelease];
}
if (page > [contentArray count]) page = 0;
if (!scrollWidth || !scrollHeight) {
rectScrollView = [[UIScreen mainScreen] applicationFrame];
scrollWidth = rectScrollView.size.width;
scrollHeight = rectScrollView.size.height;
}
rectScrollView = CGRectMake(0, 0, scrollWidth, scrollHeight);
self.scrollView = [[UIScrollView alloc] initWithFrame:rectScrollView];
self.scrollView.contentSize = CGSizeMake([self getScrollViewWidth], scrollHeight);
if (!bcgColor) bcgColor = [UIColor blackColor];
self.scrollView.backgroundColor = bcgColor;
self.scrollView.alwaysBounceHorizontal = YES;
self.scrollView.contentOffset = CGPointMake(page * scrollWidth, 0);
self.scrollView.pagingEnabled = YES;
UIImageView *imageView;
UIView *main = [[[UIView alloc] initWithFrame:rectScrollView] autorelease];
int i = 0;
for (UIImage *img in contentArray) {
imageView = [[UIImageView alloc] initWithImage:img];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
imageView.backgroundColor = [UIColor blackColor];
float ratio = img.size.width/rectScrollView.size.width;
CGRect imageFrame = CGRectMake(i, 0, rectScrollView.size.width, (img.size.height / ratio));
imageView.frame = imageFrame;
[self.scrollView addSubview:imageView];
i += scrollWidth;
}
[imageView release];
[main addSubview:scrollView];
if (pageControlEnabledTop) {
rectPageControl = CGRectMake(0, 5, scrollWidth, 15);
}
else if (pageControlEnabledBottom) {
rectPageControl = CGRectMake(0, (scrollHeight - 25), scrollWidth, 15);
}
if (pageControlEnabledTop || pageControlEnabledBottom) {
pageControl = [[[UIPageControl alloc] initWithFrame:rectPageControl] autorelease];
pageControl.numberOfPages = [contentArray count];
pageControl.currentPage = page;
[main addSubview:pageControl];
}
if (pageControlEnabledTop || pageControlEnabledBottom || rememberPosition) self.scrollView.delegate = self;
//if (margin) [margin release];
return (UIScrollView *)main;
}
- (UIScrollView *)get {
return [self getWithPosition:0];
}
- (UIScrollView *)getWithPositionMemoryIdentifier:(NSString *)identifier {
[self enablePositionMemoryWithIdentifier:identifier];
return [self getWithPosition:[[[NSUserDefaults alloc] objectForKey:[NSString stringWithFormat:#"%#%#", kIGUIScrollViewImagePageIdentifier, positionIdentifier]] intValue]];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sv {
int page = sv.contentOffset.x / sv.frame.size.width;
pageControl.currentPage = page;
if (rememberPosition) {
[[NSUserDefaults alloc] setObject:[NSString stringWithFormat:#"%d", page] forKey:[NSString stringWithFormat:#"%#%#", kIGUIScrollViewImagePageIdentifier, positionIdentifier]];
}
}
- (void)dealloc {
[scrollView release];
[super dealloc];
}
#end
Just a quick glance. You alloc UIImageView multiple times in
-(UIScrollView*)getWithPosition:(int)page and release it only once:
for (UIImage *img in contentArray) {
imageView = [[UIImageView alloc] initWithImage:img];
// ...
}
[imageView release];

Resources