I have 2 views are connected with NavigationController. Second view is scrollView with page controll. My main view has 1 UIButton. When pressing on it are obtained 3-4 seconds delay. There are 21 views in scrollView. And I have spurts when scrolling between view in scrollView. How can I reduce delay and remove spurts?
My viewDidLoad method:
-(void)viewDidLoad {
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0.0f, 280.0f, 320.0f, 20.0f)];
[self.view addSubview:self.pageControl];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 30.0f, 480.0f, 250.0f)];
[self.view addSubview:self.scrollView];
pageControlBeingUsed = NO;
NSInteger firstNumber = 1;
for (NSUInteger i = 0; i < 20; i++) {
BIDExercisePrototype *view = [[BIDExercisePrototype alloc] initWithNumber:firstNumber];
[self.viewsArray addObject:view];
firstNumber += 50;
}
for (NSUInteger i = 0; i < 20; i++) {
UIViewController *someController = [[UIViewController alloc] init];
someController = [self.viewsArray objectAtIndex:i];
someController.view.frame = CGRectMake(480.0f*i, 0.0f, 480.0f, 280.0f);
[self.scrollView addSubview:someController.view];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * self.viewsArray.count, self.scrollView.frame.size.height);
self.pageControl.backgroundColor = [UIColor clearColor];
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = self.viewsArray.count;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;
[self.pageControl addTarget:self action:#selector(changePage) forControlEvents:UIControlEventValueChanged];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (!pageControlBeingUsed) {
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (void)changePage {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
I think you should be able to follow the same practice for improving UITableView's scroll performance.
Do your drawing manually and have only one view in each cell.
Check out the following SO post which discusses multiple ways to improve scrolling performance by rendering your cells in specific ways.
Tricks for improving iPhone UITableView scrolling performance?
May I suggest you forget about recoding the wheel (so to speak) and use existing projects that have already solved the problems
Related
I want to pinch zoom in uiscrollview image. i added multiple image in uiscrollview
but pinch zoom is not working i want to pinch zoom UIImageview image. i have refer so many reference but still its not working
Thanks in advance
My code:-
for(int i = 0;i<aryImage.count;i++)
{
//Create a uiimageview
imageView =[[UIImageView alloc]init ];
imageView.frame = CGRectMake((280 *i),0.0, 280, 475);
imageView.image = [UIImage imageNamed:#"default.png"];
imageView.tag = i;
imageView.userInteractionEnabled = YES;
[scrollGallery addSubview:imageView];
scrollGallery.minimumZoomScale = 1.0;
scrollGallery.maximumZoomScale = 2.0;
scrollGallery.delegate = self;
[scrollGallery addSubview:imageView];
}
scrollGallery.contentSize = CGSizeMake(280 * aryImage.count, scrollGallery.frame.size.height);
pageControl.numberOfPages = aryImage.count;
pageControlBeingUsed = NO;
pageControl.currentPage = 0;
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
Try this:
- (void)scrollViewDidZoom:(UIScrollView *)sv
{
UIView* zoomView = [sv.delegate viewForZoomingInScrollView:sv];
CGRect zvf = zoomView.frame;
if(zvf.size.width < sv.bounds.size.width)
{
zvf.origin.x = (sv.bounds.size.width - zvf.size.width) / 2.0;
}
else
{
zvf.origin.x = 0.0;
}
if(zvf.size.height < sv.bounds.size.height)
{
zvf.origin.y = (sv.bounds.size.height - zvf.size.height) / 2.0;
}
else
{
zvf.origin.y = 0.0;
}
zoomView.frame = zvf;
}
You return only one imageView in viewForZooming but you added an array of them. This may be the cause of your problem. Try to replace your code like this:
UIView *containerView = [[UIView alloc] initWithFrame:scrollGallery.frame];
[scrollGallery addSubview:containerView];
scrollGallery.minimumZoomScale = 1.0;
scrollGallery.maximumZoomScale = 2.0;
scrollGallery.delegate = self;
for(int i = 0;i<aryImage.count;i++)
{
//Create a uiimageview
imageView =[[UIImageView alloc]init ];
imageView.frame = CGRectMake((280 *i),0.0, 280, 475);
imageView.image = [UIImage imageNamed:#"default.png"];
imageView.tag = i;
imageView.userInteractionEnabled = YES;
[containerView addSubview:imageView];
}
scrollGallery.contentSize = CGSizeMake(280 * aryImage.count, scrollGallery.frame.size.height);
// then maybe you need to resize containerView's frame
//..........
pageControl.numberOfPages = aryImage.count;
pageControlBeingUsed = NO;
pageControl.currentPage = 0;
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return containerView;
}
Hope it will help.
UPD: also try to disable userInteraction of your imageViews. They may block interaction with scrollGallery.
UPD2: store all imageViews in predefined array. For, example,
NSMutableArray *arr = [[NSMutableArray alloc] init];
for (int i=0; i<aryImage.count; i++) {
[arr addObject:[[UIImageView alloc] init]];
}
Then in your cycle instead of
imageView =[[UIImageView alloc] init];
use
UIImageView *imageView = [arr objectAtIndex:i];
And don't forget to release everything at the end ;)
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scrollView.backgroundColor = [UIColor redColor];
scrollView.maximumZoomScale = 1.0;
scrollView.minimumZoomScale = 1.0;
scrollView.clipsToBounds = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.pagingEnabled = YES;
self.view = scrollView;
pageControl = [[UIPageControl alloc] init];
pageControl.frame = CGRectMake(50, 350, 50, 50);
pageControl.numberOfPages = 3;
pageControl.currentPage = 0;
[self.view addSubview:pageControl];
I am new to iPhone software development. Please give me some code I could use here.
- (void)setupPage
{
UIView *blueView = [[UIView alloc] init];
blueView.frame = CGRectMake(0, 0, 640, 480);
blueView.backgroundColor = [UIColor whiteColor];
[scrollView addSubview:blueView];
self.pageControl.numberOfPages = 2;
[scrollView setContentSize:CGSizeMake(640, 0)];
}
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
if(pageControlIsChangingPage){
return;
}
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
}
you can create array of UIViewControllers like bellow....
NSMutableArray *viewControllers;
and then just add the viewControllers or views with for loops like bellow...
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < 5; i++) {
[controllers addObject:yourViewControlls];// use different viewControls
}
self.viewControllers = controllers;
so here 5 viewConreoller are added in array and use it in PageViewController
for more info see this tutorial and example..
pagecontrol-example-in-iphone
I have a VC with a scrollview and pagecontrol implemented in it. It has three pages, which are all set up correctly and working fine. What I want to be able to do is have the VC load onto different pages depending on what button a user presses.
In the method the button calls I have
ViewController *vc = [[ViewController alloc] init];
vc.pageControl.currentPage = 2;
[self.navigationController pushViewController:vc animated:YES];
When the VC loads, it is loaded onto the correct page (as evidenced by the bubbles at the bottom of the page) but it is displaying the information from page[0]. If I start scrolling, it jumps back to page[0] no matter what I do.
My best guess is that I need to make the VC load to a specific x origin depending on what page I want to display, but I haven't been able to find anything related to this, so I'm quite lost.
Appreciate any help.
EDIT: Here is the code from the VC class. I didn't write this and it is my first time working with PageControl, so I'm trying to learn as I go.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.scrollView = [[UIScrollView alloc] init];
self.scrollView.pagingEnabled = TRUE;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.delegate = self;
firstPage = [[UIView alloc] init];
firstPage.tag = 0;
secondPage = [[UIView alloc] init];
secondPage.tag = 1;
thirdPage = [[UIView alloc] init];
thirdPage.tag = 2;
pages = [[NSArray alloc] initWithObjects:firstPage, secondPage, thirdPage, nil];
// Page Control
self.pageControl = [[UIPageControl alloc] init];
self.pageControl.numberOfPages = 3;
self.pageControl.currentPage = 0;
[self.view addSubview:self.scrollView];
[self.view addSubview:self.pageControl];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// First Page Setup
//
[self setupFirstPage];
// Second Page Setup
//
[self setupSecondPage];
// Third Page Setup
//
[self setupThirdPage];
// Frame everything
//
CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
CGRect navBarFrame = self.navigationController.navigationBar.frame;
self.scrollView.frame = CGRectMake(0, navBarFrame.size.height, 320, screenFrame.size.height - navBarFrame.size.height);
self.pageControl.frame = CGRectMake(0, screenFrame.size.height - 70, 320, 20);
firstPage.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
secondPage.frame = CGRectMake(320 * secondPage.tag, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
thirdPage.frame = CGRectMake(320 * thirdPage.tag, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
// Add Subviews
//
[self.scrollView addSubview:firstPage];
[self.scrollView addSubview:secondPage];
[self.scrollView addSubview:thirdPage];
self.scrollView.contentSize = CGSizeMake(320 * pages.count, self.scrollView.frame.size.height);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
Try adding this function and calling it right after you set the page number:
- (void)pageSelectorChanged
{
CGRect frame = self.scrollView.frame;
frame.origin.x = frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
[self.scrollView scrollRectToVisible:frame animated:NO];
}
I need to implement a page scroll view. Each page view should load a separate view controller. On performing actions/events on the view controller should update/refresh the page view.Is it possible? If yes , how? It would be very much grateful If you are able to give me pointers in this regard.
-(void)createScrollView
{
myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 440)];
myScroll.pagingEnabled = YES;
myScroll.showsHorizontalScrollIndicator = YES;
myScroll.showsVerticalScrollIndicator = YES;
myScroll.scrollsToTop = NO;
myScroll.delegate = self;
int n = [array count];
myScroll.contentSize = CGSizeMake((320 * n), 440); // n = number of views
for (int i = 0 , X = 0; i < [photoArray count]; i++ , X += 320)
{
UIImageView *img =[[UIImageView alloc]initWithFrame:CGRectMake(X,0, 320, 480)]; //here view for you
CGRect frame;
frame.size.width=320; frame.size.height=240;
frame.origin.x=0; frame.origin.y=88;
[img addSubview:asyncImage];
img.contentMode=UIViewContentModeScaleAspectFit;
[myScroll addSubview:img];
}
}
- (void) viewDidLoad
{
[self createScrollView];
[myScroll scrollRectToVisible:CGRectMake(320*photoNumber, 0, 320 , 240) animated:NO];
[self.view addSubview:myScroll];
}
its a sample code .... try it with changes... hope that finally work for you...
I'm using pageControl in my application. View's are not changing on click on dots but on the either end's of pageControl, when i click view's are changing. I want them to change on clicks on dot. What to do? Is there any method i need to implement?
Here's the Code
#import "ScrollingViewController.h"
#implementation ScrollingViewController
#synthesize scrollView;
#synthesize pageControl;
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupPage];
pageControl =[[UIPageControl alloc] initWithFrame:CGRectMake(0,390,320,100)];
pageControl.userInteractionEnabled =YES;
pageControl.numberOfPages = 5;
pageControl.currentPage = 0;
[self.pageControl setBackgroundColor:[UIColor blackColor]];
pageControl.enabled = TRUE;
[pageControl setHighlighted:YES];
[pageControl addTarget:self action:#selector(changePage:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pageControl];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload
{
[scrollView release];
[pageControl release];
}
- (void)dealloc
{
[super dealloc];
}
- (void)setupPage
{
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,400)];
[scrollView setContentSize:CGSizeMake(1000, 800)];
//scrollView.contentSize = CGSizeMake(1000,800);
scrollView.scrollsToTop = NO;
[self.scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
NSUInteger nimages = 0;
CGFloat cx = 0;
for (; ; nimages++)
{
NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", (nimages + 1)];
UIImage *image = [UIImage imageNamed:imageName];
if (image == nil)
{
break;
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = image.size.height;
rect.size.width = image.size.width;
rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx;
rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);
imageView.frame = rect;
[scrollView addSubview:imageView];
[imageView release];
cx += scrollView.frame.size.width;
}
self.pageControl.numberOfPages = nimages;
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
scrollView.delegate = self;
[self.view addSubview:scrollView];
}
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
if (pageControlIsChangingPage)
{
return;
}
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
pageControlIsChangingPage = NO;
}
- (IBAction)changePage:(id)sender
{
/*
* Change the scroll view
*/
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * pageControl.currentPage;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
pageControlIsChangingPage = YES;
}
#end
Increase the width of the page control as much as possible. Even if you keep the width of page control short enough to as many dots that it cannot hold, it can show them. But fails to receive the action.
Use in ScrollView.m
#pragma mark -
#pragma mark loadGalleryView
-(void) loadGalleryView{
galleryArr = [memberDic objectForKey:#"arrKey"];
if ([galleryArr count]%5 != 0)
{
noOfPages = ([galleryArr count]/5)+1;
}
else
{
noOfPages = [galleryArr count]/5;
}
viewControllers = [[NSMutableArray alloc] init];
for (int i=0; i<noOfPages; i++)
{
[viewControllers addObject:[NSNull null]];
}
[galleryScrollView setPagingEnabled:TRUE];
[galleryScrollView setContentSize:CGSizeMake(self.view.frame.size.width* noOfPages,69.0f)];
[galleryScrollView setShowsHorizontalScrollIndicator:FALSE];
[galleryScrollView setShowsVerticalScrollIndicator:FALSE];
[galleryScrollView setScrollsToTop:FALSE];
[galleryScrollView setDelegate:self];
[pageControl setNumberOfPages:noOfPages];
[pageControl setCurrentPage:0];
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}
//-----------------Load scroll View----------------------------------
-(void) loadScrollViewWithPage:(int) page{
if (page < 0)
{
return;
}
if (page >= noOfPages)
{
return;
}
GalleryViewController *givc = [viewControllers objectAtIndex:page];
if ((NSNull *)givc == [NSNull null])
{
givc = [[GalleryViewController alloc] initWithPageNumber:page];
givc.imageArr = [galleryArr retain];
[viewControllers replaceObjectAtIndex:page withObject:givc];
[givc release];
}
if (nil == givc.view.superview)
{
CGRect frame = self.view.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
givc.view.frame = frame;
[galleryScrollView addSubview:givc.view];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = galleryScrollView.frame.size.width;
int page = floor((galleryScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// A possible optimization would be to unload the views+controllers which are no longer visible
}
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
isPageControlUsed = NO;
}
Use in GalleryView .m
- (void)viewDidLoad {
[super viewDidLoad];
float x = 7.0f;
for (int i = (pageNumber*5); i<(pageNumber+1)*5; i++)
{
if (i<[imageArr count])
{
NSString *url = [imageArr objectAtIndex:i];
MyImageView *imgView = [[MyImageView alloc] initWithFrame:CGRectMake(x, 7.5f, 55.0f, 55.0f)];
[imgView addImageFrom:url];
[self.view addSubview:imgView];
[imgView release];
x = x+62.5f;
}
}
}
-(id)initWithPageNumber:(int) page{
if (self = [super initWithNibName:#"GalleryViewController" bundle:nil])
{
pageNumber = page;
}
return self;
}